gpt4 book ai didi

Android BLE 写入 'onCharacteristicWrite' 返回状态 GATT_WRITE_NOT_PERMITTED

转载 作者:行者123 更新时间:2023-11-30 00:07:30 33 4
gpt4 key购买 nike

我一直在尝试创建一个小型 Android BLE 应用程序,将一些字节的数据发送到 BLE 设备(HM-10 模块)。

使用 Play 商店中的现有应用程序,我已经能够测试连接,这似乎有效,但在我自己的应用程序中尝试实现它时,我似乎一直遇到问题。

这是我使用其 MAC 地址连接到 BLE 设备的代码:

// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

BluetoothDevice myDevice = mBluetoothAdapter.getRemoteDevice("00:09:83:20:8D:18");

mBluetoothGatt = myDevice.connectGatt(getApplicationContext(), true, mGattCallback);

并使用 writeCharacteristic 方法发送数据:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
//Now we can start reading/writing characteristics
String TAG = "";
if (status == BluetoothGatt.GATT_SUCCESS)
{
for (BluetoothGattService gattService : gatt.getServices())
{
Log.i(TAG, "onServicesDiscovered: ---------------------");
Log.i(TAG, "onServicesDiscovered: service=" + gattService.getUuid());
for (BluetoothGattCharacteristic characteristic : gattService.getCharacteristics())
{
Log.i(TAG, "onServicesDiscovered: characteristic=" + characteristic.getUuid());

if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
{
Log.w(TAG, "onServicesDiscovered: found LED");

//byte[] b = { '$', 0x05, 0x05, 0x10, '$', 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\r', '\n' };
byte[] b = {0x01};

characteristic.setValue(b); // call this BEFORE(!) you 'write' any stuff to the server
boolean myResult = mBluetoothGatt.writeCharacteristic(characteristic);

Log.i(TAG, "onServicesDiscovered: , write bytes?! " + b);
}
}
}

//broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}

我使用以下代码接收回调:

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if(status != BluetoothGatt.GATT_SUCCESS){
Log.d("onCharacteristicWrite", "Failed write, retrying: " + status);
gatt.writeCharacteristic(characteristic);
}
Log.d("onCharacteristicWrite", ByteArrToHex(characteristic.getValue()));
super.onCharacteristicWrite(gatt, characteristic, status);
}

出于某种原因,回调中的状态变量始终包含状态 3 (GATT_WRITE_NOT_PERMITTED)。我一直在尝试寻找此状态的具体含义,但找不到任何有用的信息。

有谁知道我在这里遗漏了什么?

编辑:我还注意到,对 onCharacteristicWrite 中的特征调用 getPermissions() 会返回 0。

编辑: 好吧,感谢@Emil,我能够更改示例以检查导致此结果的服务 UUID:

if (gattService.getUuid().equals(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb")))
{
for (BluetoothGattCharacteristic characteristic : gattService.getCharacteristics())
{
Log.i(TAG, "onServicesDiscovered: characteristic=" + characteristic.getUuid());

if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0)
{
Log.w(TAG, "onServicesDiscovered: found LED");

//byte[] b = { '$', 0x05, 0x05, 0x10, '$', 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\r', '\n' };
byte[] b = "$\5\5\16Hello world!\r\n".getBytes();
//byte[] b = {0x01};

characteristic.setValue(b); // call this BEFORE(!) you 'write' any stuff to the server
boolean myResult = mBluetoothGatt.writeCharacteristic(characteristic);

Log.i(TAG, "onServicesDiscovered: , write bytes?! " + b);
}
}
}

BLE 设备现在可以正常接收数据了!

最佳答案

这意味着远程设备不允许您对该特性进行写入(尽管它已经在属性中设置了可写位...)

但是您不应该像那样循环遍历服务并只写入您找到的第一个可写特征。而是通过 uuid 找到正确的服务和特征,以确保找到正确的。

关于Android BLE 写入 'onCharacteristicWrite' 返回状态 GATT_WRITE_NOT_PERMITTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731701/

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