Skip to content

Instantly share code, notes, and snippets.

@aya-salama1989
Last active April 9, 2020 19:41
Show Gist options
  • Save aya-salama1989/30842661768a7ec6df996df18e0fdc91 to your computer and use it in GitHub Desktop.
Save aya-salama1989/30842661768a7ec6df996df18e0fdc91 to your computer and use it in GitHub Desktop.
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)
}
else -> {
mScanning = false
bluetoothAdapter?.bluetoothLeScanner?.stopScan(mLeScanCallback)
}
}
}
private var mLeScanCallback: ScanCallback =
object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult?) {
super.onScanResult(callbackType, result)
//Do your thing
}
override fun onBatchScanResults(results: List<ScanResult?>?) {
super.onBatchScanResults(results)
//Do your thing
}
override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
//Do your thing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment