Skip to content

Instantly share code, notes, and snippets.

val discoverableIntent =Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
discoverableIntent
.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0/*set a duration, 0 is not recommended*/)
startActivity(discoverableIntent)
private fun scanLeDevice(enable: Boolean) {
when (enable) {
true -> {
// Stops scanning after a pre-defined scan period.
Handler().postDelayed({
mScanning = false
bluetoothAdapter?.bluetoothLeScanner?.stopScan(mLeScanCallback)
}, 5000)
mScanning = true
bluetoothAdapter?.bluetoothLeScanner?.startScan(mLeScanCallback)
private fun allowLocationDetectionPermissions() {
if (ContextCompat.checkSelfPermission(this@MainActivity, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(this@MainActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), FINE_LOCATION_PERMISSION_REQUEST)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {