2017-04-12 43 views
0

我有一个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中,扫描一个beacon即使它已经被扫描一次。所以我可以设置一个超时方法,一旦它在一定时间内不被扫描(1分钟),就会自动从数组中移除它。

所以这里是我的问题: 在Swift中,如果它已经被扫描一次,我不能扫描两次信标,所以我不认为这种方法会再次工作。是否有可能检查信标是否超出范围并且不能再扫描(信标丢失)?

回答

0

实际上,你可以让iOS上的func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)委托方法从CoreBluetooth多个回调,如果指定CBCentralManagerScanOptionAllowDuplicatesKey当你开始扫描。像这样:

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

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

使用上述有可能因为你在Android中做的看到,当信标还没有在见过一段时间,确定不再在范围内使用时间戳。

+0

我想和这个作品。有了这个,我终于可以适应我在android中的解决方案。谢谢!有没有对信标丢失检测的本地支持? –

+0

那么,iOS上的本机信标支持都是使用CoreLocation的iBeacon。而且,是的,当匹配标识符模式的信标不再被看到时,它支持'didExit(region:region)'回调。如果你说CoreBluetooth,但是,我不知道有任何原生API时一定CBPeripheral'没有在一定时间内检测到'会告诉你的。我的推测是你没有使用iBeacon,因为如果你是使用CoreBluetooth,你将无法检测到它们,因为Apple阻止用这些API读取他们的广告。 – davidgyoung