BLE设备自动与Android设备断开连接.Android BLE

Mag*_*gic 10 android bluetooth bluetooth-lowenergy android-4.3-jelly-bean android-4.4-kitkat

我正在使用Android Nexus 7通过Bluetooth Low Energy链接连接设备.如果我不与设备进行任何通信,我可以连接设备并保持连接.

但是,如果我通过单击按钮启用一个特定特征的通知,则设备将在数秒传输数据后与平板电脑断开连接.

有谁知道可能是什么问题?非常感谢你!

这是我的代码:

    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;

}
Run Code Online (Sandbox Code Playgroud)

Bri*_*汤莱恩 4

(在问题编辑中回答。转换为社区 wiki 答案。请参阅当问题的答案添加到问题本身时,适当的操作是什么?

OP 写道:

我今天解决了这个问题。

只是改变descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);

跟进:

经过我的一些研究和测试,我发现自动断开的问题与Nexus 7上的蓝牙和WIFI之间的干扰有关。如果我关闭WIFI,那么蓝牙断开的问题就消失了。而这个问题在Galaxy 3、4、5上没有出现。