Android如何使用PriorityQueue读取多个BLE特

Android如何使用PriorityQueue读取多个BLE特

本文介绍了Android如何使用PriorityQueue读取多个BLE特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点困在这里,可能需要你的帮助。我想一次阅读几个BLE特征,有些人建议使用PriorityQueue。我已经知道所有的uuids等,只需要一种方法一次读几个。
有谁可以解释它究竟应该是什么样子?或者还有另一种更简单的解决方案?

A bit stuck here, might need your help. I want to read several BLE characteristics at once, some people suggest using PriorityQueue for that. I already know all the uuids, etc. just need a way to read several at once.Could anyone explain how exactly should it look like? Or maybe there is yet another easier solution?

提前致谢,这是我的代码:

Thanks in advance, here is my code:

public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    PriorityQueue<BluetoothGattCharacteristic> queue = new PriorityQueue<BluetoothGattCharacteristic>();

    // When connection state changes
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        List<BluetoothGattService> services = gatt.getServices();
        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        List<UUID> uuidsList;

        UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid();
        UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid();
        UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid();
        UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid();
        //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid();

        uuidsList = new ArrayList<UUID>();

        uuidsList.add(TRANSMISSION_POWER);
        uuidsList.add(BROADCASTING_INTERVAL);
        uuidsList.add(BEACON_NAME);
        uuidsList.add(CONNECTION_MODE);
        //uuidsList.add(SOFT_REBOOT);

        queue.add(rightService.getCharacteristic(uuidsList.get(0)));
        queue.add(rightService.getCharacteristic(uuidsList.get(1)));
        queue.add(rightService.getCharacteristic(uuidsList.get(2)));

    }

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

        Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
        onServicesDiscovered(gatt, 0);

    }

};

更新:

即使放入后也是如此在不同的线程上,它仍然只对一个gatt.readCharacteristic(...)作出反应。如下所示:

even after putting them on different threads it still only reacts to one gatt.readCharacteristic(...). like following:

// Gatt Callback
public static final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    // When connection state changes
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {


        List<BluetoothGattService> services = gatt.getServices();

        /*
        DISPLAY ALL SERVICES AND CHARACTERISTICS

        for (int i = 0; i < services.size(); i++) {
            Log.v(TAG, "SERVICE____: " + services.get(i).getUuid());

            for (int k = 0; k < services.get(i).getCharacteristics().size(); k++) {
                Log.v(TAG, "CHARACTERISTIC____: " + services.get(i).getCharacteristics().get(k).getUuid());
            }

        }
        */

        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        List<UUID> uuidsList;

        UUID TRANSMISSION_POWER = rightService.getCharacteristics().get(4).getUuid();
        UUID BROADCASTING_INTERVAL = rightService.getCharacteristics().get(6).getUuid();
        UUID BEACON_NAME = rightService.getCharacteristics().get(8).getUuid();
        UUID CONNECTION_MODE = rightService.getCharacteristics().get(9).getUuid();
        //UUID SOFT_REBOOT = rightService.getCharacteristics().get(10).getUuid();

        uuidsList = new ArrayList<UUID>();

        uuidsList.add(TRANSMISSION_POWER);
        uuidsList.add(BROADCASTING_INTERVAL);
        uuidsList.add(BEACON_NAME);
        uuidsList.add(CONNECTION_MODE);
        //uuidsList.add(SOFT_REBOOT);


        class powerThread extends Thread{

            UUID uuid;
            BluetoothGatt gatt;
            BluetoothGattService service;
            public powerThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) {
                this.gatt = gatt;
                this.service = service;
                this.uuid = uuid;
            }
            @Override
            public void run() {
                gatt.readCharacteristic(service.getCharacteristic(uuid));
            }
        }
        powerThread pt = new powerThread(TRANSMISSION_POWER, gatt, rightService);
        pt.run();


        class intervalThread extends Thread{

            UUID uuid;
            BluetoothGatt gatt;
            BluetoothGattService service;
            public intervalThread(UUID uuid, BluetoothGatt gatt, BluetoothGattService service) {
                this.gatt = gatt;
                this.service = service;
                this.uuid = uuid;
            }
            @Override
            public void run() {
                gatt.readCharacteristic(service.getCharacteristic(uuid));
            }
        }
        intervalThread it = new intervalThread(BROADCASTING_INTERVAL, gatt, rightService);
        it.run();


    }

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

        Log.v(TAG, "CHARACTERISTIC VALUE___: " + characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));

    }

};


推荐答案

对于可能遇到同样问题的人,这里是使用List<>特征的简单解决方案。

To anyone who might encounter the same problem, here is an easy solution using a List<> of characteristics.

public static final BluetoothGattCallback readGattCallback = new BluetoothGattCallback() {

    List<BluetoothGattCharacteristic> chars = new ArrayList<>();

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.v(TAG, "Connected!");
            broadcastingInterval = 999;
            transmissionPower = 999;
            gatt.discoverServices();
        }
        if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.v(TAG, "Disconnected...");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {

        List<BluetoothGattService> services = gatt.getServices();
        BluetoothGattService rightService = null;

        for (int i = 0; i < services.size(); i++) {
            if (services.get(i).getCharacteristics().size() > 8) {
                rightService = services.get(i);
            }
        }

        chars.add(rightService.getCharacteristics().get(4));
        chars.add(rightService.getCharacteristics().get(6));

        requestCharacteristics(gatt);

    }

    public void requestCharacteristics(BluetoothGatt gatt) {
        gatt.readCharacteristic(chars.get(chars.size()-1));
    }

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

            if (characteristic.getUuid().toString().substring(7, 8).equals("5")) {
                transmissionPower = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                Log.v(TAG, "tPOWER READ");

            } else if (characteristic.getUuid().toString().substring(7,8).equals("7")) {
                broadcastingInterval = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
                Log.v(TAG, "INTERVAL READ");
            }

            chars.remove(chars.get(chars.size() - 1));

            if (chars.size() > 0) {
                requestCharacteristics(gatt);
            } else {
                gatt.disconnect();
            }
        }
    }

};




  1. 创建特征列表

  2. 在onServicesDiscovered中填充具有您想要读/写的特征的列表

  3. 创建一个名为requestCharacteristics(gatt)的新方法并将gatt对象传递给它。在向列表添加特性后,从onServicesDiscovered调用此方法。

  4. 在requestCharacteristics()方法中调用gatt.readCharacteristic(chars.get(chars.size() - 1));

  5. 在onCharacteristicRead中检查列表的大小是否不为零,然后读取您的特征,删除列表的最后一项并再次调用requestCharacteristic()。

  6. 这就是

  1. Create a list of characteristics
  2. In onServicesDiscovered populate the list with characteristics you want to read/write
  3. Create a new method called requestCharacteristics(gatt) and pass the gatt object to it. Call this method from onServicesDiscovered after adding characteristics to the list.
  4. In requestCharacteristics() method call gatt.readCharacteristic(chars.get(chars.size()-1));
  5. In onCharacteristicRead check if size of your list is not zero, then read your characteristic, remove the last item of your list and call requestCharacteristic() again.
  6. That's all

这篇关于Android如何使用PriorityQueue读取多个BLE特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:11