A collection of easy to use bluetooth utilities for Android.
Add the following line under repositories to the project root build.gradle file.
maven { url 'https://jitpack.io' }
Then add the dependency in your app build.gradle file
implementation 'com.github.KPreetham:Android-EasyBluetooth:RELEASE_VERSION'
Refer to JitPack
badge above for RELEASE_VERSION.
Implement dataListener.
dataListener = new SppDataListener() {
@Override
public void onDataReceived(byte[] data, int bytes_read) {
// received `data` of length `bytes_read`
}
@Override
public void onDataSent(byte[] data) {
// `data` was successfully written to the stream.
}
};
Implement connection listener
connectionListener = new SppConnectionListener() {
@Override
public void onConnectionSuccess() {
// Successfully connected to bluetooth device.
}
@Override
public void onConnectionFailure() {
// Connection attempt failed.
}
@Override
public void onConnectionLost() {
// Connection is lost. Socket might have been closed abruptly.
}
@Override
public void onConnectionClosed() {
// Connection was closed succesfully.
}
};
Create a BluetoothSpp object.
bluetoothSpp = new BluetoothSpp.Builder()
.setConnectionListener(connectionListener)
.setDataListener(dataListener)
.setBufferSize(2048)
.build();
To connect to a BluetoothDevice
call connect(BluetoothDevice)
on bluetoothSpp object.
bluetoothSpp.connect(device);
To disconnect
bluetoothSpp.disconnect();