A library to manage connection between two devices using bluetooth
Step 1. Add the JitPack repository to your root build file.allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
} Step 2. Add the dependency to your app level gradle build file.
dependencies {
implementation 'com.github.thecoderpb:Bluetooth-Connection-Library:<version>'
}Current <version> is 1.2.3
Create an instance of the class BluetoothConnectionManager.BluetoothConnectionManagerbtConnManger = newBluetoothConnectionManager(Contextcontext,BluetoothAdapteradapter)1. getAdapter() | returns bluetoothAdapter
2. isEnabled() | returns a boolean checking if BT is enabled or not
3. getPairedDevices() | returns a list of bonded BT devices List<BluetoothDevice>
4. getSocket() | returns connected BluetoothSocket
5. disconnect() | closes connection
Setup a server to listen for communications
initConnectionAccept(OnBluetoothConnectlistener)Establish communication with server
1.connect(Bluetoothdevice, OnBluetoothConnectlistener )
2.connect(StringdeviceAddress, OnBluetoothConnectlistener)
3.connect(byte[] byteAddress, OnBluetoothConnectlistener )
4.connect(BluetoothSocketsocket, OnBluetoothConnectlistener )publicinterfaceOnBluetoothConnect {
voidconnectedStream(BluetoothMessageServiceservice);
voidonReceive(Stringmessage,Objectobject);
}Once the device obtains the socket, it opens connection through the socket and this class helps in sending data to the remote device
NOTE: Do NOT instantiate the class and directly send message using the sendMessage() method. Use the service obtained from interface call onRecieve. (Refer Sample Code Block)
BluetoothMessageServiceservice;
...
@OverridepublicvoidconnectedStream(BluetoothMessageServiceservice) {
this.service = service;
//ORservice.sendMessage("Message"); //Sends data to target device
}
@OverridepublicvoidonReceive(Stringmessage,Objectobject){
if(message != null) //check for null safety textview.setText(message);
//If message is sent as a string, onReceive will have the message as String as well as object//If message is sent as an object, onReceive will have the message as only object and String message will be null
}1. sendMessage(String message); 2. sendMessage(Object object); //Object must be serializable,else the method silently fails
BluetoothConnectionManager.getDeviceStatus(); //retrieves device status as client or serverDefault return value is -1
FLAGS
BluetoothConnectionManager.CLIENT// 0BluetoothConnectionManager.SERVER// 1All connect() and sendMessage() methods silently fails if parameters sent are incorrect/cannot be parsed.
publicclassMainActivityextendsAppCompatActivityimplementsOnBluetoothConnect, View.OnClickListener {
// A basic chat appBluetoothConnectionManagerbtManager;
BluetoothMessageServiceservice;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btManager = newBluetoothConnectionManager(this, BluetoothAdapter.getDefaultAdapter());
Buttonbutton = findViewById(R.id.button);
button.setOnClickListener(this);
if (btManager.isEnabled())
btManager.initConnectionAccept(this);
elseLog.i(TAG, "Enable bt");
for (BluetoothDevicedevice : btManager.getPairedDevices()) {
Log.i(TAG, device.getAddress() + " " + device.getName());
if (device.getAddress().equals("0C:E0:DC:2E:80:53")) //Enter your device addressbtManager.connect(device, this);
}
}
@OverridepublicvoidconnectedStream(BluetoothMessageServiceservice) {
this.service = service; // get the message service
}
@OverridepublicvoidonReceive(Stringmessage,Objectobject) {
Log.i(TAG, "message sent from device " + message);
}
@OverridepublicvoidonClick(Viewv) {
if(BluetoothConnectionManager.getDeviceStatus() == BluetoothConnectionManager.CLIENT) //only client will be able to send messageservice.sendMessage(msg);
}
}Copyright (c) 2020 thecoderpb
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* thecoderpb wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. * ----------------------------------------------------------------------------
*/