I have had the opportunity to work before with android and devices via bluetooth connection. Considering that you do not specify much about the device, I recommend that you look for the official documentation of it, but here is an example made by me at some time.
I'm going to do the example with a bixolon printer, of which there is very little documentation:
Each device has its methods and libraries to import, for example: com.bixolon.printer.BixolonPrinter and android.bluetooth.BluetoothDevice
Once you have that you must make a DeviceProvider.java class and implement some callbacks:
public interface ConnectionCallback {
void onPrinterConnected();
void onPrinterNotFound();
void onConnectingPrinter();
void onPrinterNameFound(String printerName);
}
This will help you manage the events that happen with the device, the library of the device must have a class with constants that inform you of events, states and methods:
Lastly, in the DeviceProvider, you must implement a thread that listens constantly, in the process initialize the Bluetooth of the cell phone and manage according to the responses of the device:
private PrinterProvider(Context context, BaseSchedulerProvider schedulerProvider) {
mBixolonPrinter = new BixolonPrinter(context, handler, null);
mFindPrinterSubscription = Observable.create(e -> {
int scanned = 0;
while (mPrinterScanningRunning) {
if (!mConnectedPrinter) {
if (scanned < 20) {
mBixolonPrinter.findBluetoothPrinters();
scanned++;
} else {
scanned = 0;
mBixolonPrinter.disconnect();
}
}
Thread.sleep(1000);
}
})
.subscribeOn(schedulerProvider.computation())
.subscribe();
}
This would be the device's status handler:
private final Handler handler = new Handler(msg -> {
switch (msg.what) {
case BixolonPrinter.MESSAGE_STATE_CHANGE: {
switch (msg.arg1) {
case BixolonPrinter.STATE_CONNECTED:
mConnectedPrinter = true;
String printerName = msg.getData().getString(BixolonPrinter.KEY_STRING_DEVICE_NAME);
for (ConnectionCallback callback : mConnectionCallbacks) {
callback.onPrinterConnected();
mPrintername=printerName;
}
break;
case BixolonPrinter.STATE_CONNECTING:
mConnectedPrinter = false;
for (ConnectionCallback callback : mConnectionCallbacks)
callback.onConnectingPrinter();
break;
case BixolonPrinter.STATE_NONE:
mConnectedPrinter = false;
for (ConnectionCallback callback : mConnectionCallbacks)
callback.onPrinterNotFound();
break;
}
break;
}
case BixolonPrinter.MESSAGE_BLUETOOTH_DEVICE_SET:
if (msg.obj == null) {
mConnectedPrinter = false;
for (ConnectionCallback callback : mConnectionCallbacks)
callback.onPrinterNotFound();
} else {
Set<BluetoothDevice> pairedDevices = (Set<BluetoothDevice>) msg.obj;
for (BluetoothDevice device : pairedDevices) {
if (!pairedPrinters.contains(device.getAddress())) {
pairedPrinters.add(device.getAddress());
}
if (pairedPrinters.size() == 1) {
mBixolonPrinter.connect(pairedPrinters.get(0));
}
}
}
break;
case BixolonPrinter.MESSAGE_DEVICE_NAME:
String printerName = msg.getData().getString(BixolonPrinter.KEY_STRING_DEVICE_NAME);
for (ConnectionCallback callback : mConnectionCallbacks)
callback.onPrinterNameFound(printerName);
break;
case BixolonPrinter.MESSAGE_USB_DEVICE_SET:
if (msg.obj == null) {
for (ConnectionCallback callback : mConnectionCallbacks)
callback.onPrinterNotFound();
}
break;
}
return false;
});
At the time it was the most orderly I could implement, if you look for the documentation of your device you can do something like that and you would not have problems with cache.