AprilBeaconCharacteristics.close() gives unhandled exception

Hi,

I’m using the AprilBeaconCharacteristics to get the battery level, and it works well, excepting the fact that BluetoothGatt sends me an unhandled exception everytime I try to close the characteristic reader… Is there anything to do about it ?

Below the relevant excerpt of my code (totally inspired from your github examples) + the error message thrown :

public class AprilService extends Service
{
	private static AprilBeaconCharacteristics aprilBeaconRead;
	static Handler handler;
	
	@Override
	public void onCreate() 
	{
		
		super.onCreate();
		handler = new Handler();
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		Log.i(TAG,"onHandleIntent");
		
		String proximityUUID = intent.getStringExtra("proximityUUID");
		String name = intent.getStringExtra("name");
		String macAddress = intent.getStringExtra("macAddress");
		int major = Integer.parseInt(intent.getStringExtra("major"));
		int minor = Integer.parseInt(intent.getStringExtra("minor"));
		int measuredPower = Integer.parseInt(intent.getStringExtra("measuredPower"));
		int rssi = Integer.parseInt(intent.getStringExtra("rssi"));
		int dongleID = intent.getIntExtra("dongleID",0);
		
		getSendBattery(proximityUUID,name,macAddress,major,minor,measuredPower,rssi,dongleID);
		
		return super.onStartCommand(intent, flags, startId);
	}
	
	private static void getSendBattery(String proximityUUID,String name,final String macAddress,int major,int minor,int measuredPower,int rssi,final int dongleID)
	{
		Beacon aprilBeacon = new Beacon(proximityUUID, name, macAddress, major, minor, measuredPower, rssi);
		
		aprilBeaconRead = new AprilBeaconCharacteristics(context, aprilBeacon);
		
		aprilBeaconRead.connectGattToRead(new MyReadCallBack() {
			
			@Override
			public void readyToGetBattery() {
				
				try {
					
					final Integer battery = aprilBeaconRead.getBattery();
					
					runOnUiThread(new Runnable() {
						
						@Override
						public void run() {
							
							Log.i(TAG,"battery="+battery);
							// This works fine, I get it, perfect... 
							
							// aprilBeaconRead.close();
							// Sending an unhandled exception
							
							
						}
					});
					
				
				} catch (Exception e) {
					
					Log.i("AprilService","cannot read aprilBeacon Characteristics");
					
					e.printStackTrace();
				
				} finally {
					
					aprilBeaconRead.close();
					// Sending an unhandled exception as well 
					
					
				}
				
				
			}
			

		}, READ_BATTERY);
		
		
		
		
	}
	
	private static void runOnUiThread(Runnable runnable) {
        handler.post(runnable);
    }
    
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}
	
	@Override
    public void onDestroy() 
    {
       super.onDestroy();
    }
    
    
}

And the corresponding output :

09-18 13:59:53.655: I/AprilBrotherSDK(24476): Connected to GATT server.
09-18 13:59:53.660: I/AprilBrotherSDK(24476): Attempting to start service discovery:true
09-18 13:59:53.743: I/AprilService(24476): battery=87
09-18 13:59:53.755: I/AprilBrotherSDK(24476): batteryLevel = 87
09-18 13:59:53.756: W/BluetoothGatt(24476): Unhandled exception in callback
09-18 13:59:53.756: W/BluetoothGatt(24476): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.BluetoothGattCallback.onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)' on a null object reference
09-18 13:59:53.756: W/BluetoothGatt(24476): 	at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:181)
09-18 13:59:53.756: W/BluetoothGatt(24476): 	at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
09-18 13:59:53.756: W/BluetoothGatt(24476): 	at android.os.Binder.execTransact(Binder.java:446)

Can you help me on this ?
Thank you !

Did this exception cause the crash
I test the AprilBeaconDemo in nexus 5 , it running well
Did you use other phone to have a try

This does not cause a crash, but this is a warning message.
It occurs on a Nexus 6 with Android 5.1.1

I also can test this on a Wiko Sunset with Android 4.4 , and it makes no warning there…

Perhaps beacause in april.close() it disconnect and then close, in other phone,
It have no problem but in nexus 6 disconnect is running in another thread and it have a delay before the gatt.close().
You can try to connect ohter device after aprilBeaconRead.close(); was executed
If it can connect that mean last connecting was closed

Yes I can connect after that… So in other words, this is just a matter of delay ?
So, there is no problem keeping this warning each time ?

Yes there is no problem