2015-04-06 84 views
1

我希望应用只在“立即”范围内看到信标。在其中一篇文章中(我没有链接),我读了诸如Immediate/Near/Far之类的字符串与altbeacon或其他类似的字符,因此建议使用beacon.getDistance() < 0.5来实现远程信标。但不幸的是我没有任何线索如何实现。Altbeacon - 仅在IMMEDIATE范围内检测到信标,并丢弃此范围以外的任何信息

我试着用一篇文章提出的以下代码来找到最短距离的信标,但似乎无法正常工作(最有可能是因为波动的rssi和通过将信标保持在相距很近的距离进行测试)知道为什么他们要min = Integer.MAX_VALUE ....但我期待ATLEAST一些结果)

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
       Object[] beaconArray = beacons.toArray(); 

       //find the beacon with shortest distance 
       int count=-1; //when no beacon is there 
       int min = Integer.MAX_VALUE; 

       for (int i=0; i < beaconArray.length; i++){ 
        int d=((Beacon)beaconArray[i]).getRssi(); 
         if(d < min){ 
          min=d; 
          count=i; //1st beacon in the array 
         } 
       } 

       //play with the beacon at the shortest distance 
       uuid = ((Beacon)beaconArray[count]).getId1().toString(); 

一些技巧将是一个祝福我。

回答

1

术语immediate,near和far是在iOS中实现的任意距离范围桶。我们将它们放在2.0 Android信标库中,因为这些术语很模糊,以米为单位实现特定的距离桶非常容易。这里是你如何做同等功能:

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
    for (Beacon beacon: beacons) { 
     if (beacon.getDistance() < 0.5) { 
      // This beacon is immediate (< 0.5 meters) 
     } 
     else if(beacon.getDistance() < 3.0) { 
      // This beacon is near (0.5 to 3 meters) 
     } 
     else { 
      // This beacon is far (> 3 meters) 
     }  
    } 
}