IBeacon Development Tour "One" Scan iBeacon Signal

IBeacon Development Tour "One" Scan iBeacon Signal

Since the company's recent project is related to iBeancon, the development of indoor positioning and the introduction of the principle of IBeancon are all on Baidu.
First we need to introduce a dependency:

 compile 'org.altbeacon:android-beacon-library:2.13.1'

Note: On 6.0 devices, you need to dynamically apply for permissions. The required permissions are as follows

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-feature android:name="android.hardware.bluetooth_le"android:required="true"/>
  • First look at the effect

write picture description here

-Initialize service

The way we use is to scan once a second through a background service, and then send it to the view by broadcasting

public class BeaconService extends Service implements BeaconConsumer, RangeNotifier {

    private static final long DEFAULT_BACKGROUND_SCAN_PERIOD = 2000L;
    private static final long DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD = 2000L;
    private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
    public BeaconService() {

    }
    @Override
    public void onCreate() {
        super.onCreate();
        initBeacon();
        beaconManager.bind(this);
    }

    private void initBeacon() {
        beaconManager.setBackgroundScanPeriod(DEFAULT_BACKGROUND_SCAN_PERIOD);
        beaconManager.setBackgroundScanPeriod(DEFAULT_BACKGROUND_BETWEEN_SCAN_PERIOD);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (beaconManager != null)
            beaconManager.removeRangeNotifier(this);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onBeaconServiceConnect() {
        Region region = new Region("myRangingUniqueId", null, null, null);
        beaconManager.addRangeNotifier(this);
        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> collections, Region region) {
        List<Beacon> beacons = new ArrayList<>();
        for (Beacon beacon : collections) {
            beacons.add(beacon);
        }
        Intent intent = new Intent(MainActivity.BEACON_ACTION);
        intent.putParcelableArrayListExtra("beacon", (ArrayList<? extends Parcelable>) beacons);//因为Beacon继承了Parcelable,
        sendBroadcast(intent);                                                                   // 所以能通过这个方式来传递数据
    }
}
  • We start the service in the activity, and then create a broadcast receiver to receive the data. Since the data entries received each time are inconsistent, the data needs to be processed. The principle is that if there is new data, the new data is updated. If there is no new data, the old data is retained. Finally, sort the data. Here you need to customize the sorting method and sort by distance. Collections.sort();
 class BeaconBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BEACON_ACTION.equals(action)) {
                ArrayList<Beacon> allDevice = intent.getParcelableArrayListExtra("beacon");
                //遍历新的设备
                for (Beacon beacon : allDevice) {
                    /**
                     * 如果有新设备,则将旧设备移除  将新设备添加
                     * 如果没有直接添加
                     * 一秒刷新一次  更加直观
                     * */
                    addDevice(beacon);
                }
                //对拿到的结果进行排序操作,自定义排序方法,按照距离排序
                Collections.sort(beacons, new SortByDistance());

                if (beacons != null) {
                    scanCount.setTitle("设备数:" + beacons.size());
                    if (lvDevice.getAdapter() == null) {
                        lvDevice.setAdapter(deviceAdapter);
                    } else {
                        deviceAdapter.notifyDataSetChanged();
                    }
                }
                count++;
            }
        }

    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681068&siteId=291194637