Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用绿色机器人&x27;s EventBus将事件从服务广播到活动?_Android_Bluetooth Lowenergy_Greenrobot Eventbus - Fatal编程技术网

Android 如何使用绿色机器人&x27;s EventBus将事件从服务广播到活动?

Android 如何使用绿色机器人&x27;s EventBus将事件从服务广播到活动?,android,bluetooth-lowenergy,greenrobot-eventbus,Android,Bluetooth Lowenergy,Greenrobot Eventbus,最近我意识到了EventBus库。基本上,我的用例围绕服务和活动展开 服务用于跟踪BLE连接中的更改 活动用于向UI报告该连接状态 如何使用库实现相同的功能。在活动的onResume方法中,注册事件: EventBus.getDefault().register(this); 并在暂停时取消注册 EventBus.getDefault().unregister(this); 当服务运行并获取有关BLE的信息时,通过EventBus发送此信息: BLEInfo bleInfo = new BL

最近我意识到了EventBus库。基本上,我的用例围绕服务和活动展开

服务用于跟踪BLE连接中的更改

活动用于向UI报告该连接状态


如何使用库实现相同的功能。

在活动的
onResume
方法中,注册事件:

EventBus.getDefault().register(this);
并在暂停时取消注册

EventBus.getDefault().unregister(this);
当服务运行并获取有关BLE的信息时,通过EventBus发送此信息:

BLEInfo bleInfo = new BLEInfo(... // create some kind of object to aggregate the info about ble connection
EventBus.getDefault().post(bleInfo);
最后,实现活动获取信息的行为:

public void onEvent(BLEInfo bleInfo) {
    // update your UI based on bleInfo
}