TShopping

 找回密碼
 註冊
搜索
查看: 7247|回復: 0
打印 上一主題 下一主題

[教學] Android BLE ESP32的onCharacteristicChanged沒有回撥及getDescriptor null

[複製鏈接]
跳轉到指定樓層
1#
發表於 2020-4-23 11:39:39 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
下篇問題
如何在Android BLE 發送數據 ESP32 ?
如何在Android BLE 發送到 ESP32 超過20個字元的數據?


描述:當裝置為 Indication 模式時,裝置的值有變化時會主動返回給App,App在 onCharacteristicChanged() 方法中能收到返回的值。

Indication: 從機會先向主機發送一條通知,主機接收到通知後去讀取從機資料
Notification:從機直接傳送給主機資料


問題:在App中通過如下程式碼註冊監聽,註冊成功後就能接收到裝置主動反饋的值了。然而以下程式碼執行後依舊收不到反饋。但是對裝置的讀寫都是可行的,並且iOS端可以接收到通知。

  1. bluetoothGatt.setCharacteristicNotification(characteristic, true)
複製代碼

解決: 當上面的方法執行返回true後,還要執行如下的程式碼才能註冊成功。

  1. for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){
  2.     if (dp != null) {
  3.         if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
  4.             dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  5.         } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
  6.             dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
  7.         }
  8.         gatt.writeDescriptor(dp);
  9.     }
  10. }
複製代碼

在BluetoothLeService.java加入完整程式碼如下:
  1. public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) {
  2.     boolean success = false;
  3.     BluetoothGattService service = gatt.getService(serviceUUID);
  4.     if (service != null) {
  5.         BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID);
  6.         if (characteristic != null) {
  7.             success = gatt.setCharacteristicNotification(characteristic, true);
  8.             if (success) {
  9.                 for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){
  10.                     if (dp != null) {
  11.                         if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
  12.                             dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  13.                         } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) {
  14.                             dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
  15.                         }
  16.                         gatt.writeDescriptor(dp);
  17.                     }
  18.                 }
  19.             }
  20.         }
  21.     }
  22.     return success;
  23. }

  24. private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) {
  25.     BluetoothGattCharacteristic characteristic = null;
  26.     List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
  27.     for (BluetoothGattCharacteristic c : characteristics) {
  28.         if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0
  29.                 && characteristicUUID.equals(c.getUuid())) {
  30.             characteristic = c;
  31.             break;
  32.         }
  33.     }
  34.     if (characteristic != null)
  35.         return characteristic;
  36.     for (BluetoothGattCharacteristic c : characteristics) {
  37.         if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0
  38.                 && characteristicUUID.equals(c.getUuid())) {
  39.             characteristic = c;
  40.             break;
  41.         }
  42.     }
  43.     return characteristic;
  44. }
複製代碼

舉例來說,如ESP32下圖


這裡還有一個陷阱,為什麼標1, 2 ,3 呢?
當我迴圈在抓CHARACTERISTIC時
先進入1. SERVICE
然後是2. Notify或 write CHARACTERISTIC
但是2.要選擇Notify 才能抓到 Client CHARACTERISTIC Config UUID 0X2902
如果2.是選 write CHARACTERISTIC,就怎麼都看不到 Client CHARACTERISTIC Config UUID


展示影片


參考文章
https://www.itread01.com/content/1548417271.htmlhttps://stackoverflow.com/questions/58998064/android-bluedroid-ble-certain-phones-arent-receiving-notifications-from-gatt-s

來源
http://www.netyea.com





 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-19 12:29 , Processed in 0.060848 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表