Android BLE 通知:setCharacteristicNotification 方法是否足够?
Android BLE notification: method setCharacteristicNotification is enough?
我正在开发两个应用程序,一个是外围角色,一个是核心角色。
外设
2 特征:
- 一个(称为 pippo)与 write_noresponse 属性
- 一个(称为 paperino)具有读取和通知功能 属性
y
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);
}
客户端
我已经启用了 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 还为时过早?
您必须写入描述符,它是蓝牙规范的一部分。 iOS 实现为您抽象出来。然而,Android 实现需要手动写入描述符。
我认为我前一段时间写的这个答案仍然适用:Any way to implement BLE notifications in Android-L preview
我正在开发两个应用程序,一个是外围角色,一个是核心角色。
外设
2 特征:
- 一个(称为 pippo)与 write_noresponse 属性
- 一个(称为 paperino)具有读取和通知功能 属性
y
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);
}
客户端
我已经启用了 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 还为时过早?
您必须写入描述符,它是蓝牙规范的一部分。 iOS 实现为您抽象出来。然而,Android 实现需要手动写入描述符。
我认为我前一段时间写的这个答案仍然适用:Any way to implement BLE notifications in Android-L preview