Polling GPIO in background on Android

edited January 2015 in Android
I'm seeking an architectural advice from the developer(s) who engineered Android API. 
What I'm planning to do is to connect sensor to GPIO pins which will measure some values. Now I want to constantly poll those values in Android application.
What I have in mind:
1. When app is launched, start Service, call it SensorService
2. From SensorService, bind to MetaWearBleService, register broadcast receiver.
3. Schedule polling by calling GPIO.readDigitalInput
4.Receive value with callback

So basically I want to get values from GPIO in background all the time. Is this how you would do it? I'm a bit confused by the necessity of binding from one Service to another, since this is not a common pattern in Android development(AFAIK).

Thanks in advance!

Comments

  • Anyone here? 
  • Register the MetaWear broadcast receiver and Intent filter with either the activity, MetaWearBleService, or LocalBroadcastManager.  There is no need to bind one service to another.
  • But if I want to receive update in background I need a service. Activity can't receive events if it's in background.
  • Yes it can.  As I mentioned in my previous post, bind the receive/intent to the service class or use the LocalBroadcastManager
  • I'm sorry, but I just don't see it working. The rule of thumb is that you should unregister all broadcast receivers(and I don't see how LocalBroadcastManager is different in this regard) when Activity goes into background and register them when Activity goes into foreground.
    So I don't see a way for polling data in Activity. The reason is that Activity which is background can be killed any second by the OS. So to constantly poll some data I need to create a Service and bound to SDK Service in it. I just can't imagine background polling of data working with Activity. 
    I also don't see how I can register receiver with "MetaWearBleService" as you said in your post above.
    I just think that the scenario of polling data from Metawear board is wide spread I wanted some input from SDK developers. But unfortunately I can't follow you. 
    Maybe we're just not on the same page here?  If you could support your suggestion with some examples of polling data in background? 
    Because frankly speaking your sample app hard to follow. The lack of documentation doesn't help either. 
  • The service class has a registerReceiver method or, call useLocalBroadcasterManager(true) to use the LocalBroadcastManager.

    @Override
    public void onServiceConnected(ComponentName componentName,
            IBinder service) {
        mwService= ((MetaWearBleService.LocalBinder) service).getService();
        mwService.useLocalBroadcasterManager(true);
        LocalBroadcastManager.getInstance(mwService).registerReceiver(
                MetaWearBleService.getMetaWearBroadcastReceiver(),
                MetaWearBleService.getMetaWearIntentFilter());
    }
  • Ok, but if I do it my Activity, where will I receive the broadcasts? 
  • edited February 2015
    Broadcasts are processed inside the MetaWearBleService class; your app will never see them unless you create your own BroadcastReceiver class.  All you are doing is deciding when the intents should be processed i.e. only when the app is active, or continuously handle them even in the background.
  • Sorry, looking at the OP again, I have missed a huge majority of your question.  Polling GPIO data in the background does require setting up the broadcast, however it also requires recording an event macro that is triggered via the newly added Timer class.  This was not possible prior to firmware v1.0.0 and Android API v1.4 unless the app itself kept sending readAnalogInput requests.  What you do is:
    1. Use the Timer controller to setup periodic notifications 
    2. Have Event controller listen for timer notifications 
    3. Record the gpio read command you want to execute
    4. Start the timer
    There is a post about the Event system on the MbientLab blog (http://projects.mbientlab.com/285/).
This discussion has been closed.