Android BLE写入"onCharacteristicWrite"返回状态GATT_WRITE_NOT_PERMITTED [英] Android BLE write 'onCharacteristicWrite' returns status GATT_WRITE_NOT_PERMITTED

查看:97
本文介绍了Android BLE写入"onCharacteristicWrite"返回状态GATT_WRITE_NOT_PERMITTED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

使用Play商店中的现有应用程序,我已经能够测试连接,并且似乎可以正常工作,但是在尝试在自己的应用程序中实现连接时,似乎总是遇到问题.

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

 //初始化蓝牙适配器.最终的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方法发送数据:

  @Overridepublic void onServicesDiscovered(BluetoothGatt gatt,状态为int){//现在我们可以开始读/写特征字符串TAG =";如果(状态== BluetoothGatt.GATT_SUCCESS){用于(BluetoothGattService gattService:gatt.getServices()){Log.i(TAG,"onServicesDiscovered:---------------------");Log.i(TAG,"onServicesDiscovered:service =" + gattService.getUuid());用于(BluetoothGattCharacteristic特征:gattService.getCharacteristics()){Log.i(TAG,"onServicesDiscovered:特征= +特征.getUuid());如果((characteristic.getProperties()& BluetoothGattCharacteristic.PROPERTY_WRITE)> 0){Log.w(TAG,"onServicesDiscovered:找到了LED");//byte [] b = {'$',0x05,0x05,0x10,'$','H','e','l','l','o',','W','o','r','l','d','!','\ r','\ n'};byte [] b = {0x01};feature.setValue(b);//将其称为BEFORE(!),您可以将任何内容写入"服务器布尔值myResult = mBluetoothGatt.writeCharacteristic(characteristic);Log.i(TAG,"onServicesDiscovered:,写字节?!" + b);}}}//broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);} 别的 {Log.w(TAG,"onServicesDiscovered已收到:" +状态);}} 

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

  @Overridepublic void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic特征,int状态){if(状态!= BluetoothGatt.GATT_SUCCESS){Log.d("onCharacteristicWrite",写入失败,重试:" + status);gatt.writeCharacteristic(characteristic);}Log.d("onCharacteristicWrite",ByteArrToHex(characteristic.getValue()));super.onCharacteristicWrite(gatt,特征,状态);} 

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

有人知道我在这里想念什么吗?

编辑:我还注意到在onCharacteristicWrite中的特征上调用getPermissions()会返回0.

好,因此,感谢@Emil,我能够更改示例以检查导致此问题的服务UUID:

  if(gattService.getUuid().equals(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb"))){用于(BluetoothGattCharacteristic特征:gattService.getCharacteristics()){Log.i(TAG,"onServicesDiscovered:特征= +特征.getUuid());如果((characteristic.getProperties()& BluetoothGattCharacteristic.PROPERTY_WRITE)> 0){Log.w(TAG,"onServicesDiscovered:找到了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};feature.setValue(b);//将此名称称为BEFORE(!),即可将所有内容写入"服务器布尔值myResult = mBluetoothGatt.writeCharacteristic(characteristic);Log.i(TAG,"onServicesDiscovered:,写字节?!" + b);}}} 

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

解决方案

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

但是您不应该像这样遍历这些服务,而只是写出找到的第一个可写特征.而是通过uuid找到正确的服务和特征,以确保找到正确的服务和特征.

I've been trying to create a small Android BLE app that sends some bytes of data to a BLE device (an HM-10 module).

Using existing applications on the Play Store I've been able to test the connection and this seems to be working but I seem to keep running into problems when trying to implement it in my own app.

This is the code I'm using to connect to the BLE device using its MAC-address:

// 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);

and sending data using the writeCharacteristic method:

@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);
        }
    }

I use the following code to receive the callback:

@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);
    }

For some reason the status variable in the callback always contains status 3 (GATT_WRITE_NOT_PERMITTED). I've been trying to look for the specific meaning of this status but I wasn't able to find anything useful.

Does anyone have an idea what I'm missing here?

EDIT: I also noticed that calling getPermissions() on the characteristic in onCharacteristicWrite returns 0.

EDIT: Alright, so thanks to @Emil I was able to change the example to check for the service UUID which resulted into this:

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);
                        }
                    }
                }

The BLE device now receives the data properly!

解决方案

It means that the remote device doesn't permit you to write to that characteristic (although it has set the writable bit in the properties...)

But you shouldn't loop through the services like that and just write to the first writable characteristic you find. Instead find the correct service and characteristic by uuid to make sure you find the correct one.

这篇关于Android BLE写入"onCharacteristicWrite"返回状态GATT_WRITE_NOT_PERMITTED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆