Last active
September 26, 2021 09:56
-
-
Save pritam-quantum-inventions/fc2fdf6e203921c3aac5179a8a6b7e76 to your computer and use it in GitHub Desktop.
find the bluetooth devices available
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
private fun discoverDevices(result: MethodChannel.Result) { | |
deviceFoundList.clear() | |
val list: MutableList<String> = arrayListOf() | |
val s = JSONObject() | |
val myAdapter = BluetoothAdapter.getDefaultAdapter() | |
myAdapter.startDiscovery() | |
val filter = IntentFilter(BluetoothDevice.ACTION_FOUND) | |
receiver = object : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent) { | |
when (intent.action) { | |
BluetoothDevice.ACTION_FOUND -> { | |
val device: BluetoothDevice? = if(intent.hasExtra(BluetoothDevice.EXTRA_DEVICE)) | |
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) else | |
null | |
device?.let {bt -> | |
s.put("name", bt.name) | |
s.put("address", bt.address) | |
s.put("bondState", bt.bondState.toString()) | |
s.put("type", bt.type.toString()) | |
bt.uuids?.let { | |
if(it.isNotEmpty()) { | |
s.put("uuid", it[0].uuid.toString()) | |
} | |
} | |
deviceFoundList.add(bt) | |
list.add(s.toString()) | |
} | |
} | |
BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> { | |
result.success(list.toList()) | |
} | |
"" -> println("broadcast receiver intent.action has no attribute") | |
null -> println("broadcast receiver intent.action was null") | |
} | |
} | |
} | |
registerReceiver(receiver, filter) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment