- Notifications
You must be signed in to change notification settings - Fork12
Bluetooth helper Java class
License
BasicAirData/BluetoothHelper
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A Bluetooth Java Class for Android.
Offered byBasicAirData - Open and free DIY air data instrumentation and telemetry
This Java Class implements an easy message-based Bluetooth wireless communication layer between anAndroid device (the client) and aMicrocontroller (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 BasicAirDataAir Data Bridge.
For further information about this library and its usage you can readthis 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:
BluetoothHelpermBluetoothHelper =newBluetoothHelper();
- Setup a BluetoothHelperListener to receive the messages and the changes of the connection status:
mBluetoothHelper.setBluetoothHelperListener(newBluetoothHelper.BluetoothHelperListener() {@OverridepublicvoidonBluetoothHelperMessageReceived(BluetoothHelperbluetoothhelper,finalStringmessage) {// Do something with the message received// runOnUiThread(new Runnable() {// @Override// public void run() {// // Update your UI// }// }); }@OverridepublicvoidonBluetoothHelperConnectionStateChanged(BluetoothHelperbluetoothhelper,booleanisConnected) {// Do something, depending on the new connection status }});
- Connect to a bondedBluetoothDevice:
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 fullBluetoothHelper 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);
About
Bluetooth helper Java class
Topics
Resources
License
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors4
Uh oh!
There was an error while loading.Please reload this page.