Giter Club home page Giter Club logo

android-bluetooth-serial's Introduction

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

  • Project level build.gradle
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  • App level build.gradle
dependencies {
    implementation 'com.github.harry1453:android-bluetooth-serial:v1.1'
    
    // RxJava is also required.
    implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
}

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

android-bluetooth-serial's People

Contributors

harryjph avatar augonis avatar matinzd avatar stefanlechner avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.