Skip to content

Instantly share code, notes, and snippets.

@Xiryl
Last active August 12, 2021 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xiryl/d7a0e43cd0ec9b905e2da38e269d6243 to your computer and use it in GitHub Desktop.
Save Xiryl/d7a0e43cd0ec9b905e2da38e269d6243 to your computer and use it in GitHub Desktop.
BluetoothSDK-ListenerHelper
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