douglasjunior / AndroidBluetoothLibrary

A Library for easy implementation of Serial Bluetooth Classic and Low Energy on Android. 💙
MIT License
224 stars 68 forks source link

Message Queueing #2

Closed thomaskioko closed 7 years ago

thomaskioko commented 7 years ago

@douglasjunior How can I wait for a response before running another command? I.e I have a list of 3 commands, before running the second command, I wait for a response from the first one.

douglasjunior commented 7 years ago

You need to control this because communication is asynchronous. Bluetooth does not depend on you sending information to return a response.

Every time the device sends a response it will be caught by the event onDataRead.

service.setOnEventCallback(new BluetoothService.OnBluetoothEventCallback() {
    @Override
    public void onDataRead(byte[] buffer, int length) {
         // If you want to convert to String
        String text = new String(buffer, 0, length);
        sendNextCommand();
    }
    ...
});

private void sendNextCommand() {
      bytes[] nextCommand = ...;
      service.write(nextCommand );
}
douglasjunior commented 7 years ago

Please, if you have no further questions, you can close the issue. 😄