serwus
serwus

Reputation: 165

How to receive data via Bluetooth on Android?

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.

Upvotes: 0

Views: 2843

Answers (1)

Gaurav Sarma
Gaurav Sarma

Reputation: 2297

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);

Upvotes: 1

Related Questions