Android BLE-外围设备| onCharacteristicRead返回错误的值或其一部分(但重复) [英] Android BLE - Peripheral | onCharacteristicRead return wrong value or part of it (but repeated)

查看:134
本文介绍了Android BLE-外围设备| onCharacteristicRead返回错误的值或其一部分(但重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题不知所措.

i'm losing my mind with this issue.

事实是,一个发布字符串值的Android设备:"78d89537-4309-4728-87f6-3ab2bbe231d8"(36个字节).我正在使用定义为

The fact is, one android device which is advertising a string value: "78d89537-4309-4728-87f6-3ab2bbe231d8" (36 bytes). I'm using a characteristic defined as

 anonIdCharacteristic = new BluetoothGattCharacteristic(TippeeBluetoothManager.UUID_READ_CHARACTERISTIC,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_BROADCAST,
            BluetoothGattCharacteristic.PERMISSION_READ  );
    anonIdCharacteristic.setValue(idToAdvertise);

您可以看到我在阅读"模式下投放广告,没有通知.

as you can see i'm advertising in "READ" mode, not notify.

当另一个Android设备连接并尝试读取特征时,onCharacteristicRead方法称为,但是传递的值是错误的.更具体地说是:

When another android device connect and try to read the characteristic, the onCharacteristicRead method is called but the value passed is wrong. More specifically is:

"78d89537-4309-4728-87f678d89537-4309-4728-87f678d89537-4309-4728-87f6 ..."(600字节)

"78d89537-4309-4728-87f678d89537-4309-4728-87f678d89537-4309-4728-87f6..." (600 bytes)

这是预期值的一部分,但会重复.

which is part of the value expected, but repeated.

如果我将自己置于调试服务器端",则会看到发送的字节数正确.在调试客户端"字节为600

If i put myself on debug "server side" is see that the number of bytes sent are correct. On debug "client side" the byte are 600

我在做什么错了?

预先感谢

----编辑---

---- EDIT ---

我发现了更多信息.

onCharacteristicReadRequest以新月形偏移量反复调用,这导致了脏"缓冲区,现在我以这种方式进行响应:

onCharacteristicReadRequest is called repeatedly with crescent offset that is causing the "dirty" buffer now i'm responding this way:

if (BluetoothManager.UUID_READ_CHARACTERISTIC.equals(characteristic.getUuid())) { mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, getStoredValue()); return; }

使用偏移值.尚无法正常工作,但这是什么.

using offset values. Not working yet but it's something.

我想知道是什么告诉应用程序响应多长时间.

I wonder what is telling the app how long is response..

推荐答案

好的,我明白了,所以我会留下我的答复来帮助其他人解决我的情况.

Ok, i got it so i'll leave my response to help someone else will be in my situation.

正确的解决方法是

@Override
        public void onCharacteristicReadRequest(BluetoothDevice device,
                                                int requestId,
                                                int offset,
                                                BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
            Log.i(TAG, "onCharacteristicReadRequest " + characteristic.getUuid().toString());

            byte[] fullValue = getStoredValue();

            //check
            if (offset > fullValue.length) {
                mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, new byte[]{0} );
                return;

            }


            int size = fullValue.length - offset;
            byte[] response = new byte[size];

            for (int i = offset; i < fullValue.length; i++) {
                response[i - offset] = fullValue[i];
            }



            if (MYBluetoothManager.UUID_READ_CHARACTERISTIC.equals(characteristic.getUuid())) {
                mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, response);
                return;
            }



            mGattServer.sendResponse(device,
                    requestId,
                    BluetoothGatt.GATT_FAILURE,
                    0,
                    null);
        }

    };

以偏移量重复调用该回调,这一点很明显.不清楚的是,我应该用一个数组来响应,该数组包含从该偏移量开始的所有数据.

The callback is called repeatedly with an offset, and this was clear. What was not clear it's i am supposed to respond with an array that contains all the data starting from that offset.

所以我首先准备一个包含所有数据的数组.如果请求的偏移量超出了数据的长度,我将返回一个0字节的数组.

So i start with preparing an array with all the data. If the offset requested exceed the length of the data, i just return an array of 0 byte.

如果不是这样,那么我将从回调请求的偏移量开始准备原始数组的一部分,直到获得一些信息为止.因此,如果数组包含大量信息,那么在第二,第三次回调中,我知道从哪里开始返回数据就不重要了.

If it is not so, i prepare a portion of the original array starting from the offset requested by the callback till i have some information. So is not important if the array contains to much informations, on the second, third callback i know where to start to return the data.

对不起,如果不清楚,但是启用日志记录,您将理解我的意思.

sorry if it's not clear, but enable logging and you will understand what i mean.

祝大家好运

这篇关于Android BLE-外围设备| onCharacteristicRead返回错误的值或其一部分(但重复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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