I want to control Tuya bluetooth devices from background threads on Android. But I faced with problem: When I try to do it I received IllegalStateException.
I found that tuya libraries contains singleton
public class OoooO00 {
public BluetoothStateListener OooO0oo = new BluetoothStateListener() {
...
};
It has field implemented BluetoothStateListener that inherited from AbsBluetoothListener
public abstract class AbsBluetoothListener implements Callback {
public static final int MSG_INVOKE = 1;
public static final int MSG_SYNC_INVOKE = 2;
public Handler mHandler;
public Handler mSyncHandler;
public AbsBluetoothListener() {
if (Looper.myLooper() != null) {
this.mHandler = new Handler(Looper.myLooper(), this);
this.mSyncHandler = new Handler(Looper.getMainLooper(), this);
} else {
throw new IllegalStateException();
}
}
AbsBluetoothListener controls that it created on thread with looper. And throw IllegalStateException if created from thread without looper. But how you can see from source code tuya developers supose that this thread is not MainThred.
This singleton is created when some bluetooth manager function called. It means that this function must be called from Main thread or thread with looper.
But I need to call it on background threads from RX chain or coroutine.
What is right way to initialize tuya library to have ability call Bluetooth tuya api from background threads?
At this time I found only one workaround - call some bluetooth function from main thread after tuya sdk initialization. Like this:
val bleMngr = TuyaHomeSdk.getBleManager()
bleMngr.isBleLocalOnline("fakeDeviceId")
After this singleton calls all other bluetooth manager function on handler from MainThread. But it looks like not valid way.