|
package com.webserveis.mqtthomepresence.helpers |
|
|
|
import android.bluetooth.BluetoothAdapter |
|
import android.bluetooth.BluetoothDevice |
|
import android.content.BroadcastReceiver |
|
import android.content.Context |
|
import android.content.Intent |
|
import android.content.IntentFilter |
|
import android.util.Log |
|
|
|
class BluetoothHelper(val context: Context) { |
|
companion object : SingletonHolder<BluetoothHelper, Context>(::BluetoothHelper) |
|
|
|
private val mBtAdapter by lazy { |
|
BluetoothAdapter.getDefaultAdapter()?.let { |
|
return@lazy it |
|
} ?: run { |
|
throw RuntimeException( |
|
"Bluetooth is not supported on this hardware platform. " + |
|
"Make sure you try it from the real device\n " + |
|
"You could more information from here:\n" + |
|
"https://developer.android.com/reference/android/bluetooth/BluetoothAdapter" |
|
) |
|
} |
|
} |
|
private val mStateReceiver: BluetoothStateReceiver = BluetoothStateReceiver() |
|
private val mDiscoveryReceiver: BluetoothDiscoveryReceiver = BluetoothDiscoveryReceiver() |
|
|
|
var listener: BluetoothHelperListener? = null |
|
|
|
private val deviceList: MutableList<BluetoothDevice> = arrayListOf() |
|
|
|
fun isEnabled(): Boolean { |
|
return mBtAdapter.isEnabled |
|
} |
|
|
|
fun isEnabled(value: Boolean) { |
|
if (value) |
|
enable() |
|
else |
|
disable() |
|
} |
|
|
|
fun enable() { |
|
if (!mBtAdapter.isEnabled) mBtAdapter.enable() |
|
} |
|
|
|
fun disable() { |
|
if (mBtAdapter.isEnabled) mBtAdapter.disable() |
|
} |
|
|
|
fun getFoundDeviceList(): List<BluetoothDevice> { |
|
return deviceList.toList() |
|
} |
|
|
|
fun isDiscovering(): Boolean = mBtAdapter.isDiscovering |
|
|
|
fun register() { |
|
mStateReceiver.register(context) |
|
mDiscoveryReceiver.register(context) |
|
} |
|
|
|
fun unregister() { |
|
mStateReceiver.unregister(context) |
|
mDiscoveryReceiver.unregister(context) |
|
} |
|
|
|
fun startDiscovery(): Boolean = if (mBtAdapter.isEnabled) { |
|
deviceList.clear() |
|
mBtAdapter.startDiscovery() |
|
true |
|
} else false |
|
|
|
|
|
fun stopDiscovery() { |
|
if (mBtAdapter.isEnabled && mBtAdapter.isDiscovering) { |
|
mBtAdapter.cancelDiscovery() |
|
} |
|
} |
|
|
|
private inner class BluetoothStateReceiver : BroadcastReceiver() { |
|
|
|
fun register(context: Context) { |
|
|
|
val filter = IntentFilter() |
|
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED) |
|
context.registerReceiver(this, filter) |
|
|
|
} |
|
|
|
fun unregister(context: Context) { |
|
//safeUnregister |
|
try { |
|
context.unregisterReceiver(this) |
|
} catch (e: IllegalArgumentException) { |
|
Log.w("BluetoothHelper", "This receiver was not registered") |
|
} |
|
} |
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
Log.d( |
|
"BluetoothHelper", |
|
"onReceive() called with: context = [$context], intent = [$intent]" |
|
) |
|
if (intent?.action == BluetoothAdapter.ACTION_STATE_CHANGED) { |
|
|
|
val previousState = |
|
intent.getIntExtra( |
|
BluetoothAdapter.EXTRA_PREVIOUS_STATE, |
|
BluetoothAdapter.ERROR |
|
) |
|
val state = |
|
intent.getIntExtra( |
|
BluetoothAdapter.EXTRA_STATE, |
|
BluetoothAdapter.ERROR |
|
) |
|
|
|
if (previousState == BluetoothAdapter.STATE_OFF || previousState == BluetoothAdapter.STATE_TURNING_OFF) { |
|
if (state == BluetoothAdapter.STATE_ON || state == BluetoothAdapter.STATE_TURNING_ON) { |
|
listener?.onBluetoothEnabled() |
|
|
|
} else if (state == BluetoothAdapter.STATE_OFF || state == BluetoothAdapter.STATE_TURNING_OFF) { |
|
listener?.onBluetoothDisabled() |
|
} |
|
} |
|
} |
|
|
|
|
|
} |
|
|
|
} |
|
|
|
private inner class BluetoothDiscoveryReceiver : BroadcastReceiver() { |
|
fun register(context: Context) { |
|
val filter = IntentFilter() |
|
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED) |
|
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED) |
|
filter.addAction(BluetoothDevice.ACTION_FOUND) |
|
context.registerReceiver(this, filter) |
|
} |
|
|
|
fun unregister(context: Context) { |
|
//safeUnregister |
|
try { |
|
context.unregisterReceiver(this) |
|
} catch (e: IllegalArgumentException) { |
|
Log.w("BluetoothHelper", "This receiver was not registered") |
|
} |
|
} |
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
Log.d( |
|
"BluetoothHelper", |
|
"onReceive() called with: context = [$context], intent = [$intent]" |
|
) |
|
|
|
when (intent?.action) { |
|
BluetoothAdapter.ACTION_DISCOVERY_STARTED -> listener?.onStartDiscovery() |
|
BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> listener?.onFinishedDiscovery() |
|
BluetoothDevice.ACTION_FOUND -> { |
|
val device = |
|
intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE) |
|
device?.let { |
|
if (it !in deviceList) { |
|
listener?.onFoundDevice(it) |
|
deviceList.add(it) |
|
} |
|
} |
|
} |
|
} |
|
|
|
} |
|
} |
|
|
|
interface BluetoothHelperListener { |
|
fun onBluetoothEnabled() |
|
fun onBluetoothDisabled() |
|
fun onStartDiscovery() |
|
fun onFinishedDiscovery() |
|
fun onFoundDevice(device: BluetoothDevice?) |
|
} |
|
} |