(java, bluetooth-low energy, ble) 如何通过 BLE 从 android 设备获取数据
Posted
技术标签:
【中文标题】(java, bluetooth-low energy, ble) 如何通过 BLE 从 android 设备获取数据【英文标题】:(java, bluetooth-lowenergy, ble) how to get data from android device via BLE 【发布时间】:2019-07-01 07:09:34 【问题描述】:我是 BLE(蓝牙低功耗)的初学者。我想通过 BLE 从安卓设备获取数据。
我已经阅读了 Google 文档中的特征。 我也已经在 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);
【问题讨论】:
【参考方案1】:我通过参考这个网站解决了这个问题。 https://medium.com/@avigezerit/bluetooth-low-energy-on-android-22bc7310387a
【讨论】:
以上是关于(java, bluetooth-low energy, ble) 如何通过 BLE 从 android 设备获取数据的主要内容,如果未能解决你的问题,请参考以下文章