我正在開發兩個應用程序一個外設和一箇中心角色。Android BLE通知:方法setCharacteristicNotification是否夠用?
外設
2特性:
- 一個(稱爲皮波)與write_noresponse屬性
- 一個(稱爲paperino)具有讀取和通知屬性
ÿ
private void addServiceToGattServer() {
Service = new BluetoothGattService(
UUID.fromString(CostantUUID.xxx),
BluetoothGattService.SERVICE_TYPE_PRIMARY);
paperino = new BluetoothGattCharacteristic(
UUID.fromString(CostantUUID.xxx),
BluetoothGattCharacteristic.PROPERTY_NOTIFY|BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
clientCharacteristiConfiguration = new BluetoothGattDescriptor(UUID.fromString(CostantUUID.clientCharacteristiConfiguration), BluetoothGattDescriptor.PERMISSION_WRITE);
paperino.addDescriptor(clientCharacteristiConfiguration);
Service.addCharacteristic(paperino);
pippo = new BluetoothGattCharacteristic(
UUID.fromString(CostantUUID.userCommands),
BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE ,
BluetoothGattCharacteristic.PERMISSION_WRITE);
ebikeService.addCharacteristic(pippo);
mGattServer.addService(Service);
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)
{
if(responseNeeded)
mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
................
...............
paperino.setValue(payload);
mGattServer.notifyCharacteristicChanged(device,paperino, false);
}
客戶端
我已經abilitated的setCharacteristicNotification並寫了特徵Paperino描述符:
bluetoothGatt.setCharacteristicNotification(characteristic, true);
/**
* @author I
* I abilitate the cliatcharacteristicconfiguration descriptor
*/
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CostantUUid.clientCharacteristiConfiguration));
if(descriptor != null)
{
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
這樣所有功能正常工作,但我不知道:這是真的需要在外圍端添加描述符並從客戶端寫入它?
我試圖不使用它,它不起作用。但在互聯網教程永遠不會說寫描述符...
什麼是真的?
此外,如果我做了第一個notifyCharacteristicChanged(device,paperino,false);在onConnectionStateChange回調
@Override
public void onConnectionStateChange(final BluetoothDevice device,
int status, int newState) {
super.onConnectionStateChange(device, status, newState);
Log.d(TAG, "onConnectionStateChange status=" + status + "->"
+ newState);
message=myHandler.obtainMessage();
if(newState == BluetoothProfile.STATE_CONNECTED)
{
disconnected=false;
Bundle f=new Bundle();
f.putString("connectionStatus", (String)(Utils.getStateDescription(newState)));
message.setData(f);
myHandler.sendMessage(message);
connectedDevice=device;
paperino.setValue(examplemsg);
mGattServer.notifyCharacteristicChanged(device, paperino,false);
}
在服務器端的「examplemessage」通知沒有到達客戶端。我必須第一次打電話給onDescriptorWrite中的notifyCharacteristicChanged ...
也許爲時過早調用notifycharacteristicChanged?
嗨!謝謝:)我已經按照你的答案來創建我的應用程序!我寫這篇文章是因爲在很多教程中從來沒有寫過描述符..過了一段時間後,懷疑出現在我的腦海中。 對於另一個問題,你有一些建議嗎?我已經添加了一些代碼行,以更好地解釋我的疑問:) – aeroxr1
如果您在onConnectionStateChanged回調中調用它,則說得過早調用notifyCharacteristicChanged。通過這樣做,您不會讓客戶有機會寫入描述符,實際上該通知會針對該特徵開啓通知。 你在onDescriptorWriteRequest中有它應該工作得很好。 – ludwigmace
因爲我認爲我太早調用了notifyCharacteristicChanged,所以我也嘗試在onConnectionStateChanged中啓動一個線程,每秒30秒發送一個notifyCharacteristicChanged。但最終結果並沒有改變,客戶也沒有收到通知。爲什麼?我試圖更好地理解問題:) – aeroxr1