Android Beacon Library - BLE beacons检测不起作用

时间:2015-12-06 14:03:05

标签: android ibeacon altbeacon beacon bluetooth-lowenergy

我正在尝试在Android中使用Android Beacon Library检测信标。我创建了一个在后台运行并检测信标的服务。

问题是当蓝牙关闭时应用程序没有检测到信标。但如果我打开蓝牙工作正常。还有一个非常奇怪的事情。如果我在应用程序运行时再次关闭蓝牙,它仍然会继续检测。这意味着BLE检测工作正常,但只有当我打开蓝牙并再次关闭它时。

如何启用BLE检测?我的实现如下。我想念一下吗?

Beacon服务类

public class BeaconDiscoverer extends Service implements BeaconConsumer {
    private static final String TAG = BeaconDiscoverer.class.getSimpleName();
    private static BeaconManager beaconManager;
    private static Region region;
    private BackgroundPowerSaver backgroundPowerSaver;

    public BeaconDiscoverer() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        region = new Region("myRangingUniqueId", null, null, null);
        beaconManager = BeaconManager.getInstanceForApplication(this);

        beaconManager.getBeaconParsers().add(new BeaconParser().
                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));

        configureBatterySaverMode();

        beaconManager.bind(this);
    }

    @Override
    public void onDestroy() {
        beaconManager.unbind(this);
        super.onDestroy();
    }

    private void configureBatterySaverMode() {
        BeaconManager.setAndroidLScanningDisabled(true);
        backgroundPowerSaver = new BackgroundPowerSaver(getApplicationContext());

        // set the duration of the scan to be 5 seconds
        beaconManager.setBackgroundScanPeriod(Utility.convertToMilliseconds(2));
        // set the time between each scan to be 1 min (60 seconds)
        beaconManager.setBackgroundBetweenScanPeriod(Utility.convertToMilliseconds(25));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "BeaconDiscoverer started up");

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onBeaconServiceConnect() {
        Log.d(TAG, "onBeaconServiceConnect");
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    Beacon firstBeacon = beacons.iterator().next();
                    Log.i(TAG, "Beacon detected: " + firstBeacon.getDistance() + " m. - " + firstBeacon.getBluetoothAddress());

                }
            }
        });

        startRanging();
    }

    public void stopRanging() {
        try {
            beaconManager.stopRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void startRanging() {
        if (User.currentUser() == null)
            return;

        try {
            beaconManager.startRangingBeaconsInRegion(region);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

申请类

public class App extends Application {
    private static final String TAG = App.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();

        startService(new Intent(this, BeaconDiscoverer.class));
    }
}

2 个答案:

答案 0 :(得分:4)

简单回答:您必须启用蓝牙才能使用库检测信标。信标使用蓝牙LE来宣传它们的存在,并且必须打开蓝牙无线电才能听到信号。

我无法解释为什么在关闭蓝牙后继续检测信标。一种可能性是,您只需从内存缓存中查看它们,即库的短暂扫描周期,然后才会消失。另一种可能性是Android设备在关闭时关闭蓝牙,但实际上并没有关闭。

典型的方法是在启动应用程序时检测蓝牙是否已关闭,并提示用户将其打开。参考应用程序中有示例代码执行此操作here

答案 1 :(得分:0)

 private void registerBoradcastReceiver() {
    IntentFilter stateChangeFilter = new IntentFilter(
            BluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter connectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_CONNECTED);
//IntentFilter connectedFilter = new   IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    IntentFilter disConnectedFilter = new IntentFilter(
            BluetoothDevice.ACTION_ACL_DISCONNECTED);
    registerReceiver(stateChangeReceiver, stateChangeFilter);
    registerReceiver(stateChangeReceiver, connectedFilter);
    registerReceiver(stateChangeReceiver, disConnectedFilter);
}

    private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_ACL_CONNECTED == action) {
            startService;
        }
        if (BluetoothDevice.ACTION_ACL_DISCONNECTED == action) {
            stopService;
        }
        if (BluetoothAdapter.ACTION_STATE_CHANGED == action) {

        }
    }
};