Open neone35 opened 3 years ago
Hi,
Have you tried to register Broadcast Receiver with this intent filter BluetoothDevice.ACTION_ACL_DISCONNECTED?
You can find an example in MainActivity.java
Thanks. Will try soon and report back
Thanks, it works with this code:
private fun setPosListener() {
mPOSHandler.setConnectionListener {
mPosIsConnected = true
try {
unregisterReceiver(mBTStateBroadcastReceiver)
} catch (e: Exception) {
Logger.d("BT state broadcast receiver not registered: ${e.localizedMessage}")
}
invalidateOptionsMenu()
}
}
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
if (!mPosIsConnected) {
menu.findItem(R.id.menu_option_pos).icon =
ContextCompat.getDrawable(this, R.drawable.ic_calc_gray_24dp)
} else {
menu.findItem(R.id.menu_option_pos).icon =
ContextCompat.getDrawable(this, R.drawable.ic_calc_active_24dp)
}
return super.onPrepareOptionsMenu(menu)
}
private fun watchForBTStateBroadcasts() {
val btStateFilter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
mBTStateBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
val state = intent.getIntExtra(
BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR
)
when (state) {
BluetoothAdapter.STATE_OFF -> mBluetoothAdapter.enable()
}
}
}
}
registerReceiver(mBTStateBroadcastReceiver, btStateFilter)
}
private fun watchForPosBroadcasts() {
mPosIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
mPosIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
mPosBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Logger.d("Received pos broadcast ${intent.action}")
when (intent.action) {
BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
val device =
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
if (POSHandler.getInstance().connectedDevice != null && device != null &&
POSHandler.getInstance().connectedDevice.address.equals(
device.address,
ignoreCase = true
)
) {
mPosIsConnected = false
mBluetoothAdapter.disable()
watchForBTStateBroadcasts()
invalidateOptionsMenu()
}
}
}
}
}
registerReceiver(mPosBroadcastReceiver, mPosIntentFilter)
}
Didn't manage to unpair device, so I restart BT when device is disconnected. Then on new connection attempt it's removed from connected list. Maybe there is correct way to unpair without restarting BT?
I have indicator in appbar which shows current connection status (active or disconnected).
Currently when terminal has disconnected because of high distance from android device, indicator still shows that connection is active and it's not possible to update indicator into disconnected state with POSHandler.getInstance().isConnected.
Here's my main code which runs every 15s and after setPOSReadyListener triggered. First option with ic_calc_gray_24dp is never executed: