首先安卓手机要支持蓝牙5.0
在未设置搜索广播类型时,是无法搜索到扩展广播的
val scanSettings = ScanSettings.Builder()
.setLegacy(false) // 可以搜索到扩展广播
Kotlin
val scanSettings = ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // 设置扫描模式
.setLegacy(false) // 可以搜索到扩展广播
.setReportDelay(0) // 实时报告,0 表示无延迟
.setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT) // 设置匹配数量
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // 设置回调类型
.build()
// 创建ScanFilter实例(如果需要特定的过滤条件)
val filter = ScanFilter.Builder()
// .setServiceUuid(ParcelUuid.fromString("018efc9d-6b58-79d2-bc8f-0be0388883bb"))
// .setServiceUuid(ParcelUuid.fromString("0000FFF3-0000-1000-8000-00805F9B34FB"))
.build()
mBluetoothAdapter =
(getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
// mBluetoothAdapter.bluetoothLeScanner.startScan(scanCallback)
mBluetoothAdapter.bluetoothLeScanner.startScan(listOf(filter), scanSettings, scanCallback)