Reputation: 41
I'm using a code derived from Bluetooth tutorial on Android Developer page. My app tries to connect to a BT weighing scale and record the weight information.
My program throws an error when it attempts to perform a BluetoothSocket.connect(); The program runs as a Bluetooth client/slave in order to receive the data from a BT device. The user sees a list of paired devices on the screen and clicks on a particular device in order to connect to it.
Hence, when the user clicks on a specific device, the app tries to connect to it since the device is already paired beforehand.
The app obtains a BluetoothSocket but fails to perform the connect()
I have tried to just establish a connection with other Bluetooth devices but I always end up getting the same error over and over.
The following is the java code for connectThread:
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;
Log.i(tag, "construct");
// 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) {
Log.i(tag, "get socket failed");
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (IOException connectException) { Log.i(tag, "connect failed");
// 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();
}
The entire code for the app can be found here
The following is the log I obtained while running the app:
01-01 01:46:00.359: I/ActivityManager(1553): Displayed com.test.bluetooth/.Main_Activity: +579ms
01-01 01:46:00.953: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:00.968: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:19:15:66:C4:2D)
01-01 01:46:00.984: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:1A:88:00:01:25)
01-01 01:46:01.000: D/BluetoothService(1553): uuid(system): 00001105-0000-1000-8000-00805f9b34fb 1
01-01 01:46:04.421: I/debugging(2400): construct
01-01 01:46:04.421: D/BluetoothEventLoop(1553): Property Changed: Discovering : false
01-01 01:46:04.421: I/debugging(2400): in click listener
01-01 01:46:04.421: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.429: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.437: I/debugging(2400): connect - run
01-01 01:46:04.437: V/BluetoothDiscoveryReceiver(1864): Received: android.bluetooth.adapter.action.DISCOVERY_FINISHED
01-01 01:46:04.445: E/BluetoothEventLoop.cpp(1553): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
01-01 01:46:05.484: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: true
01-01 01:46:06.554: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:06.562: D/BluetoothService(1553): uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1
01-01 01:46:06.562: D/BluetoothService(1553): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1
01-01 01:46:06.562: I/debugging(2400): connect failed
01-01 01:46:06.562: W/System.err(2400): java.io.IOException: Invalid argument
01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
01-01 01:46:06.562: W/System.err(2400): at com.test.bluetooth.Main_Activity$ConnectThread.run(Main_Activity.java:326)
01-01 01:46:10.132: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: false
01-01 01:47:05.914: D/SurfaceFlinger(1311): About to give-up screen, flinger = 0x41876af0
Any/all help in order to set up a basic BT connection is greatly appreciated.
Thank you.
Upvotes: 4
Views: 6791
Reputation: 67
Got a same problem while connecting to bluetooth device other than mobile. Changing UUID value works for me. Earlier I was using UUID value mention at https://github.com/googlesamples/android-BluetoothChat/blob/master/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java but that is for mobile bluetooth device then i changed my UUID with 00001101-0000-1000-8000-00805F9B34FB and it works
Upvotes: 0
Reputation: 31
Check to make sure that the UUID you're using is appropriate for the type of device you're trying to connect to.
Upvotes: 3