A lightweight, unified library for Serial Bluetooth Classic and Low Energy (BLE) communication on Android. ๐
Android Bluetooth Library provides a simple, robust, and unified solution for Serial Bluetooth Classic (SPP) and Bluetooth Low Energy (BLE GATT) communication on Android. Designed with asynchronous thread-safe I/O, automated delimiter-based packet framing, and seamless background-to-main thread dispatching.
Quick Start ยท
Why ยท
Compatibility ยท
Installation ยท
Configuration ยท
Usage ยท
API Reference ยท
Troubleshooting ยท
Contributing
๐ Quick Start
- Add the JitPack repository to your root
build.gradle:
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
- Add the dependency to your app module
build.gradle (choose Classic or Low Energy):
dependencies {
// For Bluetooth Classic (API 7+)
implementation 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothClassicLibrary:0.3.5'
// OR for Bluetooth Low Energy (API 18+)
// implementation 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothLowEnergyLibrary:0.3.5'
}
- Initialize the service in your
Application class or main activity:
BluetoothConfiguration config = new BluetoothConfiguration();
config.context = getApplicationContext();
config.bluetoothServiceClass = BluetoothClassicService.class; // Or BluetoothLeService.class
config.bufferSize = 1024;
config.characterDelimiter = '\n';
config.deviceName = "Your App Name";
config.callListenersInMainThread = true;
config.uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard SPP UUID
BluetoothService.init(config);
- Retrieve the service singleton and send a message:
BluetoothService service = BluetoothService.getDefaultInstance();
BluetoothWriter writer = new BluetoothWriter(service);
// Send text terminated with the configured delimiter ('\n')
writer.writeln("Hello from Android!");
๐ก Why Android Bluetooth Library?
- โก Unified Dual-Engine Architecture. Switch between Bluetooth Classic and Bluetooth Low Energy by swapping a single service class name without rewriting your event listeners or UI code.
- ๐ Automated Framing & Delimiters. Never struggle with fragmented TCP-like byte buffers again: automatically buffers incoming streams and triggers
onDataRead upon reaching a custom delimiter (\n, \r, or custom bytes).
- ๐งต Asynchronous Non-Blocking I/O. Device connections, GATT handshakes, and byte streams run safely on isolated background worker threads, keeping your application responsive.
- ๐ฏ Automatic Main Thread Dispatch. Event callbacks (
onDataRead, onStatusChange, onDataWrite, onDeviceName, onToast) are dispatched directly to Android's UI Main Thread by default.
- ๐ก Built-In Scanning & Filtering. Discover nearby peripherals with real-time RSSI signal strength monitoring and target UUID filtering.
- ๐ LE Connection Priority Tuning. Easily request high-throughput GATT connection parameters (
CONNECTION_PRIORITY_HIGH) for fast data transfers.
- ๐ชถ Modular & Zero Bloat. Import only the sub-library you need (
BluetoothClassicLibrary or BluetoothLowEnergyLibrary) with zero heavy external dependencies.
- ๐ค Broad Android Support. Battle-tested compatibility ranging from legacy Android 2.1 (API 7) up to modern Android versions.
๐ฑ Compatibility
| Engine |
Target |
Minimum Android |
Recommended Use Cases |
Bluetooth Classic (BluetoothClassicLibrary) |
RFCOMM / SPP |
Android 2.1 (API 7+) |
Arduino HC-05/HC-06, thermal receipt printers, barcode scanners, ELM327 OBD-II |
Bluetooth Low Energy (BluetoothLowEnergyLibrary) |
GATT Client |
Android 4.3 (API 18+) |
Nordic nRF, ESP32, HM-10, fitness trackers, smart home sensors, low-power IoT |
๐ฆ Installation
1. Root Repository
Add the JitPack repository URL in your root build.gradle file at the end of repositories:
allprojects {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
[!NOTE]
If your project uses Gradle 7+ with dependencyResolutionManagement in settings.gradle, add the maven { url "https://jitpack.io" } entry inside repositoriesMode block instead.
2. Module Dependencies
Add the module dependency in your app module's build.gradle:
Bluetooth Classic
dependencies {
implementation 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothClassicLibrary:0.3.5'
}
Bluetooth Low Energy
dependencies {
implementation 'com.github.douglasjunior.AndroidBluetoothLibrary:BluetoothLowEnergyLibrary:0.3.5'
}
3. AndroidManifest Permissions
Add the required permissions to your AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">
<!-- Basic Bluetooth permissions -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
<!-- Required for Bluetooth device scanning (Android 6.0+) -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Optional: fine location for high accuracy scanning -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
[!IMPORTANT]
Location Permission for Bluetooth Scanning: Starting with Android 6.0 (API 23), Android requires runtime location permission to detect nearby Bluetooth devices. See Troubleshooting for details.
Configuration
Initialize the library once in your Application.onCreate() or before using any Bluetooth services.
Bluetooth Classic
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothClassicService;
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothConfiguration;
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothService;
import java.util.UUID;
BluetoothConfiguration config = new BluetoothConfiguration();
config.context = getApplicationContext();
config.bluetoothServiceClass = BluetoothClassicService.class;
config.bufferSize = 1024;
config.characterDelimiter = '\n';
config.deviceName = "My Android App";
config.callListenersInMainThread = true;
// Standard Serial Port Profile (SPP) UUID (Required for Classic)
config.uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
BluetoothService.init(config);
Bluetooth Low Energy (BLE)
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothConfiguration;
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothService;
import com.github.douglasjunior.bluetoothlowenergylibrary.BluetoothLeService;
import java.util.UUID;
BluetoothConfiguration config = new BluetoothConfiguration();
config.context = getApplicationContext();
config.bluetoothServiceClass = BluetoothLeService.class;
config.bufferSize = 1024;
config.characterDelimiter = '\n';
config.deviceName = "My Android App";
config.callListenersInMainThread = true;
// Custom or standard GATT Service & Characteristic UUIDs (Required for BLE)
config.uuidService = UUID.fromString("e7810a71-73ae-499d-8c15-faa9aef0c3f2");
config.uuidCharacteristic = UUID.fromString("bef8d6c9-9c21-4c9e-b632-bd58c1009f9f");
// Transport preference for dual-mode devices
config.transport = BluetoothDevice.TRANSPORT_LE;
// Optional: Filter devices by UUID during scan (set null to scan all BLE devices)
config.uuid = null;
// Optional: Automatically request high throughput priority upon connection
config.connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_HIGH;
BluetoothService.init(config);
Kotlin Configuration Example
val config = BluetoothConfiguration().apply {
context = applicationContext
bluetoothServiceClass = BluetoothClassicService::class.java // or BluetoothLeService::class.java
bufferSize = 1024
characterDelimiter = '\n'
deviceName = "My Kotlin App"
callListenersInMainThread = true
uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb")
}
BluetoothService.init(config)
๐ป Usage & Examples
1. Obtaining the Service Instance
Once initialized via BluetoothService.init(config), retrieve the singleton anywhere in your app:
BluetoothService service = BluetoothService.getDefaultInstance();
2. Scanning for Devices
Register OnBluetoothScanCallback to receive discovery events and start scanning:
service.setOnScanCallback(new BluetoothService.OnBluetoothScanCallback() {
@Override
public void onDeviceDiscovered(BluetoothDevice device, int rssi) {
Log.d("Bluetooth", "Discovered: " + device.getName() + " [" + device.getAddress() + "] RSSI: " + rssi);
}
@Override
public void onStartScan() {
Log.d("Bluetooth", "Scan started");
}
@Override
public void onStopScan() {
Log.d("Bluetooth", "Scan finished");
}
});
// Start scanning (BLE scans run for 10 seconds by default)
service.startScan();
// Stop scanning at any time
// service.stopScan();
3. Connecting to a Device & Listening to Events
Register OnBluetoothEventCallback to monitor connection state changes and incoming data:
service.setOnEventCallback(new BluetoothService.OnBluetoothEventCallback() {
@Override
public void onStatusChange(BluetoothStatus status) {
// Status can be: NONE, CONNECTING, or CONNECTED
Log.d("Bluetooth", "Status changed: " + status);
}
@Override
public void onDeviceName(String deviceName) {
Log.d("Bluetooth", "Connected device name: " + deviceName);
}
@Override
public void onToast(String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onDataRead(byte[] buffer, int length) {
String message = new String(buffer, 0, length);
Log.d("Bluetooth", "Received: " + message);
}
@Override
public void onDataWrite(byte[] buffer) {
Log.d("Bluetooth", "Data sent successfully");
}
});
// Connect to a discovered or paired BluetoothDevice
service.connect(device);
// Disconnect when done
// service.disconnect();
4. Writing Data
The library provides a handy BluetoothWriter helper to write strings, numbers, characters, or line-delimited messages:
BluetoothWriter writer = new BluetoothWriter(service);
// Write text and automatically append characterDelimiter ('\n')
writer.writeln("CMD:READ_SENSORS");
// Write text without trailing delimiter
writer.write("RAW_DATA");
// Write integer or character
writer.write(42);
writer.write('A');
// Write raw byte array directly via service
service.write(new byte[]{ 0x02, 0x55, 0x03 });
5. Requesting Connection Priority (BLE)
For high-throughput BLE data transfer, request higher connection priority at runtime:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
service.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
}
6. Complete Sample Projects
Explore the full working sample applications in this repository:
- Java Sample:
Sample (features ESC/POS thermal printing, bitmap streaming, and device selection)
- Kotlin Sample:
SampleKotlin (Kotlin implementation of scanning and communication)
๐ API Reference
BluetoothConfiguration
| Property |
Type |
Description |
Default |
bluetoothServiceClass |
Class<? extends BluetoothService> |
Service implementation class (BluetoothClassicService.class or BluetoothLeService.class). |
Required |
context |
Context |
Android Application context reference. |
Required |
deviceName |
String |
Name of your application or device. |
null |
bufferSize |
int |
Maximum bytes retained in the read buffer before triggering onDataRead. |
0 |
characterDelimiter |
char |
Delimiter character to frame complete packets. Triggers onDataRead. |
'\0' |