1

我试图读取存储在BluetoothGattCharacteristic. 以下是我的BluetoothGattCallback代码,大部分操作都在其中发生:

 private final BluetoothGattCallback mGattCallback =
            new BluetoothGattCallback() {
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status,
                                                    int newState) {
                    if (newState == BluetoothProfile.STATE_CONNECTED) {
                        Log.i(TAG, "Connected to GATT server.");
                        Log.i(TAG, "Getting services....");
                        gatt.discoverServices();
                    } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                        Log.i(TAG, "Disconnected from GATT server.");
                    }
                }

                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        BluetoothGattService serv = gatt.getService(Constants.MY_UUID);
                        if (serv != null) {
                            BluetoothGattCharacteristic characteristic = serv.getCharacteristic(Constants.ANOTHER_UUID);
                            boolean res = gatt.readCharacteristic(characteristic);
                            if (res) {
                                Log.d(TAG, "res was true");
                            } else {
                                Log.d(TAG, "res was false");
                            }
                        }
                    } else {
                        Log.w(TAG, "onServicesDiscovered received: " + status);
                    }
                }

                @Override
                public void onCharacteristicRead(BluetoothGatt gatt,
                                                 BluetoothGattCharacteristic characteristic,
                                                 int status) {
                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        Log.d(TAG, "Succesfully read characteristic: " + characteristic.getValue().toString());
                    } else {
                        Log.d(TAG, "Characteristic read not successful");
                    }
                }
            };

因此,要从特征中读取,我正在尝试使用该gatt.readCharacteristic()方法,该方法采用特征并返回一个指示操作成功与否的布尔值。在这里,这个方法正在返回false(打印“res was false”),表明它失败了。

没有打印错误消息。阅读特征的正确方法是什么?为什么这个方法会返回false

编辑: 按照 Inferno 的建议,继续下载所需的源代码,然后在BluetoothGatt readCharacteristic()方法中设置断点:

这是readCharacteristic()android-23..\BluetoothGatt 中的方法

public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) {
        if ((characteristic.getProperties() &
                BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false;

(characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ)正在返回 0,因此false立即返回。现在根据调试器characteristic.getProperties()返回的值是8,而BluetoothGattCharacteristic.PROPERTY_READ静态 int 值是0x02

据我了解,0x08 & 0x02== 0。由于PROPERTY_READ是硬编码的值,我认为从characteristic.getProperties(). 这里可能出了什么问题?

4

2 回答 2

2

阅读特征的正确方法是什么?

首先,您gatt.readCharacteristic(characteristic)onServicesDiscovered()回调内部调用,这没关系。我在您的代码中看不到任何严重的缺陷。

您可以在验证之前onConnectionStateChange()添加额外的检查:newState == BluetoothProfile.STATE_CONNECTED

if (status == BluetoothGatt.GATT_SUCCESS) { ...

为什么这个方法会返回 false?

我检查了BluetoothGatt 这里的 android 源代码,结果发现,返回值false在许多不同的情况下都会返回,如下面的代码所示:

public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if ((characteristic.getProperties() &
            BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false;

    if (VDBG) Log.d(TAG, "readCharacteristic() - uuid: " + characteristic.getUuid());
    if (mService == null || mClientIf == 0) return false;

    BluetoothGattService service = characteristic.getService();
    if (service == null) return false;

    BluetoothDevice device = service.getDevice();
    if (device == null) return false;

    synchronized(mDeviceBusy) {
        if (mDeviceBusy) return false;
        mDeviceBusy = true;
    }

    try {
        mService.readCharacteristic(mClientIf, device.getAddress(),
            characteristic.getInstanceId(), AUTHENTICATION_NONE);
    } catch (RemoteException e) {
        Log.e(TAG,"",e);
        mDeviceBusy = false;
        return false;
    }

    return true;
}

所以我建议你做的是,在 Android Studio 中启动调试器并在readCharacteristic()方法 (in BluetoothGatt.java) 中设置一个断点,并仔细检查代码以查看false返回的位置。这样,您就有希望将问题本地化。除此之外,其他任何事情都将是疯狂的猜测。

当然,您需要下载源才能查看BluetoothGatt.java。但 Android Studio 会在编辑器顶部为您提供一个黄色小栏,询问您是否要下载和安装。只需执行此操作并在下载完成后重新启动 Android Studio。然后你应该可以在BluetoothGatt.java.

更新:

据我了解,0x08 & 0x02 == 0。由于 PROPERTY_READ 是硬编码的值,我认为从characteristic.getProperties() 返回的值有问题。这里可能出了什么问题?

根据 BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 533,0x8返回的值characteristic.getProperties()意味着您的特征具有只写权限。所有阅读尝试都失败并不奇怪。换句话说:您的蓝牙设备不允许您读取该特定特征。

引用规范:

特征属性位域决定了如何使用特征值,或者如何访问特征描述符(见第 3.3.3 节)。

于 2016-09-28T16:24:29.300 回答
0

我试图从带有 BLE 芯片的牛刷刮刀中读取数据。它在 BLE 模块上处于读取特性之下。数据以十六进制返回,即 BRUSH_OFF 为 0x00,BRUSH_ON 为 0x01

我试图在我的 android 应用程序中读取这些数据,但它一直显示为空白。

问题是 0x00 = NULL in ascii 和 0x01 = SOH ascii 它不能显示在屏幕上。

0x30 = 0 in ascii 0x31 = 1 in ascii

也许您有转义字符以十六进制返回,并且无法读取。

我花了几个月的时间试图弄清楚为什么我不能读回这些值。希望这可以帮助你。

于 2017-02-15T14:47:07.370 回答