gpt4 book ai didi

安卓 BLE 通知 : method setCharacteristicNotification is enough?

转载 作者:行者123 更新时间:2023-11-29 17:37:16 24 4
gpt4 key购买 nike

我正在开发两款应用,一款是外围应用,一款是核心应用。

周边

2 特点:

  • 一个(称为 pippo)具有 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);
}

客户端

我已经启用了 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

关于安卓 BLE 通知 : method setCharacteristicNotification is enough?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com