【问题标题】:Continuesly reading values for BLE in Android在 Android 中连续读取 BLE 的值
【发布时间】:2016-06-16 01:58:00
【问题描述】:

我正在尝试从 Android 设备与 BLE 交互并读取一些传感器值,我遵循 API 级别 > 21。我设法扫描设备并连接到它。第一个挑战是一次性读取所有特征值,有人建议使用优先级队列来做到这一点,我也很聪明地实现了它,它工作正常。

BluetoothGattCallback gattCallBack = new BluetoothGattCallback() {
        @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.d(TAG, "STATE_CONNECTED");
                bluetoothGatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.d(TAG, "STATE_DISCONNECTED");
                break;
            default:
                Log.d(TAG, "STATE_OTHER");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        final List<BluetoothGattService> services = gatt.getServices();
        characteristics = new ArrayList<BluetoothGattCharacteristic>();
        characteristics.add(services.get(1).getCharacteristics().get(0));
        characteristics.add(services.get(1).getCharacteristics().get(1));
        characteristics.add(services.get(1).getCharacteristics().get(2));

        characteristics.add(services.get(2).getCharacteristics().get(0));
        characteristics.add(services.get(2).getCharacteristics().get(1));
        characteristics.add(services.get(2).getCharacteristics().get(2));

        characteristics.add(services.get(3).getCharacteristics().get(0));
        characteristics.add(services.get(3).getCharacteristics().get(1));
        characteristics.add(services.get(3).getCharacteristics().get(2));
        requesReadCharacteristics(gatt);
    }

    public void requesReadCharacteristics(BluetoothGatt gatt) {
        gatt.readCharacteristic(characteristics.get(characteristics.size() - 1));
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == 0 ) {
            Log.d(TAG, "DeviceNameFetchFromDevice: " + characteristic.getValue());
            characteristics.remove(characteristics.get(characteristics.size() - 1));

            if (characteristics.size() > 0) {
                requesReadCharacteristics(gatt);
            } else {
                gatt.disconnect();
            }
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
    }
};

现在我需要通过 BLE 设备持续监控以读取传感器值(比如每隔 10 秒),所以我需要重复读取特征值并获取它们的值。但是 ble 不允许在第一次调用后调用“readCharacteristic”。

现在我已经实现了它,定期调用 connectDevice(BluetoothDevice),这就像断开与 BLE 设备的现有连接并再次连接。有什么方法可以让我们反复调用 readCharacteristic 连接到设备。根据我的理解,断开和连接设备并不是那么有效。请告诉我,继续读取设备的正确方法是什么,或者如果有这样的例子,请分享

https://github.com/sandeeptengale/androidble/

【问题讨论】:

  • 是否有错误或“ble 不允许在第一次调用后调用 readCharacterisitc”是什么意思?

标签: android android-bluetooth bluetooth-lowenergy


【解决方案1】:

首先,你应该打开通知:

mBluetoothLeService.setCharacteristicNotification(characteristic, true);

然后,当数据发生变化时,您可以在此方法中读取值:

onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)

【讨论】:

  • 谢谢@zkywaklker,但它会在特征值发生更改时通知。我需要定期读取每个特征而不断开连接
  • 您是否获得了有关如何定期读取每个特征的解决方案?
  • 通知时间取决于外围设备。如果你也在实现外围设备,你可以让它随时发送通知。外围设备没有协议定义的行为,使它们仅在数据更改时才发送通知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多