2016-01-13 76 views
0

我試圖設置一個應用程序來表現爲使用Android 5.0(21)api的BLE外圍設備。到目前爲止,我已經建立了服務器,廣告和連接工作,設置自定義的GATT服務和特性,並且可以在外圍設備和其他測試設備之間讀寫。現在我試圖將通知添加到一些參數中,但只是簡單地放置;他們不工作。Android BLE GATT外設模式通知

當定義這些特性,它們包括通知屬性:

BluetoothGattService battService = new BluetoothGattService(BatteryProfile.UUID_BATTERY_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY); 

BluetoothGattCharacteristic batteryLevelCharacteristic = 
     new BluetoothGattCharacteristic(BatteryProfile.UUID_BATTERY_LEVEL, 
       BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, 
       BluetoothGattCharacteristic.PERMISSION_READ); 
battService.addCharacteristic(batteryLevelCharacteristic); 

mGattServer.addService(battService); 

在我BluetoothGattServerCallback,我包括方法:

@Override 
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) { 
    Log.d(TAG, "Descriptor write: " + descriptor.toString()); 
    if (responseNeeded) { 
     mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, value); 
    } 
    super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value); 
} 

,我希望記錄的「描述寫:」消息每當客戶端啓用通知時(或者爲此寫入任何描述符);但是沒有記錄過這樣的消息(調試器也沒有進入該功能)。

後來我嘗試使用發送通知:

BluetoothGattCharacteristic batteryLevelCharacteristic = service.getCharacteristic(BatteryProfile.UUID_BATTERY_LEVEL); 
batteryLevelCharacteristic.setValue(new byte[]{ mBatteryLevel }); 
mGattServer.notifyCharacteristicChanged(mConnectedDevice, batteryLevelCharacteristic, false); 

那些雖然更新的價值;它不會向已連接的客戶端發送通知。

我已經在Nexus 5X上測試了這個使用Android 6.0.1的版本(不幸的是我只能用於測試的Android設備)。在那裏沒有太多使用外設/ BluetoothGattServer API的例子,所以我有點迷路。是否有一些方法我忘記調用,或者我需要在某些未公開的順序中執行某些操作才能使通知生效?

任何幫助將不勝感激!

回答

3

我不知道你是否已經找到了解決方案。但我認爲你可以通過爲你需要通知的特徵定義客戶端配置特徵描述符來嘗試。

這可以按照這篇文章所示完成。 Any way to implement BLE notifications in Android-L preview

+0

我忘了我問過,是的,我確實找到了解決方案。我無法確切地記得它修復了什麼,但我確定你是對的 - 我需要使用並寫入配置描述符。 – FuzzyWuzzie