Android BluetoothLE Priperal

Asked 2 years ago, Updated 2 years ago, 37 views

http://qiita.com/anzai_k/items/131373caa0a6294efdd4

I created the BLE Peripheral app by referring to the above site.
However, when I receive a ReadRequest from Central, I output the following error to clear the service.I would like to ask you a question because I couldn't solve the problem.

W/BluetoothGattServer:Unhandled exception in callback

java.lang.NullPointerException: Attempt to Invoke virtual method 'boolean android.bluetooth.BluetoothGattServer.sendResponse(android.bluetooth.BluetoothDevice,int,int,int,byte[])' on an all object reference

android

2022-09-30 19:54

1 Answers

Since the code of the app being created is not listed, it is a guess from the code of the site being referenced.

java.lang.NullPointerException is occurring while trying to call the API below.

android.bluetooth.BluetoothGattServer.sendResponse

Since it was when the ReadRequest arrived, it must have occurred with the following code:

// ReadRequest coming from central (client)
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onCharacteristicReadRequest(android.bluetooth.BluetoothDevice, int requestId, 
    int offset, BluetoothGattCharacteristiccharacteristic) {
(omitted)
    bluetoothGattServer.sendResponse(device,requestId,BluetoothGatt.GATT_SUCCESS, offset,
(omitted)
}

This is because bluetoothGattServer is null.
Next, look at bluetoothGattServer.
You can see that the following constructors have been substituted:

//BLE
private BluetoothGattServer bluetoothGattServer;
public BLEServer (BluetoothGattServer) {
    This.bluetoothGattServer=gattServer;
}

The gattServer passed to the constructor is probably null.
Then look for this BELServer instantiating.
Then it seems that BLEServer is instantiated by the following actions:

 // Get GattServer
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private BluetoothGattServer getGattServer(Context context, BluetoothManager){
    return manager.openGattServer(context, new BLEServer(gattServer)));
}

Look for the gattServer passed here.Declaration is a member variable.

private BluetoothGattServergtServer;

It seems that you are replacing gattSever here.

// Start advertising
@TargetApi (Build.VERSION_CODES.LOLIPOP)
public void startAdvertise (Context context) {

    // Get BLE Various
    BluetoothManager= (BluetoothManager) context.getSystemService (Context.BLUETOOTH_SERVICE);
    BluetoothAdapter=manager.getAdapter();
    advertise = getAdvertiser(adapter);
    getServer=getGattServer(context,manager);
(omitted)
}

Please investigate so far and notice something strange.
The method you are calling to substitute gattServer is the getGattServer method you were just trying to instantiate BLEServer.
In other words, gattServer is not instantiated when calling the getGattServer method. (=null)
Therefore, gattServer passed to the constructor of BLEServer is likely to be null.

Then try debugging your own code to find out where null is actually.


2022-09-30 19:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.