1 year ago
#287787
Baran Gungor
BLE Scan Started but doesn't find any device
I'm trying create a plugin that scan BLE devices for Flutter. I did it in Kotlin and i started scan from Flutter via MethodChannel. I saw results in console on Android 11 but i can't see in Android 12.
I saw this logs in Android 12 :
D/BluetoothAdapter: isLeEnabled(): ON
D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=8 mScannerId=0
That's my code:
//Permissions check
private fun checkPermissions(binding: ActivityPluginBinding): String {
if(bluetoothAdapter != null){
val requiredPermissions = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
listOf(Manifest.permission.ACCESS_FINE_LOCATION)
} else {
listOf(Manifest.permission.BLUETOOTH_CONNECT, Manifest.permission.BLUETOOTH_SCAN)
}
val missingPermissions = requiredPermissions.filter { permission ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
binding.activity.applicationContext.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED
} else {
TODO("VERSION.SDK_INT < M")
}
}
Log.v("REQPERMISSIONSS",requiredPermissions.toString())
if (missingPermissions.isEmpty()) {
return "${bluetoothAdapter.isEnabled}"
} else {
Log.v("MISSINGPERMISSIONSS",missingPermissions.toString())
requestPermissions(binding.activity, missingPermissions.toTypedArray(), BLUETOOTH_PERMISSION_REQUEST_CODE)
}
}
return ""
}
//Start scan for BLE Devices
private fun startBeaconScan() {
val scanSetting = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
if (!scanning) {
scanning = true
bluetoothAdapter.bluetoothLeScanner.startScan(null,scanSetting, scanCallBack)
} else {
scanning = false
bluetoothAdapter.bluetoothLeScanner.stopScan(scanCallBack)
}
Log.v("ISSCANNING",scanning.toString())
}
private val scanCallBack : ScanCallback by lazy {
object: ScanCallback(){
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
super.onBatchScanResults(results)
Log.v("SCANNER","onScanRESULT $results")
}
override fun onScanResult(callbackType: Int, result: ScanResult?) {
var device = result?.device
if(device!=null&& eventSink!=null){
eventSink!!.success(device)
}
Log.v("SCANNER","device name is ${device?.address}")
}
override fun onScanFailed(errorCode: Int) {
Log.v("SCANNER","onScanFailed $errorCode")
}
}
}
android
flutter
kotlin
bluetooth-lowenergy
beacon
0 Answers
Your Answer