Reputation: 2719
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!
Upvotes: 1
Views: 113
Reputation: 9087
Here's some Kotlin code from the project I'm working on right now which shows you how to iterate through paired BT devices on your phone.
private fun GetPairedDevices(btAdapter: BluetoothAdapter): MutableSet<BluetoothDevice> {
val pairedDevices = btAdapter?.bondedDevices
// If there are paired devices
Log.i("FirstFrag", "checking paired devices")
if (pairedDevices != null) {
Log.i("FirstFrag", "pairedDevices NOT null")
if (pairedDevices.size > 0) {
Log.i("FirstFrag", pairedDevices.size.toString())
for (device in pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
adapter!!.add(device.name) // + "\n" + device.getAddress());
}
adapter!!.notifyDataSetChanged()
}
}
return pairedDevices
}
Here's what the call to the function looks like:
btAdapter = BluetoothAdapter.getDefaultAdapter()
pairedDevices = GetPairedDevices(btAdapter)
If you want to see this work very fast then get my entire project from my github repo and run it.
The first thing the app does when you run it is get the paired devices and fill the drop list. You'll see your phones appear in there.
This project is used to connect to HC-05 devices but it will find all your paired devices. I just tried it with two of my android phones and my phone showed up in the drop list.
Some Additional Info
I run this code from a fragment in the onViewCreated() method.
The adapter
is just a ArrayAdapter<String>?
which I use to display the device.name
in a Spinner
.
If you want to try the method very quickly just remove the adapter stuff and add a Log.i("FirstFrag", device.name)
and watch your logcat.
Upvotes: 2