gpt4 book ai didi

java - (java,蓝牙低功耗,BLE)如何通过BLE从Android设备获取数据

转载 作者:行者123 更新时间:2023-12-02 09:43:55 25 4
gpt4 key购买 nike

enter image description here

我是 BLE(蓝牙低能耗)的初学者。我想通过 BLE 从 Android 设备获取数据。

我已经在 Google 文档中阅读了有关特征的内容。我也已经在谷歌上搜索过。

我的设备没有响应我的请求字节代码。我认为这是因为我设置了错误的特征。因为我认为我没有完全理解特征。

有人能帮我设置正确的特征吗?

这是自定义 Uuid(最好在顶部看到添加的图像。)

CUSTOM SERVICE
0783b03e-8535-b5a0-7140-a304d2495cb7

NOTIFY Uuid : 0783B03E-8535-B5A0-7140-A304D2495CB8
Read Uuid : 00002a19-0000-1000-8000-00805f9b34fb
Write Uuid : 0783b03e-8535-b5a0-7140-a304d2495cba

这是我设置的 Uuid

    public final UUID serviceUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb7");
public final UUID notifyUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb8");
public final UUID readUuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
public final UUID writeUuid = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cba");

这是我的代码

BluetoothHandler.java
 targetGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);
BluetoothGattCharacteristic readGattCharacteristic = targetGattService.getCharacteristic(Define.GetInstance().notifyUuid);

if (readGattCharacteristic != null) {
mBleService.setCharacteristicNotification(readGattCharacteristic, true);

} else {
callInterface();
return;
}
(BluetoothService.java)
    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString(Define.GetInstance().readUuid.toString()), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
characteristic.addDescriptor(gD);
if (Define.GetInstance().notifyUuid.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(Define.GetInstance().readUuid.toString()));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

}
}

最佳答案

关于java - (java,蓝牙低功耗,BLE)如何通过BLE从Android设备获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831324/

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