내 원격 BLE 장치의 특정 특성에서 데이터를 Android 태블릿 Nexus 7로 읽으 려합니다.특성을 읽을 수 없습니다. Android BLE
문제는 해당 특성에 대한 알림을 활성화하여 데이터를 수신 할 수 있다는 것입니다. 전화하지 않고 readCharacteristic
. 그러나 알림을 활성화하지 않고 readCharacteristic
으로 전화를 걸면 특성을 성공적으로 읽을 수 없습니다.
mBluetoothGatt.readCharacteristic(characteristic)
은 false를 반환합니다. 따라서 기능 onCharacteristicRead
이 트리거 된 적이 없습니다. 나는 또한 속성 값 BluetoothGattCharacteristic.PROPERTY_READ
을 확인했습니다. 3030입니다.
여기에 무슨 일이 벌어지고 있는지에 대한 아이디어가 있습니까? 나는 특성을 개별적으로 읽어야한다. 왜냐하면 만약 내가 통보에 기초한 데이터만을 분석한다면, 나는 데이터가 어디에서 시작되는지 알 수 없다. 이것은 내 장치가 매번 12 바이트를 전송하기 때문입니다. 그리고 바이트 배열을 계속해서 보냅니다. 그러나 알림은 한 번에 한 바이트 씩 데이터를 가져옵니다. 그래서 바이트 배열의 시작 바이트인지 알 수 없습니다.
지금 Android에서 제공하는 샘플 코드를 사용하고 있습니다. 콜백에서
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean status = mBluetoothGatt.readCharacteristic(characteristic);
System.out.println("Initialize reading process status:" + status);
}
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
코드는 다음과 같습니다 : 여기
은 조각이다 나는 특성을 읽기의 문서를 읽고@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
System.out.println("In onCharacteristicRead!!!!!!!");
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
System.out.println("Received Data Success!!!!!!");
}
}
,하지만 아무것도 할 수 없습니다. 누구든지 나를 도울 수 있습니까? 고마워요!
안녕하세요, 귀하의 회신에 감사드립니다. 나는 알림을 먼저 활성화하고 특성을 읽으려고했다. 데이터가 장치에서 준비되면 "onCharacteristicChanged"만 호출되고 함수 "onCharacteristicRead"는 여전히 호출되지 않은 것으로 나타납니다. 이거 좀 생각해? 고맙습니다. – Magic
특성을 읽을 수 있는지 확인하기 위해 특성 사용 권한을 얻으려고 시도 할 수도 있습니다. 그렇지 않은 경우 작동하지 않거나 try/catch 블록 사이의 값을 읽으면 예외가 throw되는지 확인할 수 있습니다. 도움이되기를 바랍니다 –
당신은이 설명을 서술자로 작성 했습니까? –