Android 如何使用 PriorityQueue 读取多个 BLE 特征
Android how to read multiple BLE characteristics with a PriorityQueue
有点卡在这里,可能需要你的帮助。我想一次读取多个 BLE 特性,有些人建议为此使用 PriorityQueue。我已经知道所有的 uuid 等。只需要一种方法来一次读取多个。
谁能解释一下它到底应该是什么样子?或者也许还有另一种更简单的解决方案?
提前致谢,这是我的代码:
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(...) 做出反应。如下所示:
// 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));
}
};
对于可能遇到相同问题的任何人,这里有一个使用特性列表<>的简单解决方案。
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();
}
}
}
};
- 创建特征列表
- 在 onServicesDiscovered 中用您想要的特征填充列表 read/write
- 创建一个名为 requestCharacteristics(gatt) 的新方法并将 gatt 对象传递给它。将特征添加到列表后,从 onServicesDiscovered 调用此方法。
- 在requestCharacteristics()方法中调用gatt.readCharacteristic(chars.get(chars.size()-1));
- 在 onCharacteristicRead 中检查您的列表大小是否不为零,然后读取您的特征,删除列表的最后一项并再次调用 requestCharacteristic()。
- 就这些了
非常感谢您的 post,Robert K。我希望我的应用程序能够自行读取所有特征值,并在 ExpandableList 而不是 UUID 中显示特征值。罗伯特 K 的代码对我有用,但我遇到了显示值的偏移。所以我做了一些小改动,想分享一下: 1.
这是我修改onServicesDiscovered
和OnCharacteristicRead
的方式:
public List<BluetoothGattCharacteristic> chars = new ArrayList<>();
public void requestCharacteristics(BluetoothGatt gatt) {
gatt.readCharacteristic(chars.get(chars.size() - 1));
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> services = gatt.getServices();
for(BluetoothGattService service:services){
chars.addAll(service.getCharacteristics());
}
requestCharacteristics(gatt);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
chars.remove(chars.get(chars.size() - 1));
if (chars.size() > 0) {
requestCharacteristics(gatt);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
} else {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
}
}
}
引入的特性List每次断线后清空
我引入了一个列表来保存特征值和一个整数显示我在哪个索引(每次断开连接后这些都需要清空/设置为零)。我修改了displayData
方法,在每个characteristicRead
:
之后调用
public
public List<String> mCharValues = new ArrayList<>();
public int idx_charValues = 0;
private void displayData(String data) {
if (data != null) { mCharValues.add(0, data); }
else { mCharValues.add(0, "no Data"); }
}
- 我希望在发现服务后立即将特征值显示在 ExpandableList 上。 displayGattServices 方法用服务和特征的名称以及 UUID 填充 ExpandableList。
我没有填UUID,而是填mCharValues对应的元素
currentCharaData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, mCharValues.get(idx_charValues));
idx_charValues = idx_charValues+1;
gattCharacteristicGroupData.add(currentCharaData);
- 最后,我把
ExpandableListView.OnChildClickListener
的内容注释掉了。
除了我在这里介绍的内容外,其余代码几乎没有变化。
有点卡在这里,可能需要你的帮助。我想一次读取多个 BLE 特性,有些人建议为此使用 PriorityQueue。我已经知道所有的 uuid 等。只需要一种方法来一次读取多个。 谁能解释一下它到底应该是什么样子?或者也许还有另一种更简单的解决方案?
提前致谢,这是我的代码:
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(...) 做出反应。如下所示:
// 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));
}
};
对于可能遇到相同问题的任何人,这里有一个使用特性列表<>的简单解决方案。
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();
}
}
}
};
- 创建特征列表
- 在 onServicesDiscovered 中用您想要的特征填充列表 read/write
- 创建一个名为 requestCharacteristics(gatt) 的新方法并将 gatt 对象传递给它。将特征添加到列表后,从 onServicesDiscovered 调用此方法。
- 在requestCharacteristics()方法中调用gatt.readCharacteristic(chars.get(chars.size()-1));
- 在 onCharacteristicRead 中检查您的列表大小是否不为零,然后读取您的特征,删除列表的最后一项并再次调用 requestCharacteristic()。
- 就这些了
非常感谢您的 post,Robert K。我希望我的应用程序能够自行读取所有特征值,并在 ExpandableList 而不是 UUID 中显示特征值。罗伯特 K 的代码对我有用,但我遇到了显示值的偏移。所以我做了一些小改动,想分享一下: 1.
这是我修改
onServicesDiscovered
和OnCharacteristicRead
的方式:public List<BluetoothGattCharacteristic> chars = new ArrayList<>(); public void requestCharacteristics(BluetoothGatt gatt) { gatt.readCharacteristic(chars.get(chars.size() - 1)); } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { List<BluetoothGattService> services = gatt.getServices(); for(BluetoothGattService service:services){ chars.addAll(service.getCharacteristics()); } requestCharacteristics(gatt); } else { Log.w(TAG, "onServicesDiscovered received: " + status); } } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { chars.remove(chars.get(chars.size() - 1)); if (chars.size() > 0) { requestCharacteristics(gatt); broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); } else { broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); } } }
引入的特性List每次断线后清空
我引入了一个列表来保存特征值和一个整数显示我在哪个索引(每次断开连接后这些都需要清空/设置为零)。我修改了
displayData
方法,在每个characteristicRead
: 之后调用
public
public List<String> mCharValues = new ArrayList<>();
public int idx_charValues = 0;
private void displayData(String data) {
if (data != null) { mCharValues.add(0, data); }
else { mCharValues.add(0, "no Data"); }
}
- 我希望在发现服务后立即将特征值显示在 ExpandableList 上。 displayGattServices 方法用服务和特征的名称以及 UUID 填充 ExpandableList。
我没有填UUID,而是填mCharValues对应的元素
currentCharaData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownCharaString));
currentCharaData.put(LIST_UUID, mCharValues.get(idx_charValues));
idx_charValues = idx_charValues+1;
gattCharacteristicGroupData.add(currentCharaData);
- 最后,我把
ExpandableListView.OnChildClickListener
的内容注释掉了。 除了我在这里介绍的内容外,其余代码几乎没有变化。