BLE 장치에 대한 일련의 특성 읽기를 구현 중입니다. readCharacteristic()
은 비동기 적으로 실행되기 때문에 다른 "읽기"호출을하기 전에 완료해야 할 때까지 기다려야하므로 wait()
에 대한 잠금을 사용하고 다시 'onCharacteristicRead()
에 I notify()
잠금을 사용합니다.Android BLE : onCharacteristicRead()가 스레드에 의해 차단 된 것으로 나타납니다.
readCharacteristic()
을 호출 한 후 I wait()
을 호출하면 onCharacteristicRead()
으로 전화를받지 않습니다. wait()
이 없다면 onCharacteristicRead()
으로 전화를 걸면 올바른 값이보고됩니다. 단순히 위의 while()
문을 제거하면
private void doRead() {
//....internal accounting stuff up here....
characteristic = mGatt.getService(mCurrServiceUUID).getCharacteristic(mCurrCharacteristicUUID);
isReading = mGatt.readCharacteristic(characteristic);
showToast("Is reading in progress? " + isReading);
showToast("On thread: " + Thread.currentThread().getName());
// Wait for read to complete before continuing.
while (isReading) {
synchronized (readLock) {
try {
readLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
showToast("onCharacteristicRead()");
showToast("On thread: " + Thread.currentThread().getName());
byte[] value = characteristic.getValue();
StringBuilder sb = new StringBuilder();
for (byte b : value) {
sb.append(String.format("%02X", b));
}
showToast("Read characteristic value: " + sb.toString());
synchronized (readLock) {
isReading = false;
readLock.notifyAll();
}
}
, 나는 성공적으로 읽기 콜백을 얻을 :
여기 onCharacteristicRead()
에 콜백을 차단하는 것 관련 코드입니다. 물론, 그로 인해 추가 읽기를 기다리는 것을 막을 수 있으므로 기다리지 않고 앞으로 나아갈 수 없습니다.
readCharacteristic()
이 비동기 적이기 때문에 호출하는 스레드를 실행하면 실제로 읽는 기능 또는 콜백을 호출 할 수있는 기능과 관련이 있습니다.
좀 더 혼란스럽게 만들기 위해 readCharacteristic()
을 호출 할 때뿐만 아니라 onCharacteristicRead()
을 호출 할 때 스레드를 식별하는 축하를 보여줍니다. 이 두 스레드는 서로 다른 이름을 가지고 있습니다. 어떤 이유로 호출 스레드에서 콜백이 호출 된 것일 수도 있지만 그 경우는 아닌 것으로 보입니다. 그럼 스레딩으로 무엇이 진행되고 있습니까?
를 추가하여이 문제를 해결했다? 나는 또한 비슷한 문제에 직면하고있다. 미리 감사드립니다. – Anukool
'mBackgroundHandler'는 어떻게 초기화됩니까? – Felix
똑같은 것으로 나타났습니다!. writeCharacteristic()에서 디버깅하고 깨고 나면 항상 작동하는 것처럼 보입니다. 나는 이것이 동기화 문제라고 생각한다. – aclokay