gpt4 book ai didi

Android 作为 BLE 外设

转载 作者:太空狗 更新时间:2023-10-29 14:59:23 27 4
gpt4 key购买 nike

我正在尝试创建一个充当低功耗蓝牙 (BLE) 外围设备的 Android 5 (lollipop) 应用程序。该应用程序在支持 BLE 外设模式的 Nexus 9 上运行。到目前为止,我已经设法为服务做广告并允许另一个 BLE 设备成功连接到它。但是,尝试读取特征值时失败。

我已检查该特性是否具有读取属性并且已设置读取权限。

当使用 LightBlue iOS 应用程序 (https://itunes.apple.com/gb/app/lightblue-bluetooth-low-energy/id557428110?mt=8) 时,我设法发现并连接到我的 Nexus 并看到了特征 uuid,但该值未显示。

如有任何帮助,我们将不胜感激。

最佳答案

首先查看你在外设模式下转换广告的特征数据一般有三种模式

BluetoothGattCharacteristic.PROPERTY_WRITE,      BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PROPERTY_NOTIFY;

并且您可以使用所有模式构建特性

BluetoothGattCharacteristic.PROPERTY_WRITE |BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY;

完成后,在构建特性时在 BluetoothGattServerCallback() 中查找 onCharacteristicWriteRequest()。当中央想要发送数据时,它可以使用 WRITE 模式将数据写入特性,并且您将在外设端触发 onCharacteristicWriteRequest() 回调方法,并且您将在 byte[] 中有数据并确保使用 btGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, null);通过检查回调方法中的 responseNeeded bool 值。以这种方式,数据从中央传输到外围设备。

并将数据从外设发送到中央使用通知charatertististc

BluetoothGattCharacteristic bgc = bluetoothGattService
.getCharacteristic(chartersticUUID);
bgc.setValue(bnd.data);
btGattServer.notifyCharacteristicChanged(centralbluetoothdevice, bgc, false);

.

关于Android 作为 BLE 外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28378129/

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