Swift 检测信标是否超出范围

Swift detect if beacon is out of range

我有一个 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 获得多个回调到 func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) 委托方法,如果 当您指定 CBCentralManagerScanOptionAllowDuplicatesKey开始扫描。像这样:

centralManager.scanForPeripherals(withServices: uuids, 
        options: [CBCentralManagerScanOptionAllowDuplicatesKey: true] )

但值得注意的是,这只允许您在应用程序处于前台时获得同一广告的多个回调。在后台,这个选项被忽略。

使用上面的方法,可以像在 Android 中那样使用时间戳来查看一段时间内没有看到信标并确定它不再在范围内。