问题描述
我有3个组成部分。
-
活动1 有按钮连接和断开连接BLE
Activity1 has button for connecting and disconnecting the BLE Connection
活性2 需要从BLE设备获取的数据。
Activity2 Needs to Get the data from the BLE Device.
服务所有的连接逻辑(如getRemoteDevice(),connectGatt等)属于服务。
Service All the connection logic (like getRemoteDevice(),connectGatt etc.,) belongs to service.
活动1 的连接通过绑定服务BLE设备。
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);
和使连接尽快BLE装置按钮pressed。
and makes connection to ble device as soon as button is pressed.
现在的活动1 为活性2 我在解除绑定服务的活动1
Now when I move from Activity1 to Activity2 I am unbinding the service in Activity1.
mContext.unbindService(mServiceConnection);//In Activity1 context
现在我如何使用现有设备BLE连接的活性2
Now How do I use the existing BLE Device connection in Activity2 ?
我的临时解决方法:
我的的重新连接的时再BLE设备移动到活性2 由绑定到其新的服务实例的活性2 上下文。 (我不想要的。)
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.)
在活性2 我检查我的服务是否已在运行,如果再没有运行,我再次从活性2 上下文绑定服务。
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");
}
触发下ServiceConnection回调onServiceConnected()连接
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;
}
但功能 isMyServiceRunning()始终返回false。这意味着该服务移动时断开连接的活动1 为活性2
But the function isMyServiceRunning() always returns false. meaning the service gets disconnected when moving from Activity1 to Activity2
任何解决方案持续整个活动的BLE设备连接?
Any Solution for persisting the ble device connection across the activities?
推荐答案
创建LocalBinder(扩展粘结剂)。从活动#1,您可以启动该服务,也可以使用bindservice访问粘结剂对象,然后调用unbindservice从服务中断开。从活动#2,你可以简单地再次调用bindservice访问粘结剂对象作为服务仍在运行。这样,您就可以保留该服务始终运行和访问连接的蓝牙对象。从下面链接,参阅的例子。
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.
这篇关于如何使正在使用的服务跨接在活动中使用BLE连接而无需停止服务或断开BLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!