Hi I have to develop an app so I have a device (the server) with 3 clients. I make all the validation, turn on the bluetooth, find devices and all work great. But when I'm going to connect a device I don't know what happen.
I'm using the next code, when I click a device I want to connect it. I only have my app in the mother device.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
try {
if(btADapter.isDiscovering()){
btADapter.cancelDiscovery();
}
if(listAdapter.getItem(position).contains("Paired")){
BluetoothDevice selectedDevice = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
}
else{
BluetoothDevice selectedDevice = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
//pairDevice(devices.get(position));
//Toast.makeText(getApplicationContext(), "device is not paired", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
System.out.println(e);
}
}
Here I have a question, what happen if its not paired? if I try to connect its going to pair them automatically?
My UUID is: "00001101-0000-1000-8000-00805F9B34FB" Then my connect code:
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server
// code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btADapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
System.out.println(connectException);
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
}
}
}
When I do mmSocket.connect(); the app crash and returns: java.io.IOException: Service discovery failed and I don't know what to do.
Please I need help. Thanks.
EDIT
After looking for more answers, i found in this question (Android Bluetooth Connection - Service Discovery Failed) the answer of Sandeep Maram and connects perfectly. This is the code:
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();