0

我试图从我的小米手环 2 中读取心率值。为此,我尝试使用和调整来自https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt的 BluetoothLeGatt 项目。

到目前为止,我能够调查附近的 BLE 设备。之后我选择了小米手环2,我可以成功列出这台设备的所有服务,包括心率服务。在心率服务中,我找到了我正在寻找的特征心率测量。

我试图从这个特征打印数据但没有成功。即使我做了一些细微的更改,我也无法在日志中看到任何数据。

这是我的 LEScanCallback(稍作改动)

    // Implements callback methods for GATT events that the app cares about.  For example,
    // connection change and services discovered.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.i(TAG, "Attempting to start service discovery:" +
                        mBluetoothGatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }

            BluetoothGattCharacteristic characteristic =
                            gatt.getService(HEART_RATE_SERVICE_UUID)
                            .getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);

            System.out.println("HEART_RATE_MEASUREMENT_CHAR_UUID: "+HEART_RATE_MEASUREMENT_CHAR_UUID);
            /*gatt.setCharacteristicNotification(characteristic, true);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            gatt.writeDescriptor(descriptor); */

            setCharacteristicNotification(characteristic,true);

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }

            System.out.println("onCharacteristicRead: " + Arrays.toString(characteristic.getValue()));

        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
            super.onDescriptorWrite(gatt, descriptor, status);

            System.out.println("onDescriptorWrite");
            BluetoothGattCharacteristic characteristic = gatt.getService(HEART_RATE_SERVICE_UUID)
                    .getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);

            try {
                Thread.sleep(2000);
                } catch (InterruptedException e) {
            e.printStackTrace();
            }
            characteristic.setValue(new byte[]{1, 1});
            //gatt.writeCharacteristic(characteristic);
            boolean success = gatt.readCharacteristic(characteristic);
            System.out.println("success? "+success);
            System.out.println("status: "+status);

        }


        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

            System.out.println("onCharacteristicChanged: " + Arrays.toString(characteristic.getValue()));

        }
    };

我不明白 onCharacteristicRead() 或 onCharacteristicChanged() 是否被调用。我相信这是因为 gatt.readCharacteristic(characteristic) 是 FALSE 并且 onDescriptorWrite() 函数中的状态是 3 (GATT_WRITE_NOT_PERMITTED)。

有人可以帮我解决问题吗?我开始失去所有希望了...

4

0 回答 0