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.MiguelHorta:Bluetooth-Library:'
}
dependencies {
implementation("com.github.MiguelHorta:Bluetooth-Library:")
}
<dependency>
<groupId>com.github.MiguelHorta</groupId>
<artifactId>Bluetooth-Library</artifactId>
<version></version>
</dependency>
libraryDependencies += "com.github.MiguelHorta" % "Bluetooth-Library" % ""
:dependencies [[com.github.MiguelHorta/Bluetooth-Library ""]]
I used a BufferedReader to read data from the bluetooth socket. As I'm reading with readLine(), each message you're sending to the Android must end with a \n. Otherwise it won't be received.
The class uses listeners so everything is really easy! Enjoy :)
Add to your gradle dependencies:
compile 'me.aflak.libraries:bluetooth:1.2.4'
Bluetooth bluetooth = new Bluetooth(this);
bluetooth.enableBluetooth();
bluetooth.setDiscoveryCallback(new Bluetooth.DiscoveryCallback() {
@Override
public void onFinish() {
// scan finished
}
@Override
public void onDevice(BluetoothDevice device) {
// device found
}
@Override
public void onPair(BluetoothDevice device) {
// device paired
}
@Override
public void onUnpair(BluetoothDevice device) {
// device unpaired
}
@Override
public void onError(String message) {
// error occurred
}
});
bluetooth.scanDevices();
bluetooth.pair(device);
bluetooth.setCommunicationCallback(new Bluetooth.CommunicationCallback() {
@Override
public void onConnect(BluetoothDevice device) {
// device connected
}
@Override
public void onDisconnect(BluetoothDevice device, String message) {
// device disconnected
}
@Override
public void onMessage(String message) {
// message received (it has to end with a \n to be received)
}
@Override
public void onError(String message) {
// error occurred
}
@Override
public void onConnectError(BluetoothDevice device, String message) {
// error during connection
}
});
// three options
bluetooth.connectToName("name");
bluetooth.connectToAddress("address");
bluetooth.connectToDevice(device);
bluetooth.send("message");
List<BluetoothDevice> devices = bluetooth.getPairedDevices();