Last active
August 12, 2021 09:34
-
-
Save Xiryl/d7a0e43cd0ec9b905e2da38e269d6243 to your computer and use it in GitHub Desktop.
BluetoothSDK-ListenerHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BluetoothSDKListenerHelper { | |
companion object { | |
private var mBluetoothSDKBroadcastReceiver: BluetoothSDKBroadcastReceiver? = null | |
class BluetoothSDKBroadcastReceiver : BroadcastReceiver() { | |
private var mGlobalListener: IBluetoothSDKListener? = null | |
public fun setBluetoothSDKListener(listener: IBluetoothSDKListener) { | |
mGlobalListener = listener | |
} | |
public fun removeBluetoothSDKListener(listener: IBluetoothSDKListener): Boolean { | |
if (mGlobalListener == listener) { | |
mGlobalListener = null | |
} | |
return mGlobalListener == null | |
} | |
override fun onReceive(context: Context?, intent: Intent?) { | |
val device = | |
intent!!.getParcelableExtra<BluetoothDevice>(BluetoothUtils.EXTRA_DEVICE) | |
val message = intent.getStringExtra(BluetoothUtils.EXTRA_MESSAGE) | |
when (intent.action) { | |
BluetoothUtils.ACTION_DEVICE_FOUND -> { | |
mGlobalListener!!.onDeviceDiscovered(device) | |
} | |
BluetoothUtils.ACTION_DISCOVERY_STARTED -> { | |
mGlobalListener!!.onDiscoveryStarted() | |
} | |
BluetoothUtils.ACTION_DISCOVERY_STOPPED -> { | |
mGlobalListener!!.onDiscoveryStopped() | |
} | |
BluetoothUtils.ACTION_DEVICE_CONNECTED -> { | |
mGlobalListener!!.onDeviceConnected(device) | |
} | |
BluetoothUtils.ACTION_MESSAGE_RECEIVED -> { | |
mGlobalListener!!.onMessageReceived(device, message) | |
} | |
BluetoothUtils.ACTION_MESSAGE_SENT -> { | |
mGlobalListener!!.onMessageSent(device) | |
} | |
BluetoothUtils.ACTION_CONNECTION_ERROR -> { | |
mGlobalListener!!.onError(message) | |
} | |
BluetoothUtils.ACTION_DEVICE_DISCONNECTED -> { | |
mGlobalListener!!.onDeviceDisconnected() | |
} | |
} | |
} | |
} | |
public fun registerBluetoothSDKListener( | |
context: Context?, | |
listener: IBluetoothSDKListener | |
) { | |
if (mBluetoothSDKBroadcastReceiver == null) { | |
mBluetoothSDKBroadcastReceiver = BluetoothSDKBroadcastReceiver() | |
val intentFilter = IntentFilter().also { | |
it.addAction(BluetoothUtils.ACTION_DEVICE_FOUND) | |
it.addAction(BluetoothUtils.ACTION_DISCOVERY_STARTED) | |
it.addAction(BluetoothUtils.ACTION_DISCOVERY_STOPPED) | |
it.addAction(BluetoothUtils.ACTION_DEVICE_CONNECTED) | |
it.addAction(BluetoothUtils.ACTION_MESSAGE_RECEIVED) | |
it.addAction(BluetoothUtils.ACTION_MESSAGE_SENT) | |
it.addAction(BluetoothUtils.ACTION_CONNECTION_ERROR) | |
it.addAction(BluetoothUtils.ACTION_DEVICE_DISCONNECTED) | |
} | |
LocalBroadcastManager.getInstance(context!!).registerReceiver( | |
mBluetoothSDKBroadcastReceiver!!, intentFilter | |
) | |
} | |
mBluetoothSDKBroadcastReceiver!!.setBluetoothSDKListener(listener) | |
} | |
public fun unregisterBluetoothSDKListener( | |
context: Context?, | |
listener: IBluetoothSDKListener | |
) { | |
if (mBluetoothSDKBroadcastReceiver != null) { | |
val empty = mBluetoothSDKBroadcastReceiver!!.removeBluetoothSDKListener(listener) | |
if (empty) { | |
LocalBroadcastManager.getInstance(context!!) | |
.unregisterReceiver(mBluetoothSDKBroadcastReceiver!!) | |
mBluetoothSDKBroadcastReceiver = null | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment