我正在使用 ble plx 软件包进行扫描。如何获取所有设备的列表。当包不断扫描时,它会多次将同一设备添加到列表中。如果你能帮助我我会很高兴
在扫描方法中,您可以将 false 传递给allowDuplicates 字段,您的问题将得到解决。
例如:
bleManager.startDeviceScan(
[], false
)
我强烈鼓励您阅读
react-native-ble-plx
的 Wiki,因为它包含 startDeviceScan
的参数类型以及示例/示例用法...
react-native-ble-plx
我们可以看到:startDeviceScan
的维基百科:https://github.com/dotintent/react-native-ble-plx/wiki/Bluetooth-Scanning
例如
bleManager.startDeviceScan(
UUIDs: ?Array<UUID>,
options: ?ScanOptions,
listener: (error: ?Error, scannedDevice: ?Device) => void
)
注意:当前签名与此 Wiki 页面上列出的签名不匹配,但找到问题根源的过程/方法是相同的。
ScanOptions
的定义可以在这里找到:https://github.com/dotintent/react-native-ble-plx/blob/1814f4a3db334f3af4a2349f614ea9bfa3cabbb1/src/index.d.ts#L152
例如
/**
* Options which can be passed to scanning function
* @name ScanOptions
*/
export interface ScanOptions {
/**
* By allowing duplicates scanning records are received more frequently [iOS only]
*/
allowDuplicates?: boolean
/**
* Scan mode for Bluetooth LE scan [Android only]
*/
scanMode?: ScanMode
/**
* Scan callback type for Bluetooth LE scan [Android only]
*/
callbackType?: ScanCallbackType
/**
* Use legacyScan (default true) [Android only]
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setLegacy(boolean)
*/
legacyScan?: boolean
}
注意:允许重复,根据上面的签名,是iOS独有的!!
要消除重复项(在 iOS 上!!),您必须在
false
参数中将此“allowDuplicates”标志设置为 options
,如下所示:
bleManager.startDeviceScan(
[],
{ allowDuplicates: false },
(error: Error | null, scannedDevice: Device | null) => {
/** handler code **/
}
)
或者,更明确地说:
let serviceUUIDs: UUID[] = [];
let scanOptions: ScanOptions = {
allowDuplicates: false
};
let listener = (error: Error | null, scannedDevice: Device | null) => {
/** handler code **/
};
bleManager.startDeviceScan(
serviceUUIDs,
scanOptions,
listener
);