반응형
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// UUID_INIT_NOTI_RECV 수신 설정
// callback method 등록하는 것 처럼
// 이 부분이 반드시 있어야 함
// --------중요 시작--------
if ( (UUID_INIT_NOTI_RECV.equals(characteristic.getUuid())) || (UUID_MSG_RECV.equals(characteristic.getUuid())) ) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.NOTI_DESC));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
// --------중요 끝--------
}
setCharacteristicNotification 의 존재 이유를 설명해준다
callback method 처럼 characteristic이 변화 될 때 onCharacteristicChanged을 작동하게 하려면
위와 같은 설정을 해주어야 한다
characteristic 하위의 descriptor에 등록을 해줘야 하는데
descriptor를 모를 경우 함수를 통해 UUID를 찾아낸뒤 설정해주면 된다
반응형