正确使用信标和区域的方式

时间:2018-01-15 00:10:24

标签: java android ibeacon altbeacon

使用iBeacon。 根据我的理解,我应该开始使用" beaconManager.startRangingBeaconsInRegion"扫描信标。或者" startMonitoringBeaconsInRegion"。 但我尝试创建一个区域,当进入和退出时,它们会显示标识符,如" null"," null"," null"

我需要知道何时不再检测到特定的信标

请遵循以下代码,考虑到" LocusData.locusBeacons"是具有先前注册的信标的ArrayList

@Override
public void onBeaconServiceConnect() {
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                Beacon aux = beacons.iterator().next();
                Log.i("ScanBeacon", "The beacon "+
                        aux.getId1().toString()+"|"+
                        aux.getId2().toString()+"|"+
                        aux.getId3().toString());

                LocusData.updateDistance(
                        aux.getId1().toString(),
                        aux.getId2().toString(),
                        aux.getId3().toString(),
                        aux.getDistance());
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    notifyListView();
                }
            });

        }
    });

    beaconManager.addMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didEnterRegion(Region region) {
            Log.i("SCANNING", "ENTER: " +
                    region.getId1() + "|" +
                    region.getId2() + "|" +
                    region.getId3());
        }

        @Override
        public void didExitRegion(Region region) {
            Log.i("SCANNING", "EXIT: " +
                    region.getId1() + "|" +
                    region.getId2() + "|" +
                    region.getId3());
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            //Log.i(TAG, "I have just switched from seeing/not seeing beacons: "+state);
        }
    });

    try {
        for (LocusBeacon i : LocusData.locusBeacons) {
            beaconManager.startRangingBeaconsInRegion(new Region(i.getId(),
                    Identifier.parse(i.getUUID()),
                    Identifier.fromInt(Integer.parseInt(i.getMajor())),
                    Identifier.fromInt(Integer.parseInt(i.getMinor()))));
        }
    } catch (RemoteException e) {    }

}

1 个答案:

答案 0 :(得分:0)

如果你监控一个用填充的标识符的所有三个部分定义的区域,然后开始监控,当这样做时,你会得到一个回调,当这个确切的信标停止被检测到时:

open System
open System.Drawing
open System.Windows.Forms

let form = new Form(Text="Simple Animation", Size=Size(400,500))
let pen = new Pen(Color.Red, 4.0f)
let random = new Random()

let line x = 
    let flexRight = random.Next(29,300)
    form.Paint.Add (fun e -> e.Graphics.DrawLine(pen, 30, 30, 350, flexRight))

let timer=new Timer(Interval=1000, Enabled=true)
timer.Tick.Add(fun time -> line())

form.Show()
Application.Run(form)

for (LocusBeacon i : LocusData.locusBeacons) { beaconManager.startMonitoringBeaconsInRegion(new Region(i.getId(), Identifier.parse(i.getUUID()), Identifier.fromInt(Integer.parseInt(i.getMajor())), Identifier.fromInt(Integer.parseInt(i.getMinor())))); } 的回调将有一个具有相同标识符的区域对象,用于开始监视已消失的特定信标。

要使其正常工作,didExitRegion(Region region)返回的字符串必须是唯一的。

如果您还在监视一个所有标识符都设置为null的区域,那么当所有信标从视图中消失时,didExitRegion将为该区域触发。