내 앱에서 BLE 장치로부터 데이터를 받으려고합니다. 나는 내 애플 리케이션과 성공적으로 BLE 장치를 연결할 수 있고 나는 BLE 장치에 의해 제공되는 서비스를 찾을 수있다.onCharacteristicChanged가 Android BLE GATT 서비스에서 호출되지 않습니다.
특성으로 데이터를 쓸 수 있고 특정 서비스에 대한 알림을 성공적으로 사용하고 그 결과를 TRUE으로 반환 할 수 있습니다. 문제가 이후입니다. gatt.writeCharacteristic (특성) 성공적으로 onCharacteristicChanged 재정의 메소드를 호출해야합니다. 그러나 그 방법을 부르지 않습니다. 그 방법에서 나온 만 BLE 장치로부터 데이터를 수신 할 수 있습니다.
나는
참고 URL
이하 이어 : 를, 내가 GATT 연결을 설정 호출하고 서비스를 사용하여.private class BleGattCallback extends BluetoothGattCallback {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.d(TAG, "BluetoothProfile.STATE_DISCONNECTED");
gatt.close();
break;
default:
break;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d(TAG, "onServicesDiscovered");
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(READING_UUID);
if (characteristic != null) {
Log.d(TAG, " Subscribe Characteristic Notification UUID : "+characteristic.getUuid());
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
boolean success = gatt.writeDescriptor(descriptor);
Log.d(TAG, "writeDescriptor Status : " + success);
}
}
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
Toast.makeText(mContext,"onCharacteristicChanged",Toast.LENGTH_LONG).show();
// read Value
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(INDEX_UUID);
Log.d(TAG, "onDescriptorWrite success UUID for "+characteristic.getUuid());
if (characteristic != null) {
characteristic.setValue(new byte[] {0x03, 0x00});
gatt.writeCharacteristic(characteristic);
}
}
}
}
}
하지만 characterstics (READING_UUID)에 대한 알림을 설정 한 다음 문자 (INDEX_UUID)에만 쓰고 있습니다. –
쓰기가 완료 될 때까지 대기하려면 onCharacteristicWrite를 재정의해야합니다. – Emil