我有一个 iOS 应用程序,它使用 CoreBluetooth 扫描附近的信标。 但我必须检测信标是否超出范围。我已经在 android 中做了类似的事情:
@Override
public void run() {
try {
if(expirationTime <= 0) {
device.setLost(true);
if(!BeaconScanCallback.getBeaconScanCallbackInstance(activity).isInBackground())
activity.getListAdapter().removeDevice(device);
DeviceManager.getInstance().removeDevice(device);
if(getLocation() != null) {
Log.i("AUTOLOST", "Device lost: " + device.getDeviceName() + " " + getLocation().getLatitude());
activity.postDeviceLocation(device, getLocation().getLatitude(), getLocation().getLongitude(), BeaconStatus.BEACON_LOST, "Device lost");
}
} else {
expirationTime -= 1;
if(isAccepted()) {
handler.postDelayed(new AutoLost(device), expirationTimer);
}
}
} finally {
}
}
在 Android 中,即使已经扫描过一次,信标也会被扫描。因此,我可以设置一个超时方法,一旦在特定时间(1 分钟)内未扫描它,就会自动将其从数组中删除。
这是我的问题: 在 Swift 中,如果已经扫描过一次,我就无法扫描信标两次,所以我认为这种方法不会再次起作用。是否有可能检查信标是否超出范围并且不再可扫描(信标丢失)?
最佳答案
实际上,您可以从 iOS 上的 CoreBluetooth 获取对 funccentralManager(_central: CBCentralManager, didDiscovereripheral: CBPeripheral,advertisementData: [String : Any], rssi RSSI: NSNumber)
委托(delegate)方法的多个回调,如果您在开始扫描时指定CBCentralManagerScanOptionAllowDuplicatesKey
。像这样:
centralManager.scanForPeripherals(withServices: uuids,
options: [CBCentralManagerScanOptionAllowDuplicatesKey: true] )
但值得注意的是,这仅允许您在应用程序位于前台时获得同一广告的多个回调。在后台,此选项将被忽略。
使用上面的方法,可以像在 Android 中一样使用时间戳来查看一段时间内未看到信标的时间,并确定它不再处于范围内。
关于android - 快速检测信标是否超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370342/