-
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
String value = characteristic.getStringValue(0);
Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString());
}
特性は
private void getDeviceInformation() {
BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE);
BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision;
for (BluetoothGattCharacteristic characteristic : bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics()) {
bluetoothGatt.setCharacteristicNotification(characteristic, true);
bluetoothGatt.readCharacteristic(characteristic);
}
}
Logcatとしてループされています。だから、後:
List<BluetoothGattCharacteristic> characteristics = new ArrayList<>();
boolean isGettingDeviceInformation;
int count = 0;
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
String value = characteristic.getStringValue(0);
Log.e("TAG", "onCharacteristicRead: " + value + " UUID " + characteristic.getUuid().toString());
if(isGettingDeviceInformation) {
if(count < characteristics.size()) {
count++;
bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true);
bluetoothGatt.readCharacteristic(characteristics.get(count));
} else {
isGettingDeviceInformation = false;
}
}
}
private void getDeviceInformation() {
BluetoothGattService deviceInfoService = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE);
BluetoothGattCharacteristic deviceSerialNumber, deviceHardwareRevision, deviceSoftwareRevision;
characteristics = bluetoothGatt.getService(UUIDs.DEVICE_INFORMATION_SERVICE).getCharacteristics();
bluetoothGatt.setCharacteristicNotification(characteristics.get(count), true);
bluetoothGatt.readCharacteristic(characteristics.get(count));
}
UPDATE
:
bluetoothGatt.readCharacteristic(characteristic);
あなたはあなたが記述のような何かをしたいのであれば、そのようなことをするコールバック
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
を待つ必要があります
あなたはブルートゥースであなたの操作がなくなることを忘れないでください。待っているコールバックのために、ループでスタックできます。だから、それを避けるためには、動作時にタイムアウトをする必要があるので、コールバックを取得しないと、現在の操作をキャンセルして、さらに進むことができます。
ループでは機能しません。最初の読み込みのコールバックの後に2番目の文字を読み込もうとします。 –
私はそれらを別々に定義しようとしました。どちらもうまくいきません。彼らは同じ機能で書かれていましたが。 –
0から始まるカウンタ変数をとり、最初の文字を読み込みます。最初の文字列のコールバックの後、1つ目のカウンタを増やして2番目の文字列を読み込むなどします。 –