我正在我的Application类中运行beacon,如下所示:
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的Android设备没有蓝牙?
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)
答案 0 :(得分:0)
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
//Bluetooth is not supported
} else{
//Bluetooth is supported
}
也不要忘记在清单中添加蓝牙权限。