1

I have built an android app that can connect to a peripheral device by bluetooth(Classic Bluetooth) using BluetoothSocket.connect() and do some interaction with it. The app works well on Samsung Galaxy S3(4.3) and Note 3(4.3), but when on Nexus 5, the app can't connect to the device.

I have 2 Nexus 5, and both of them can't connect, what make me think there must be something wrong with Nexus 5, not me. But when I use the app supported by the manufacturer of the device to connect to the device, the connection always can be established, even using Nexus 5.

So I guess, there must be something wrong with Nexus 5 on classic bluetooth, what make it not very easy to establish a connection, and there might be some way to solve it.

Sorry for my poor English.Thanks for any pointers on how to solve this problem.

Here are 3 blocks of my logcat info:

logcat info of Samsung Galaxy S3(worked):

04-01 17:03:33.340 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:03:33.360 D/BluetoothUtils﹕ isSocketAllowedBySecurityPolicy start : device null
04-01 17:03:33.360 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:03:33.360 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:03:33.365 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:03:35.095 D/BloodPressureService﹕ connected
04-01 17:03:35.095 D/BloodPressureService$ConnectedThread﹕ create ConnectedThread
04-01 17:03:35.105 I/BloodPressureService$ConnectedThread﹕ BEGIN mConnectedThread
04-01 17:03:35.110 D/BloodPressureService﹕ setState() 1 -> 2
04-01 17:03:35.125 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 2

logcat info of Nexus 5 at the first time(worked.Don't know why, I never seen that before. But just one time):

04-01 17:14:37.577 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:14:37.577 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:14:37.577 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:14:37.587 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:14:37.587 D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[47]}
04-01 17:14:39.207 D/BloodPressureService﹕ connected
04-01 17:14:39.207 D/BloodPressureService$ConnectedThread﹕ create ConnectedThread
04-01 17:14:39.207 D/BloodPressureService﹕ setState() 1 -> 2
04-01 17:14:39.207 I/BloodPressureService$ConnectedThread﹕ BEGIN mConnectedThread
04-01 17:14:39.217 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 2

logcat info of Nexus 5 at the 2nd time(didn't work. And never work again):

04-01 17:14:59.977 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:14:59.977 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:14:59.977 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:14:59.977 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:14:59.987 D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[56]}
rengwuxian
  • 650
  • 1
  • 6
  • 11
  • This question is not related to programming. – Lucifer Apr 01 '14 at 06:58
  • Add some logcat output when you try and use `BluetoothSocket.connect`. Otherwise, your question is probably going to be closed. – jww Apr 01 '14 at 08:37
  • Also worth considering that this is a known issue https://code.google.com/p/android/issues/detail?id=41415 – user1840255 Apr 01 '14 at 09:33
  • Odd the question was closed with "general computing hardware and software". You got a re-open vote because the reason was wrong and there is logcat to help identify the issue. – jww Apr 01 '14 at 09:41

1 Answers1

1

There is some changes in the Bluetooth Stack with Nexus 5, it is the most annoying thing ever. When you open the socket you could try using an insecure socket and see if that helps. Also make sure all existing sockets are closed and null:-

adapter= BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = adapter.getRemoteDevice(mAddress);
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID);
socket.connect();

At this point you should try and retrieve the output streams:-

 out = socket.getOutputStream();
 in = socket.getInputStream();

Also make sure you fully close the socket:-

        if (in != null) { 
            in.close();
            in = null;
        }
        if (out != null) { 
            out.close();
            out = null;
        }
        if (socket != null) { 
            socket.close();
            socket = null;
        }
user1840255
  • 287
  • 1
  • 6
  • 15
  • 1
    Taken your advice, and did some optimization in my code. Now my Nexus 5 can connect the device sometimes-but only sometimes. Anyway, thanks. – rengwuxian Apr 01 '14 at 10:19
  • I found making sure I fully closed the Bluetooth socket also helped. (added to answer) – user1840255 Apr 02 '14 at 07:32