2

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

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

如何启用 BLE 检测?下面是我的实现。我错过了什么吗?

信标服务类

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));
    }
}
4

2 回答 2

4

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

我无法解释为什么您在关闭蓝牙后继续检测信标。一种可能性是,在它们消失之前,您只是在库的短暂扫描周期期间从内存缓存中看到它们。另一种可能性是,当您将其关闭时,Android 设备说蓝牙已关闭,但它并没有真正关闭。

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

于 2015-12-06T15:57:40.840 回答
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) {

        }
    }
};
于 2015-12-06T15:31:21.493 回答