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

非 Y 不嫁゛ 提交于 2020-01-01 05:39:07

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!