【问题标题】:Android Beacon Library - BLE beacons detecting is not workingAndroid 信标库 - BLE 信标检测不起作用
【发布时间】:2015-12-06 14:03:04
【问题描述】:

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

问题是当蓝牙关闭时,应用程序没有检测到信标。但是如果我打开蓝牙工作正常。还有一件非常奇怪的事情。如果我在应用程序运行时再次关闭蓝牙,它仍然会继续检测。这意味着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));
    }
}

【问题讨论】:

  • 你想要达到的目标还不清楚。
  • 我想在蓝牙未关闭时检测信标。我的意思是如果用户至少有一次没有打开蓝牙,则蓝牙检测不起作用。

标签: android ibeacon altbeacon beacon bluetooth-lowenergy


【解决方案1】:

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

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

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

【讨论】:

  • 哦,谢谢,我以为我们不需要在棒棒糖设备之后启用蓝牙来检测信标。我很困惑,因为我的手机在我关闭蓝牙后检测到信标。无论如何,我还实施了警告用户打开蓝牙。谢谢。
【解决方案2】:
 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) {

        }
    }
};

【讨论】:

  • 你能解释一下吗?
  • 接收蓝牙广播。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2020-11-25
  • 1970-01-01
相关资源
最近更新 更多