Android – how to use the ble connection of service connection across activities without stopping the service or disconnecting?
I have three components
>Activity1 has buttons for connecting and disconnecting ble > activity2 needs to obtain data from ble devices. > all connection logic of the service (such as getremotedevice(), connectgatt, etc.) belongs to the service
Activity1 connects to ble device through binding service
Intent gattServiceIntent = new Intent(mContext,BleService.class);//In Activity1 context
bindService(gattServiceIntent, mServiceConnection,BIND_AUTO_CREATE);
Connect to the device immediately after pressing the button
Now, when I move from activity1 to activity2, I unbind the service in activity1
mContext.unbindService(mServiceConnection);//In Activity1 context
Now how do I use an existing ble device connection in activity2?
My interim solution:
I'm connecting the ble device again when I move to activity2 through a new service instance bound to it from the activity2 context. (I don't want.)
In activity2, I am checking whether my service is already running. If it is not running, I bind the service from activity2 context again
if(!isMyServiceRunning(BleWrapper.class)){
Intent wrapperServiceIntent = new Intent(mContext,BleWrapper.class);
bindService(wrapperServiceIntent,mBLEWrapperServiceConnection,BIND_AUTO_CREATE);
}else{
Log.w(LOGTAG, "Service already connected. In onCreate");
}
Trigger the connection in onserviceconnected() under the serviceconnection callback
@Override
public void onServiceConnected(ComponentName componentName,IBinder service) {
mBluetoothLeService = ((BleWrapper.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
showAlertDialog(getString(R.string.ble_not_supported_on_this_device));
}else {
mBluetoothLeService = BleWrapper.getInstance();
}
mBluetoothLeService.connect(/*address from shared preference*/); //Reconnecting to the same device using address stored in Shared pref
}
Used to check whether my service is running
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
However, the function ismyservicerunning() always returns false, which means that the service will be disconnected when moving from activity1 to activity2
Can any solution keep connected devices connected during activities?
resolvent:
Create a localbinder (extended binder) in the service class. Starting from the activity #1, you can start the service, use bindservice to access the binder object, and call unbindservice to disconnect from the service. Starting from the activity #2, you can call bindservice again to access the binder object because the service is still running, You can always keep the service running and access connected Bluetooth objects. See the example link below
bound service example