Android信标监控和配置

时间:2016-05-18 09:53:55

标签: android configuration monitoring ibeacon eddystone

我正在开发用于监控和配置信标设备的应用程序。我需要监控iBeacon和Eddystone信标设备。我已经完成了nRF主控制应用程序。它的工作完美。但我需要一个源代码。是否有其他选择。请给我一个分析信标设备的最佳解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我建议您使用AltBeacon库,我已经在我的一个项目中使用了它并且它非常好(我没有以任何方式与它相关联)。它提供了与信标交互的API。

以下是帮助您入门的示例Activity

public class MyActivity extends AppCompatActivity implements
    BeaconConsumer,
    BootstrapNotifier,
    RangeNotifier
{
    private RegionBootstrap                    mRegionBootstrap;
    private org.altbeacon.beacon.BeaconManager mAltBeaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_application);

        mAltBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(MyActivity.this);

        boolean isBleAvailableAndEnabled;
        try {
            isBleAvailableAndEnabled = mAltBeaconManager.checkAvailability();
        } catch (BleNotAvailableException ex) {
            isBleAvailableAndEnabled = false;
        }

        if (!isBleAvailableAndEnabled) {
            // Handle case ...
            finish();
        }

        // Disable Android L scanning on devices with Android 5.0 and above
        if (Build.VERSION.SDK_INT >= 21) mAltBeaconManager.setAndroidLScanningDisabled(true);

        // Add iBeacon Layout
        mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));

        // Add Eddystone Layout
        mAltBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("Eddystone_layout"));

        mAltBeaconManager.setBackgroundBetweenScanPeriod(3000); // 3 sec
        mAltBeaconManager.setBackgroundScanPeriod(5000); // 5 sec
        mAltBeaconManager.bind(MyActivity.this);

        // Enable Beacon scanning
        mRegionBootstrap = new RegionBootstrap(MyActivity.this, getScanningRegion());
    }

    @Override
    public Context getApplicationContext() {
        return (!isFinishing()) ? MyActivity.this : null);
    }

    @Override
    public void onBeaconServiceConnect() {
        try {
            // Attach beacon range listener
            mAltBeaconManager.setRangeNotifier(this);
            mAltBeaconManager.startRangingBeaconsInRegion(new Region("Region", null, null, null));
        } catch (RemoteException ex) {
            // Handle exception
        }
    }

    @Override
    public void unbindService(ServiceConnection serviceConnection) {
        // Not needed
    }

    @Override
    public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
        return false;
    }

    @Override
    public void didEnterRegion(Region region) {
        // Handle event
    }

    @Override
    public void didExitRegion(Region region) {
        // Handle event
    }

    @Override
    public void didDetermineStateForRegion(int i, Region region) {
        // Handle event
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<org.altbeacon.beacon.Beacon> rangingBeacons, Region region) {
        // Here you will receive the beacons which are currently in range
    }
}

将此添加到您的清单:

<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

注意:另外,请注意此库的限制,这主要是Android在信标支持方面的软件和硬件限制。