11

I'm using Android Nexus 7 to connect a device via Bluetooth Low Energy link. I'm able to connect the device, and stay connected if I don't do any communication with the device.

However, if I enable the notification of one specific characteristic by clicking a button, then the device would disconnect with the tablet after a few seconds' data transmission.

Does anyone know what might be the problem? Thank you very much!

Here's my code:

    public boolean setCharacteristicNotification(boolean enabled){

      if (mBluetoothAdapter == null || mBluetoothGatt == null) {
          Log.w(TAG, "BluetoothAdapter not initialized");
               return false;      
      }

      BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
      if (Service == null) {
          Log.e(TAG, "service not found!");
          return false;
      }

      BluetoothGattCharacteristic characteristic = Service.getCharacteristic(UUID_MY_CHARACTERISTIC);

      final int charaProp = characteristic.getProperties();

      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
          mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

            mBluetoothGatt.writeDescriptor(descriptor);

          return true;
      }

    return false;

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Magic
  • 385
  • 1
  • 5
  • 13

2 Answers2

4

(Answered in a question edit. Converted to a community wiki answer. See What is the appropriate action when the answer to a question is added to the question itself? )

The OP wrote:

I solved this problem today.

Just change descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

to descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

Follow Up:

After I did some research and testing, I found that the automatically disconnection problem has something to do with the interference between Bluetooth and WIFI on Nexus 7. If I turned off the WIFI, then the disconnection problem of Bluetooth has gone. And this problem did not occur on Galaxy 3,4,5.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • @Brain : Before apply this every characteristics will notifying continue some time and then it disconnect it self. after change this even single characteristics not notify single time and disconnect sometime same. – Mehul Kabaria Jan 31 '18 at 07:57
  • @Brain : i have tried connecting with mac from android app. But its not connected, and status will be disconnected at every time. status code will be 133. But device will be paired. – sameer May 28 '19 at 10:00
0

Problem: I was having same problem on Tesco Hudl 2, if i transmit some data soon as Bluetooth is connected, it will disconnect.

Solution: Wait for few seconds after connection, it seems to work okay.

iUK
  • 31
  • 8