Andoird [蓝牙 LE] 在外围设备(Android 应用程序)中广告服务之前创建具有预定义值的特征

问题描述 投票:0回答:1

我的应用程序充当外围设备,广告具有预定义值的特征的服务,中央设备读取它并处理广告数据

但我一直在努力在广告时设置服务中的特征值

setValue
类中有
BluetoothGattCharacteristic
方法,但已弃用

这是我的代码片段


val rxCharacteristic = BluetoothGattCharacteristic(
    UUID.fromString("myuuid"),
    BluetoothGattCharacteristic.PROPERTY_READ,
    BluetoothGattCharacteristic.PERMISSION_READ
)
rxCharacteristic.setValue(payload)

上面的代码可以工作,但特征值为空,并且 android studio 向我显示该方法已被弃用,这是它的文档页面:

https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic#setValue(java.lang.String)

正如我所说,我的用例是外围设备(移动应用程序)提前设置特征值,然后中央设备读取它

我成功地在

iOS
中用
CoreBluetooth

实现了这一点

    let myChar1 = CBMutableCharacteristic(
    type: CBUUID(nsuuid: UUID(uuidString: "characteristicUuid")!), 
    properties: [.read],
    value: payload,
    permissions: [.readable])

Android 也可以吗?还是我的方法不对

android bluetooth-lowenergy android-bluetooth bluetooth-gatt
1个回答
0
投票

如果没有更多代码上下文,我不确定答案是否足够,但如果您实现自己的 BluetoothGattServerCallback 子类,则可以将初始值设置为类的属性并使用 BluetoothGattServer.sendResponse 返回它() 当 onCharacteristicReadRequest 被调用时。

https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onCharacteristicReadRequest(android.bluetooth.BluetoothDevice,%20int,%20int,%20android.bluetooth.BluetoothGattCharacteristic)

© www.soinside.com 2019 - 2024. All rights reserved.