我正在使用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; }
(在问题编辑中回答。转换为社区维基答案。请参阅问题答案添加到问题本身时,适当的操作是什么? )
OP写道:
我今天解决了这个问题。
只需更改descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
到descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
跟进:
在我做了一些研究和测试后,我发现自动断线问题与Nexus 7上的蓝牙和WIFI之间的干扰有关。如果我关闭了WIFI,那么蓝牙的断开问题已经消失。 并且这个问题在Galaxy 3,4,5上没有发生。
问题:我在Tesco Hudl 2上遇到了同样的问题,如果我在连接蓝牙后立即传输一些数据,它将断开连接。
解决方案:连接后等待几秒钟,似乎工作正常。