【问题标题】:Android Bluetooth LE - Read Characteristics not working on SamsungAndroid 蓝牙 LE - 读取特性不适用于三星
【发布时间】:2016-04-15 22:44:11
【问题描述】:

我正在尝试连续读取 BLE 设备的特征。

我在我的服务类中创建了一个 Runnable:

private class BackgroundRunnableForRead implements Runnable
    {


        private volatile  boolean isRunning = true ;
        @Override
        public void run() {
            try {
            BluetoothLeService.this.backgroundRunID = Thread.currentThread().getId();
            while( isRunning) {

                    List<BluetoothGattService> gattServices = BluetoothLeService.this.getSupportedGattServices();

                    if (gattServices != null && gattServices.size() > 0) {
                        BluetoothGattCharacteristic characteristic = getCharacteristic(gattServices);

                        if (characteristic != null && (characteristic.getProperties() & 2) > 0) {
                            BluetoothLeService.this.readCharacteristic(characteristic);
                        }
                    }
                }
            }
            catch(Exception e)
            {
                isRunning= false;
                e.printStackTrace();
            }

        }

        public void kill()
        {
            this.isRunning = false;
        }
    }

成功发现我正在调用的服务:

public void startReadingCharacteristics()
    {
        System.out.println("BluetoothLeService.startReadingCharacteristics");
        this.mBackgroundRunnable = new BackgroundRunnableForRead();
        mReadThread =  new Thread(mBackgroundRunnable);
        mReadThread.start();

    }

这是我的特征读取回调 -

public void  onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            System.out.println("BluetoothLeService.onCharacteristicRead" + status);
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }

        }

该应用程序在 Nexus 5、Nexus 4 和 Motorola G 上运行良好。

当我在三星 S6 上运行此代码时,它不起作用,onCharacteristicRead() 未被调用。

我了解到,对 readCharacteristics() 进行顺序调用可能会导致问题,因为它会等待 onCharacteristicRead 执行。

【问题讨论】:

  • 什么是操作系统版本?
  • 操作系统版本 - Android 5.1.1。
  • 请格式化您的代码

标签: android bluetooth-lowenergy


【解决方案1】:

建议一次只执行一个 gatt 命令,因为commands are not stacked。因此,您必须实现某种机制,在获得当前读取回调后调用下一次读取。

请记住,gatt 回调可以来自不同的线程,但是如果您将读取的值保存在回调中,然后从那里触发下一次读取,这应该没有问题。

【讨论】:

  • 我尝试仅在触发 onCharacteristicRead 回调时才开始读取特征。虽然不工作,你认为如果我在某处做错了什么?
  • 好吧,不先执行读特征命令,为什么要触发onCharacteristicRead回调呢?
  • 首先我在发现服务后立即执行读取特征,然后第一次调用 oncharacteristicsRead。之后我在 onCharacteristicsRead 中调用读取特征它不起作用。
  • 您尝试使用getCharacteristic 获取特征,但您将gattServices 作为参数传递。您应该从设备上运行的服务中选择正确的特征。 gattServices 应该是提供服务的列表。
  • 它适用于所有其他设备 - Moto G、HTC、LG nexus 4 和 5、华为 P7 和 P8 等。所以我知道逻辑是正确的。不幸的是,我无法找出为什么它不适用于三星 S5 neo、三星 note 4 等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2016-07-09
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多