0

I'm trying to create an android app that communicate with Bluetooth low energy module. I am able to connect to module, read data and so on. But the problem is i do not know how to update textview from class that extends Service (the one in which whole connection to BLE is going on). I know there is like BILLION or even more posts like these, but believe me i tried.

Here is MainActivity: http://pastebin.com/6yaP0dYM

and here is a class that extends Service: http://pastebin.com/cYuAUina

If someone could provide me some tips to solve my issue that would be more than great!

2 Answers2

1

Create Broadcast Receiver and broadcast that in Service. If your Activity is ope, register receiver and in onReceive() update the UI.

The receiver should be in the Activity...

Below is the working code

https://stackoverflow.com/a/14695943/1403112

Community
  • 1
  • 1
Reddy Raaz
  • 699
  • 8
  • 9
0

You can get values through broadcast receiver......as follows, First create your own IntentFilter as,

Intent intentFilter=new IntentFilter();
intentFilter.addAction("YOUR_INTENT_FILTER");

Then create inner class BroadcastReceiver as,

  private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    /** Receives the broadcast that has been fired */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction()=="YOUR_INTENT_FILTER"){
           //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
           String receivedValue=intent.getStringExtra("KEY");
        }
    }
};

Now Register your Broadcast receiver in onResume() as,

registerReceiver(broadcastReceiver, intentFilter);

And finally Unregister BroadcastReceiver in onDestroy() as,

unregisterReceiver(broadcastReceiver);

Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);

....cheers :)

Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36