【问题标题】:Android read characteristics issueAndroid读取特性问题
【发布时间】:2014-03-17 15:13:42
【问题描述】:

我正在使用来自官方 android 开发者网站的代码,发现阅读特性有困难

当迭代所有 uuid 时,我使用此代码在 DisplayGattServices 函数的 DeviceControlActivity 类中创建 gatt 特征读取调用:

mBluetoothLeService.readCharacteristic(new BluetoothGattCharacteristic(
                    UUID.fromString(uuid),
                    BluetoothGattCharacteristic.PERMISSION_READ,
                    BluetoothGattCharacteristic.PROPERTY_READ));

readCharacteristic 函数 int BluetoothLeService calss 是:

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.readCharacteristic(characteristic);
}

BluetoothLeService 类中的回调也是:

@Override
    public void onCharacteristicRead(BluetoothGatt gatt,
                                     BluetoothGattCharacteristic characteristic,
                                     int status) {

        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }

我在回调处设置了一个断点,但我永远不会停在那里... 可能是许可还是财产?还有什么?... 有人有一个可行的例子吗?

【问题讨论】:

    标签: android bluetooth-lowenergy gatt characteristics


    【解决方案1】:

    这通常不是您阅读特征的方式。您应该遵循的步骤是: - 查找设备 - 连接到设备 - 发现服务 - 从您的服务中选择您想要的特征 - 使用该特征读取值

    您必须首先获得对服务中特征的引用才能读取它,而不仅仅是使用构造函数创建新特征。

    如果您需要进一步澄清,请告诉我。

    【讨论】:

      【解决方案2】:
      BluetoothGattCharacteristic(UUID uuid, int properties, int permissions)
      

      参考:BluetoothGattCharacteristic documentation

      所以:

      mBluetoothLeService.readCharacteristic(new BluetoothGattCharacteristic(
                      UUID.fromString(uuid),
                      BluetoothGattCharacteristic.PROPERTY_READ,
                      BluetoothGattCharacteristic.PERMISSION_READ
                      ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2021-07-30
        • 2020-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多