ฉันกำลังพัฒนาแอปบน Samsung ACE3 เพื่อเชื่อมต่ออุปกรณ์ Bluetooth Low Energy เนื่องจาก Samsung ไม่ต้องการให้ ACE3 อัปเกรดเป็น Android 4.3 ฉันจึงต้องใช้ Samsung ble api ในปัจจุบัน การเชื่อมต่อ การอ่านข้อมูล การส่งข้อมูล และรับการแจ้งเตือนจากคุณลักษณะหนึ่งๆ ถือว่าใช้ได้ทั้งหมด แต่เมื่อฉันเปิดใช้งานการแจ้งเตือนสำหรับคุณสมบัติหลายรายการ เฉพาะคุณสมบัติแรกที่เปิดใช้งานเท่านั้นที่สามารถรับการแจ้งเตือนได้ ใครมีปัญหาเดียวกันบ้างไหม? ขอบคุณที่คุณช่วย!
รหัสต่อไปนี้เปิดใช้งานการแจ้งเตือนเกี่ยวกับการเชื่อมต่อ
if (mBluetoothGatt != null && device != null) {
BluetoothGattService pucService = mBluetoothGatt.getService(device, PROFILE_UART_CONTROL_SERVICE);
if (pucService == null) {
showMessage("PUC service not found!");
return;
}
BluetoothGattCharacteristic motion = pucService.getCharacteristic(MOTION_READ);
if (motion == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, motion);
BluetoothGattCharacteristic voltage = pucService.getCharacteristic(VOLTAGE_READ);
if (voltage == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, voltage);
BluetoothGattCharacteristic pressure = pucService.getCharacteristic(PRESSURE_READ);
if (pressure == null) {
showMessage("charateristic not found!");
return;
}
enableNotification(true, pressure);
}
ต่อไปนี้เป็นวิธีการเปิดใช้งานการแจ้งเตือน:
public boolean enableNotification(boolean enable, BluetoothGattCharacteristic characteristic) {
if (mBluetoothGatt == null)
return false;
if (!mBluetoothGatt.setCharacteristicNotification(characteristic, enable))
return false;
BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
if (clientConfig == null)
return false;
if (enable) {
Log.i(TAG,"enable notification");
clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
} else {
Log.i(TAG,"disable notification");
clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
}
return mBluetoothGatt.writeDescriptor(clientConfig);
}