2

我在 iBeaconProject 中使用 BroadcastReceiver 从 onReceive 接收一系列信号。我想做的是只跟踪一个信标(我指定)以及它从我的手机到信标的距离。有什么想法吗,伙计们?请帮我!我正在使用http://www.radiusnetworks.com。我使用以下 onReceive 函数获得了一系列信号。我该怎么做呢?提前谢谢大家!

BroadcastReceiver bReceiver = new BroadcastReceiver() {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        int countBea = 0; 
        if (intent.getAction().equals(intentname) && intent.getExtras() != null && intent.getExtras().containsKey(intentname)) {
            Collection<IBeacon> beaconsCol = (Collection<IBeacon>)intent.getExtras().getSerializable(intentname);

            for (IBeacon bea : beaconsCol) {

                    Log.d("beac receive!","receive! "+bea.getProximityUuid()+" "+bea.getMajor()+" "+bea.getMinor()+" "+bea.getAccuracy()+" "+bea.getProximity()+" "+bea.getRssi()+" "+bea.getTxPower());
                    countBea++;
                     if(((mainActivity)getActivity()).UUIDValue.equalsIgnoreCase(bea.getProximityUuid())
                            && ((mainActivity)getActivity()).MajorValue == bea.getMajor() 
                            && ((mainActivity)getActivity()).MinorValue == bea.getMinor()) {
                        update(bea.getProximityUuid(), +bea.getMajor(), bea.getMinor(), bea.getAccuracy());

                    } else if (((mainActivity)getActivity()).UUIDValue.equalsIgnoreCase(bea.getProximityUuid())
                            && (((mainActivity)getActivity()).MajorValue == 0 ||
                             ((mainActivity)getActivity()).MinorValue == 0)) {
                        updateNILMajorMinor();
                    } else {
                        updateMultipleBeaconsDetected();
                    }

            }
            System.out.println("COUNTBEAC " + countBea);

        }
    }
};
4

2 回答 2

2

很高兴看到 for-each 循环。

在其中,您可以识别要跟踪的信标,

for (IBeacon bea : beaconsCol) {
    //in the following if, identify the specified beacon
    // this will remain the same for every refresh
    if(bea.getProximityUuid().equals("match it here") && bea.getMajor()==major 
        && bea.getMinor()==minor){
    //now display that beacon's proximity and accuracy
    //the same code will update a textview or notification every time

    // here you will have 1 beacon at a time, can add that to a global list
    }
}  

你能给出一个具体的实施思路吗?

您的代码会定期输入 onReceive 吗?

于 2014-05-28T11:04:44.177 回答
0

我从未见过任何提及通过收听广播来使用 Radius Networks SDK。相反,他们要求您实现某些接口并将它们注册到IBeaconManager.

您可能会发现他们的代码示例很有用。该页面包含以下代码段,您可能会将其识别为与您问题中的代码等效。

public class RangingActivity extends Activity implements IBeaconConsumer, RangeNotifier {

    private static final String TAG = RangingActivity.class.getName();
    private IBeaconManager iBeaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iBeaconManager = IBeaconManager.getInstanceForApplication(this);
        iBeaconManager.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iBeaconManager.unBind(this);
    }

    @Override
    public void onIBeaconServiceConnect() {
        iBeaconManager.setRangeNotifier(this);

        try {
            // edit this to match the UUID of your beacon
            // or leave null to detect everything
            String uuid = null;

            Region region = new Region("myRangingUniqueId", uuid, null, null);
            iBeaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            Log.e(TAG, "problem while starting ranging", e);
        }
    }

    @Override 
    public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
        if (!iBeacons.isEmpty()) {
            double accuracy = iBeacons.iterator().next().getAccuracy();
            Log.i(TAG, "The first iBeacon I see is about " + accuracy + " meters away.");
        }
    }

}
于 2014-05-28T10:49:53.550 回答