killexe
killexe

Reputation: 466

Android BluetoothGattServerCallback SIGSEGV

I have two Android apps, one is my BluetoothLE server the other one is the client. On the client after I have discovered Services I use a characteristic to send a message. Something like this:

BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHAR_UUID);
characteristic.setValue("START SENDING...".getBytes());
gatt.writeCharacteristic(characteristic);

Then on my server the callback method onCharacteristicWriteRequest is called. Here I just log the message (which works fine) and then set a value in the characteristic and call notifyCharacteristicChanged. Something like this:

@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, 
    BluetoothGattCharacteristic characteristic, boolean preparedWrite, 
    boolean responseNeeded, int offset, byte[] value) {

    super.onCharacteristicWriteRequest(device, requestId, characteristic, 
        preparedWrite, responseNeeded, offset, value);

    byte[] bytes = value;
    String message = new String(bytes);
    Log.d(TAG, message);

    String someText = "Some Value";
    characteristic.setValue(someText.getBytes());
    bluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);
     if (responseNeeded) {
            bluetoothGattServer.sendResponse(device, requestId, 
                BluetoothGatt.GATT_SUCCESS, 0,null);
    }
}

Unfortunately I get the following error when the above method returns:

A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x10 in tid 10460 (Binder_3)

According to some stackoverflow questions, this error occurs if you try to dereference a null pointer, but none of my variables is null. Does anyone have an idea what could be the problem or at least have a tip on how to debug it?

EDIT:

When I comment out the following line of code I don't get this error, so apparently it has something to do with this, but I still don't know what exactly:

bluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);

EDIT 2:

This is how I configure my characteristic on the server:

BluetoothGattCharacteristic characteristic = new  
    BluetoothGattCharacteristic(CHAR_UUID,   
    BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ |   BluetoothGattCharacteristic.PROPERTY_BROADCAST, 
    BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

BluetoothGattDescriptor descriptor = new 
    BluetoothGattDescriptor(DESCRIPTOR_UUID,     
    BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

characteristic.addDescriptor(descriptor);
bluetoothGattService.addCharacteristic(characteristic);

Upvotes: 0

Views: 153

Answers (1)

killexe
killexe

Reputation: 466

Apparently calling

bluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);

in the onCharacteristicWriteRequest callback method causes the problem. Calling it somewhere else works just fine.

It would still be interesting if anyone knows WHY it doesn't work.

Upvotes: 1

Related Questions