栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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 == BluetoothGatt.GATT_SUCCESS) { 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. 就这样


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506225.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号