gpt4 book ai didi

android - 扫描特定 BLE 设备的问题

转载 作者:行者123 更新时间:2023-12-03 20:40:00 25 4
gpt4 key购买 nike

我正在研究 BLE,必须使用特定的 udid 扫描 BLE 设备。
目前,所有支持 BLE 的设备均作为扫描结果提供。
为了避免和仅扫描特定设备,我所做的如下:

fun startScanDevices(interval: Long = Constants.DEFAULT_SCAN_TIME_OUT,
connectedDevices: HashMap<String, BLEDeviceModel>? = null,
scanWithId: String? = null, context: Activity? = null) {
this.context = context
if (scanWithId != null)
this.scanWithId = scanWithId
if (isBluetoothEnabled) {
clearDeviceList(connectedDevices)
val BLP_SERVICE_UUID = UUID.fromString(UUID_OF_DEVICE)
val serviceUUIDs = arrayOf<UUID>(BLP_SERVICE_UUID)
var filters: MutableList<ScanFilter?>? = null
if (serviceUUIDs != null) {
filters = ArrayList()
for (serviceUUID in serviceUUIDs) {
val filter: ScanFilter = ScanFilter.Builder()
.setServiceUuid(ParcelUuid(serviceUUID))
.build()
filters!!.add(filter)
}
}
val scanSettings: ScanSettings = ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_POWER)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE)
.setNumOfMatches(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT)
.setReportDelay(1L)
.build()
mBluetoothAdapter?.bluetoothLeScanner?.startScan(filters,scanSettings,scanCallback)
mScanning = true
handler.postDelayed(stopScanningRunnable, interval)
}
}
现在,在上述方法中,您可以验证我是否通过了 过滤器 扫描设置 作为 的参数开始扫描 方法。
但是有了这两个论点,我得到了以下错误:

2021-05-06 11:16:24.295 14669-14669/? E/AndroidRuntime: FATALEXCEPTION: main Process: com.dev, PID: 14669java.lang.IndexOutOfBoundsException: Index: 0, Size: 0at java.util.ArrayList.get(ArrayList.java:437)at com.ble.BLEManager$scanCallback$1.onBatchScanResults(BLEManager.kt:153)at android.bluetooth.le.BluetoothLeScanner$BleScanCallbackWrapper$2.run(BluetoothLeScanner.java:627)at android.os.Handler.handleCallback(Handler.java:938)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:246)at android.app.ActivityThread.main(ActivityThread.java:8506)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)


这与 中的单个参数一起工作正常开始扫描() 如下 - 没有收到此错误:
mBluetoothAdapter?.bluetoothLeScanner?.startScan(scanCallback)
因此,错误在以下方法中:
override fun onBatchScanResults(results: List<ScanResult>) {
results[0].device
super.onBatchScanResults(results)
}
编辑
删除了这个不必要的方法,名为: onBatchScanResults
因为我使用 扫描结果 .
现在,错误不会出现,但无法使用过滤器和 scanSettings 扫描任何设备。
可能是什么问题?请指导。谢谢。

最佳答案

步骤 1) 像这样添加过滤器:
String[] filterNames = intent.getStringArrayExtra(FILTER_NAME_LIST);
String[] filterMacs = intent.getStringArrayExtra(FILTER_MAC_LIST);
String[] 忽略Macs =
Intent .getStringArrayExtra(IGNORE_MAC_LIST);
mFilter = new ScanServiceFilter.Builder()
.filterNames(filterNames)
.filterMacs(过滤器Macs)
.ignoreMacs(忽略Macs)
。 build ();
第 2 步:调用 scanLeDevice()
扫描乐设备(真);
第三步: scanLeDevice 创 build 置

                 ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(CALLBACK_TYPE_ALL_MATCHES)
.setMatchMode(MATCH_MODE_AGGRESSIVE)
.setNumOfMatches(MATCH_NUM_FEW_ADVERTISEMENT)
.setReportDelay(0)
.build();

// We only want to scan for devices advertising our custom service
ScanFilter scanFilter = new ScanFilter.Builder()
.setServiceUuid(new ParcelUuid(UUIDConstants.RX_SERVICE_UUID)).build();
if (mBluetoothLeScanner != null && mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
mBluetoothLeScanner.startScan(Collections.singletonList(scanFilter), settings, mScanCallback);
broadcastAction(new ScanBleActionEvent(ScanBleActionEvent.Actions.SCAN_STARTED));
} else {
bluetoothDeviceStopped();
}
第 4 步:在 ScanCallback 检查
    if (mFilter.passesFilter(scanDevice)) {
filteredResults.add(scanDevice);
broadcastAction(new ScanBleActionEvent(ScanBleActionEvent.Actions.SCANNED_DEVICE, scanDevice));
}

关于android - 扫描特定 BLE 设备的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67412584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com