[英]BLE Device disconnect with Android device automatically. Android BLE
我正在使用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上遇到了同樣的問題,如果我在連接藍牙后立即傳輸一些數據,它將斷開連接。
解決方案:連接后等待幾秒鍾,似乎工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.