gpt4 book ai didi

Android BLE 特性 setValue 无法写入正确的数据

转载 作者:太空狗 更新时间:2023-10-29 14:42:17 26 4
gpt4 key购买 nike

我正在尝试开发一个 Android 应用程序,它使用 BLE 连接到我基于 NRF51822 的系统。目的是将 3 字节值 (RGB) 写入我的自定义特征。

Android 是 GATT 客户端,基于 NRF51 的设备是 GATT 服务器。

我能够成功地建立 ble 连接并发现我的特征。

但是数据发送部分(setValue)给我带来了麻烦。无论我写什么 3 个字节,我在 NRF51 端得到相同的常量数据

下面是我的相关代码(Android)

 @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d("BLENotifier", "BLE GAT : SERVICES DISCOVERED");
for(BluetoothGattService gs: gatt.getServices()) {
Log.d("BLENotifier", "SERVICE = " + gs.getUuid().toString());
}
//SELECT MY CHARACTERSTIC
ble_my_characterstic = gatt.getService(ble_service_uuid).getCharacteristic(ble_characterstic_uuid);
Log.d("BLENotifier", "BLE SELECTED CHARACTERSTIC " + ble_my_characterstic.getUuid().toString());
ble_connected = true;
}

public void writedata(String data){
//WRITE DATA TO MY CHARACTERSTIC
if(ble_my_characterstic != null && ble_connected == true){

my_gatt_handle.executeReliableWrite();
//ble_my_characterstic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
ble_my_characterstic.setValue(hexStringToByteArray(data));
my_gatt_handle.writeCharacteristic(ble_my_characterstic);

Log.d("BLENotifier", "BLE WRITE DATA " + data);
}

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
Log.d("BLENotifier", "hexStringToByteArray " + Integer.toString((int)data[0]) + " " + Integer.toString((int)data[1]) + " " + Integer.toString((int)data[2]));
return data;
}

我调用 writeData 方法作为 ble_handle.writedata("0000FF")

这是我在 NRF51 方面得到的

R = 4 | G = 239 | B= 1

谢谢

最佳答案

I did like this.Not exact answer but may help you.

    @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
List<BluetoothGattService> services = gatt.getServices();
Log.i("onServicesDiscovered", services.toString());
keepConnect=services.get(3).getCharacteristics().get(0);
if(keepConnect!=null){
writeCharacteristic(gatt,keepConnect);
}
}

private void writeCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
{

byte[] byteData = hexToBytes("6fd362e40ebcd0945bf58dc4");
byte[] writeData = new byte[byteData.length];
for (int i = 0; i < byteData.length; i++) {
writeData[i] = byteData[i];
}
characteristic.setValue(writeData);
gatt.writeCharacteristic(characteristic);

}

public static byte[] hexToBytes(String hexRepresentation) {
int len = hexRepresentation.length();
byte[] data = new byte[len / 2];

for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexRepresentation.charAt(i), 16) << 4)
+ Character.digit(hexRepresentation.charAt(i + 1), 16));
}

return data;
}

关于Android BLE 特性 setValue 无法写入正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46023939/

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