Android蓝牙LE - 读取特性不适用于三星

Anu*_*ool 7 android bluetooth-lowenergy

我试图不断地从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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

在成功发现我呼吁的服务时:

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

    }
Run Code Online (Sandbox Code Playgroud)

这是我的特色阅读回调 -

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);
            }

        }
Run Code Online (Sandbox Code Playgroud)

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

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

我读到,在readCharacteristics()等待onCharacteristicRead执行时,进行顺序调用会导致问题.

JPS*_*JPS 0

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

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