【问题标题】:What happened if android device has no bluetooth support when beacon runs如果信标运行时android设备不支持蓝牙会发生什么
【发布时间】:2017-05-26 04:39:52
【问题描述】:

我在我的应用程序类中像这样运行信标:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        beaconManager = new BeaconManager(getApplicationContext());
        beaconManager.setBackgroundScanPeriod(5000, 25000);
        beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
            @Override
            public void onEnteredRegion(Region region, List<Beacon> list) {


            }

            @Override
            public void onExitedRegion(Region region) {

            }
        });
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {

                beaconManager.startMonitoring(new Region("rid", null, null, null));
            }
        });

    }

它在我的设备上正确运行,但在模拟器中它崩溃了。因为模拟器不支持蓝牙。所以我的问题是如果真实设备没有蓝牙会发生什么?如果它会崩溃,我该如何防止它? 4.3以上的安卓设备会不会没有蓝牙?

 E/BluetoothAdapter: Bluetooth binder is null
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference
                                                               at com.estimote.sdk.service.internal.JellyBeanBluetoothAdapter.start(JellyBeanBluetoothAdapter.java:43)
                                                               at com.estimote.sdk.service.internal.CycleBluetoothScanner.start(CycleBluetoothScanner.java:64)
                                                               at com.estimote.sdk.service.BeaconService.startScanning(BeaconService.java:336)
                                                               at com.estimote.sdk.service.BeaconService.startMonitoring(BeaconService.java:286)
                                                               at com.estimote.sdk.service.BeaconService.access$300(BeaconService.java:69)
                                                               at com.estimote.sdk.service.BeaconService$IncomingHandler$1.run(BeaconService.java:440)
                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:154)
                                                               at android.os.HandlerThread.run(HandlerThread.java:61)

【问题讨论】:

  • 你需要在执行bacon代码之前检查蓝牙权限,当设备有蓝牙时它会运行,否则会显示设备不支持的消息。
  • 你遇到了什么异常?发布 logcat。
  • 您不必担心这个问题。因为很少有设备,因为我什至没有听过任何没有蓝牙设备的设备。
  • @AndroidGeek 谢谢
  • @Henry 我添加了模拟器的异常

标签: android bluetooth ibeacon-android beacon estimote


【解决方案1】:
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    //Bluetooth is not supported
} else{
    //Bluetooth is supported
}

另外不要忘记在清单中添加蓝牙权限。

【讨论】:

    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    相关资源
    最近更新 更多