Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.harry1453:android-bluetooth-serial:1.1.2'
}
dependencies {
implementation("com.github.harry1453:android-bluetooth-serial:1.1.2")
}
<dependency>
<groupId>com.github.harry1453</groupId>
<artifactId>android-bluetooth-serial</artifactId>
<version>1.1.2</version>
</dependency>
libraryDependencies += "com.github.harry1453" % "android-bluetooth-serial" % "1.1.2"
:dependencies [[com.github.harry1453/android-bluetooth-serial "1.1.2"]]
A library for Android to simplify basic serial communication over Bluetooth, for example when communicating with Arduinos.
Gradle
build.gradleallprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
build.gradledependencies {
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'
}
Please see the demoApplication directory for a fully-featured demo app.
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();
}
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());
}
Select a device you want to connect to from the list and fetch its MAC Address.
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
}
// 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();