我使用的是Android的Nexus 7通过蓝牙低耗能链接连接的设备。我能够连接到设备,并保持连接,如果我不这样做与设备的任何沟通。
不过,如果我通过点击一个按钮,使一个具体特性的通知,然后该设备将断开与几秒钟的数据传输后片剂。
有谁知道可能是什么问题?非常感谢你!
下面是我的code:
公共布尔setCharacteristicNotification(启用布尔){ 如果(mBluetoothAdapter == NULL || mBluetoothGatt == NULL){ Log.w(TAGBluetoothAdapter未初始化); 返回false; } BluetoothGattService服务= mBluetoothGatt.getService(UUID_MY_SERVICE); 如果(服务== NULL){ Log.e(TAG的服务没有找到!); 返回false; } BluetoothGattCharacteristic特征= Service.getCharacteristic(UUID_MY_CHARACTERISTIC); 最终诠释charaProp = characteristic.getProperties(); 如果((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY)大于0){ mBluetoothGatt.setCharacteristicNotification(特点,启用); BluetoothGattDescriptor描述符= characteristic.getDescriptor( UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(描述); 返回true; } 返回false;}
解决方案
(在回答一个问题的编辑,转换为一个社区维基答案。参见What是当一个问题的答案将被添加到问题本身进行相应的操作?)
在OP写道:
今天我解决了这个问题。
只要改变 descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
到 descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
后续行动:
我做了一些研究和试验之后,我发现,自动断线问题有事情做对的Nexus 7蓝牙和WIFI之间的干扰。如果我关掉了WIFI,蓝牙随后的断线问题已经一去不复返了。并没有对银河3,4,5出现此问题。
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;
}
解决方案
(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.