对于可能遇到相同问题的任何人,这都是使用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(); } } }};- 创建特征列表
- 在onServicesDiscovered中,使用要读取/写入的特征填充列表
- 创建一个称为requestCharacteristics(gatt)的新方法,并将gatt对象传递给它。将特征添加到列表后,从onServicesDiscovered调用此方法。
- 在requestCharacteristics()方法中,调用gatt.readCharacteristic(chars.get(chars.size()-1));
- 在onCharacteristicRead中,检查列表的大小是否不为零,然后读取特征,删除列表的最后一项,然后再次调用requestCharacteristic()。
- 就这样



