0

I read many topics, but I couldn't find good answer. I'm working on Android application that uses Bluetooth to send and receive data from Microcontroller. I have already finished sending part and it works fine, but I have problem with receiving data on Android. I'm using this library: https://android-arsenal.com/details/1/690#!description It doesn't have properly tutorial (or at least I don't see it), it just says about receiving data on android this:

//Listener for data receiving
bt.setOnDataReceivedListener(new OnDataReceivedListener() {
    public void onDataReceived(byte[] data, String message) {
        // Do something when data incoming
    }
});

Does anybody have any idea how to use it? I have tried to write whole Bluetooth part by myself, but it was too hard, so I decided to use this library. I need to listen for incoming datas all the time, but also I can't do it in loop, because it will block the UI thread.

serwus
  • 155
  • 1
  • 14

1 Answers1

1

This is basically an callback function and as you can see in the parameter it is giving you 2 things data of type byte[] and message of type String. Now you can just log the 2 and see what values are being given to you like below

Log.d("Data value : " + data.toString() + "Message : " + message);

And then you can do whatever that you intend to do with it like update a view, etc. like below

TextView messageView = findViewById(R.id.message);
messageView.setText(message);
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
  • I think I almost got it. So this looks like the library buffers data for me, and when I want to do something with it, I just need to call this listener? I need to do somehing like this: if Microcontroller sends data (it can be anytime), APP will change some variables. Should I put this listener to separate thread? – serwus Sep 02 '16 at 16:47
  • You can't put the listener on a different thread but you can put the class that implements this listener and execute it in a separate thread – Gaurav Sarma Sep 02 '16 at 17:01
  • I was going to. That helps a lot. Thanks! – serwus Sep 02 '16 at 18:28