问题
I have 3 components.
Activity1 has button for connecting and disconnecting the BLE Connection
Activity2 Needs to Get the data from the BLE Device.
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?
回答1:
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
来源:https://stackoverflow.com/questions/33581347/how-to-make-a-ble-connection-that-is-connected-using-service-to-use-across-the-a