Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a BLE connection that is connected using service to use across the activities without stopping the service or disconnecting ble?

I have 3 components.

  1. Activity1 has button for connecting and disconnecting the BLE Connection

  2. Activity2 Needs to Get the data from the BLE Device.

  3. Service All the connection logic (like getRemoteDevice(),connectGatt etc.,) belongs to service.

Activity1 is connecting to BLE Device by binding the service.

Intent gattServiceIntent = new Intent(mContext,BleService.class);//In Activity1 context
bindService(gattServiceIntent, mServiceConnection,BIND_AUTO_CREATE);

and makes connection to ble device as soon as button is pressed.

Now when I move from Activity1 to Activity2 I am unbinding the service in Activity1.

mContext.unbindService(mServiceConnection);//In Activity1 context

Now How do I use the existing BLE Device connection in Activity2 ?

My temporary Solution:

I am re Connecting the BLE Device again when moving to Activity2 by new service instance binded to it from Activity2 context. (Which I don't want.)

In Activity2 I am checking whether my service is already running if not running then I am binding the service again from Activity2 Context.

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 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
}  

For Checking whether my service is running or not

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;
}

But the function isMyServiceRunning() always returns false. meaning the service gets disconnected when moving from Activity1 to Activity2

Any Solution for persisting the ble device connection across the activities?

like image 256
Dinesh Ravi Avatar asked Sep 27 '22 06:09

Dinesh Ravi


1 Answers

Create a LocalBinder(extends Binder) in your Service class. From Activity #1, you can start the service and also use bindservice to access the binder object and call unbindservice to disconnect from service. From Activity #2, you can simply call bindservice again to access the binder object as service is still running. This way you can keep the service running always and access the connected bluetooth objects. Refer the example from below link.

bound service example

like image 115
siva Avatar answered Nov 15 '22 12:11

siva