A Bluetooth Java Class for Android.
Offered by BasicAirData - Open and free DIY air data instrumentation and telemetry
This Java Class implements an easy message-based Bluetooth wireless communication layer between an Android device (the client) and a Microcontroller (the server).
Using this class you can Connect, Disconnect, Send String messages, Receive String messages via Listener (best way) or by explicit polling, automatically reconnect and check the status of your Bluetooth connection in a simple and thread-safe way.
The library is compatible with android 4.0 (API 14) and newer.
Here in this repo (in the Arduino/BluetoothHelperExample folder) you can also find a very simple example (around 100 lines of code, including comments) that shows how to use it.
You can find a complete implementation of this library in BasicAirData Air Data Bridge.
For further information about this library and its usage you can read this article.
Use Gradle
dependencies {
implementation 'eu.basicairdata:bluetoothhelper:1.0.5'
}
Or Maven
<dependency>
<groupId>eu.basicairdata</groupId>
<artifactId>bluetoothhelper</artifactId>
<version>1.0.5</version>
<type>pom</type>
</dependency>
- Add Bluetooth permissions to your AndroidManifest.xml:
<uses-permission android:name = "android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name = "android.permission.BLUETOOTH"/>
- Declare a new BluetoothHelper instance into your activity:
BluetoothHelper mBluetoothHelper = new BluetoothHelper();
- Setup a BluetoothHelperListener to receive the messages and the changes of the connection status:
mBluetoothHelper.setBluetoothHelperListener(new BluetoothHelper.BluetoothHelperListener() {
@Override
public void onBluetoothHelperMessageReceived(BluetoothHelper bluetoothhelper, final String message) {
// Do something with the message received
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// // Update your UI
// }
// });
}
@Override
public void onBluetoothHelperConnectionStateChanged(BluetoothHelper bluetoothhelper, boolean isConnected) {
// Do something, depending on the new connection status
}
});
- Connect to a bonded BluetoothDevice:
mBluetoothHelper.Connect(mBluetoothDevice);
- As alternative, connect to a BluetoothDevice directly using its Device Name:
mBluetoothHelper.Connect("HC-05");
- Send a message:
mBluetoothHelper.SendMessage("Hello World");
- Check the connection status:
mBluetoothHelper.isConnected();
- Disconnect:
mBluetoothHelper.Disconnect();
For further information you can read the full BluetoothHelper API Documentation.
- Android/BluetoothHelper.class = The Helper Class for Android;
The repo includes also a very simple but fully functional example, that shows how to use the helper class:
- Android/BluetoothHelperExample/ = Example app for Android - around 100 lines of code, including comments - that controls 2 leds (one ON/OFF using a button and one in PWM using a slider);
- Arduino/BluetoothHelperExample/ = The sketch for Arduino; this example (ready to be uploaded on Arduino 2009) is made to communicate with the example app;
- doc/ = The related documentation. The folder includes the API documentation of the Class and the electrical scheme of the microcontroller circuit, showing one of the possible hardware configurations (Arduino 2009 + BlueSMIRF Gold);