android - 快速检测信标是否超出范围

标签 android iphone swift bluetooth-lowenergy beacon

我有一个 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/

相关文章:

ios - 是否可以创建 NSFetchedResultsController 来获取特定属性具有不同值的所有实体?

android - 如何使 ListView 动画化以逐一显示?

android - Apache Cordova 在 div 中加载外部 URL 或网站

android - 使用 Jetpack 的 Android 导航组件销毁/重新创建的 fragment

iphone - 在现有 iPhone 项目上将编译器更改为 llvm-clang

iphone - 本地化 Nib 无法在 iPhone 项目中工作

android - 使用 JSON Body 执行 HTTPPost 请求,Android

iPhone UITabBarController 内存管理

ios - 修改 iPhone 相机/手电筒?

ios - UIBarButtonItem 不能快速工作 3.0