I'm trying to capture events from a BLE HID, and I've been getting this error when running gatt.setCharacteristicNotification(characteristic, true);
inside onServicesDiscovered
, which can't be easily resolved as BLUETOOTH_PRIVILEGED
is reserved for system-level apps. Does anyone know what I'm doing wrong? Here's my onServicesDiscovered
function:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> services = gatt.getServices();
for(int i = 0; i < services.size(); i++){
List<BluetoothGattCharacteristic> characteristics = services.get(i).getCharacteristics();
for(int j = 0; j < characteristics.size(); j++){
//Register all characteristics for all services
BluetoothGattService service = services.get(i);
if (service != null) {
BluetoothGattCharacteristic characteristic = characteristics.get(j);
if (characteristic != null) {
try {
gatt.setCharacteristicNotification(characteristic, true);
}
catch(Exception e){
Log.d(TAG, "EXCEPTION: " + e);
}
}
}
}
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
I've made sure Bluetooth + Location are enabled, and have added the following lines to my AndroidManifest.xml
:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>