harryjph / android-bluetooth-serial

A library for Android to simplify basic serial communication over Bluetooth, for example when communicating with Arduinos.
Apache License 2.0
177 stars 60 forks source link
android android-arduino android-arduino-remote android-bluetooth android-bluetooth-device android-bluetooth-serial android-library arduino arduino-bluetooth arduino-bluetooth-android arduinos bluetooth bluetooth-arduino bluetooth-connection java kotlin kotlin-android library serial serial-communication

android-bluetooth-serial

Build Status

A library for Android to simplify basic serial communication over Bluetooth, for example when communicating with Arduinos.

How to include the library

JitPack

Gradle

Using the library

Please see the demoApplication directory for a fully-featured demo app.

  1. Declare your BluetoothManager (Make sure to include the library BluetoothManager, not the Android one):
import com.harrysoft.androidbluetoothserial.BluetoothManager;

Your Activity's onCreate():

// Setup our BluetoothManager
BluetoothManager bluetoothManager = BluetoothManager.getInstance();
if (bluetoothManager == null) {
    // Bluetooth unavailable on this device :( tell the user
    Toast.makeText(context, "Bluetooth not available.", Toast.LENGTH_LONG).show(); // Replace context with your context instance.
    finish();
}
  1. Get the list of paired devices:
Collection<BluetoothDevice> pairedDevices = bluetoothManager.getPairedDevices();
for (BluetoothDevice device : pairedDevices) {
    Log.d("My Bluetooth App", "Device name: " + device.getName());
    Log.d("My Bluetooth App", "Device MAC Address: " + device.getAddress());
}
  1. Select a device you want to connect to from the list and fetch its MAC Address.

  2. Connect to the device and send/receive messages:

import com.harrysoft.androidbluetoothserial.BluetoothSerialDevice;
private SimpleBluetoothDeviceInterface deviceInterface;

private void connectDevice(String mac) {
    bluetoothManager.openSerialDevice(mac)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(this::onConnected, this::onError);
}

private void onConnected(BluetoothSerialDevice connectedDevice) {
    // You are now connected to this device!
    // Here you may want to retain an instance to your device:
    deviceInterface = connectedDevice.toSimpleDeviceInterface();

    // Listen to bluetooth events
    deviceInterface.setListeners(this::onMessageReceived, this::onMessageSent, this::onError);

    // Let's send a message:
    deviceInterface.sendMessage("Hello world!");
}

private void onMessageSent(String message) {
    // We sent a message! Handle it here.
    Toast.makeText(context, "Sent a message! Message was: " + message, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
}

private void onMessageReceived(String message) {
    // We received a message! Handle it here.
    Toast.makeText(context, "Received a message! Message was: " + message, Toast.LENGTH_LONG).show(); // Replace context with your context instance.
}

private void onError(Throwable error) {
    // Handle the error
}
  1. Disconnect the device:
    
    // Please remember to destroy your instance after closing as it will no longer function!

// Disconnect one device bluetoothManager.closeDevice(macAddress); // Close by mac // OR bluetoothManager.closeDevice(connectedDevice); // Close by device instance // OR bluetoothManager.closeDevice(deviceInterface); // Close by interface instance

// Disconnect all devices bluetoothManager.close();