0

This is a follow up from this post How to use GreenRobot's EventBus in broadcasting events from Service to Activity?

my use case revolves around a service and an Activity.

Service is used for tracking the changes in the BLE connection.

Activity is used for reporting that connection state to the UI.

Existing scenario. Service is using broadcast for sending the events (via sendBroadcast() method) of each state revolving around BLE (connected/Disconnected, Data available, etc..)

My Doubt: Can I make use of this GreenRobot's EventBus Library to control (send and receive events) in same way as broadcast does? If so, Is there anything I should consider (about thread safety) or to be must known, while replacing the broadcast control (send and receive) paradigm completely.

Community
  • 1
  • 1
Dinesh Ravi
  • 1,209
  • 1
  • 17
  • 35

1 Answers1

3

I'm currently using Otto (very similar to EventBus) to accomplish what you're wanting to do.

I have a service which holds a timer, and shows a persistent notification. Each update of the notification also publishes the newest information for the Activity to receive and then update the UI.

It's ALOT easier to implement with Otto (and possibly also EventBus, i haven't used that particular library) as i needed to send 4 pieces of information each time i published some information, and it grew tiresome to add extras to intents each time with the regular sendBroadcast() system.

I haven't had any issues after switching to Otto, and it helped me clean up alot of my code.

One thing to note (with Otto atleast) is that it's setup by default to only allow publishing/subscribing on the main thread, and as you're using a service, you'll have to edit this. I'm not sure whether or not EventBus has the same, but look out for it in their documentation.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • "One thing to note (with Otto at least) is that it's setup by default to only allow publishing/subscribing on the main thread, and as you're using a service, you'll have to edit this." >> Edit what Kasper? – Dinesh Ravi Oct 15 '15 at 16:09
  • Because you're using a service, which runs in another thread, you will have to set up Otto to allow publishing and subscribing from all threads (not just the main thread), otherwise it wont work. There's a full guide to it on their github page. – Moonbloom Oct 15 '15 at 17:20