public WMBeaconManager( MonitorNotifier monitorNotifier, RangeNotifier rangeNotifier, Context context) { this.context = context; mBeaconManager = BeaconManager.getInstanceForApplication(this.context); // Detect the main Eddystone-UID frame: mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(EDDYSTONE_LAYOUT)); mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(IBEACON_LAYOUT)); mBeaconManager.setRangeNotifier(rangeNotifier); mBeaconManager.setMonitorNotifier(monitorNotifier); }
public BeaconSearcher(Context context) { this.mContext = context.getApplicationContext(); this.mBeaconManager = BeaconManager.getInstanceForApplication(mContext); // 经过测试,天津的Beacon应该是Apple的Beacon,beaconTypeCode=0215 // 其传输帧的字节序列按照以下顺序传输,但是网络上查到2013年后的Estimote beacons也是下列的字节顺序,ok // mBeaconManager.getBeaconParsers().add(new // BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); // 也可能是AltBeacon(即Radius)的Beacon,ok mBeaconManager .getBeaconParsers() .add(new AltBeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); // 设置距离计算模型 DefaultDistanceCalcuator defaultDistanceCalcuator = new DefaultDistanceCalcuator(mContext); // 使用asserts/model-distance-calculations.json defaultDistanceCalcuator.setDefaultDistanceCalcuator(null); // 使用url // defaultDistanceCalcuator.setDefaultDistanceCalcuator("http://data.altbeacon.org/android-distance.json"); // 设置发现beacon监听回调,看到beacon,看不到beacon; 进入,离开,临界状态 // Specifies a class that should be called each time the BeaconService // sees or stops seeing a Region of beacons. // IMPORTANT: Only one MonitorNotifier may be active for a given // application. If two different activities or services set different // MonitorNotifier instances, the last one set will receive all the // notifications. mBeaconManager.setMonitorNotifier(mMonitorNotifier); // 设置测距修正回调,每个扫描周期结束,根据20秒内各beacon的RSSI平均值计算它的距离,该回调获取这些beacon的距离值 // Specifies a class that should be called each time the BeaconService // gets ranging data, which is nominally once per // second(实际上每个扫描周期,计算一次距离) when beacons are detected. // IMPORTANT: Only one RangeNotifier may be active for a given // application. If two different activities or services set different // RangeNotifier instances, // the last one set will receive all the notifications. mBeaconManager.setRangeNotifier(mRangeNotifier); // 当程序切换到后台,BeaconService自动切换到后台模式,为了省电,蓝牙扫描频率降低;程序恢复到前台,BeaconService也跟随恢复至前台 // simply constructing this class and holding a reference to it in your // custom Application // class will automatically cause the BeaconLibrary to save battery // whenever the application // is not visible. This reduces bluetooth power usage by about 60% Context appContext = this.mContext.getApplicationContext(); mBackgroundPowerSaver = new BackgroundPowerSaver(appContext); }