I cannot find any mobile phone with bluetooth search (I mean available unpaired devices). I have this code to search devices:
fun searchDevices() {
val adapter = BluetoothAdapter.getDefaultAdapter()
val scanner = adapter.bluetoothLeScanner
val scanSettingsBuilder = ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.setReportDelay(0L)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scanSettingsBuilder
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
}
val scanSettings = scanSettingsBuilder.build()
val scanCallback: ScanCallback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
val device: BluetoothDevice = result.device
val deviceName = device.name ?: result.scanRecord?.deviceName
Log.d(TAG, "Available device name: $deviceName")
// ...do whatever you want with this found device
}
override fun onBatchScanResults(results: List<ScanResult?>?) {
Log.d(TAG, "results: $results")
}
override fun onScanFailed(errorCode: Int) {
Log.d(TAG, "Scan failed, error code: $errorCode")
}
}
if (scanner != null) {
scanner.startScan(null, scanSettings, scanCallback)
Log.d(TAG, "scan started")
} else {
Log.e(TAG, "could not get scanner object")
}
}
In manifest I have these permissions:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Each persmission is granted. But when I'm trying to search, I cannot find any MOBILE device. I can find two TV, smart clock, even kettle in my kitchen. But no mobile phones (two enabled for bluetooth search). I can find these phones from OS, but not from my code. Why? Please help!