【问题标题】:Android iBeacon Receiving Single Specified SignalAndroid iBeacon 接收单个指定信号
【发布时间】:2014-05-28 10:44:58
【问题描述】:

我在 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);

        }
    }
};

【问题讨论】:

    标签: android bluetooth broadcastreceiver ibeacon-android


    【解决方案1】:

    很高兴看到 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?

    【讨论】:

    • 嘿!感谢您的回复!我确实尝试添加 if 条件,但它会不断刷新,然后接收另一批信号,然后它不会跟踪我指定的那个特定信号。
    • 嘿..如果你只匹配一个,你想识别的信标:) 你能检查你想识别的信标的详细信息吗..主要,次要,uuid
    • 是的,我做了,但是我的代码检查是否检测到额外的信标,然后它会显示检测到多个信标。
    • 我正在使用分片,所以 onReceive 在分片接口,因为接收到的信标信号将在不同分片之间共享。
    • 你可以使用一个全局变量(或更多信标的列表),它将在片段之间共享,但只有你想要的信标的数据
    【解决方案2】:

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

    您可能会发现他们的code samples 很有用。该页面包含以下 sn-p,您可能会认为它等同于您问题中的代码。

    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.");
            }
        }
    
    }
    

    【讨论】:

    • 我马上试试这段代码。因为如果我使用您提供的代码,我需要更改很多代码。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多