简体   繁体   English

如果在android lollipop中关闭了蓝牙,Android安卓应用程序崩溃

[英]Android ble app crash if bluetooth is turned off in android lollipop

Hello I am try to scan the bluetooth ble device for Android Lollipop. 您好我正在尝试扫描Android Lollipop的蓝牙设备。 It is working fine but if turned off the bluetotooth an then run the App it will crash and then give the pop up to enable the bluetooth.Ideally like it should give the pop up to enable bluetooth if it is turned off. 它工作正常,但如果关闭bluetotooth然后运行应用程序它将崩溃,然后弹出启用蓝牙。理想它应该弹出启用蓝牙,如果它关闭。

This is the method, which should enable bluetooth: 这是应该启用蓝牙的方法:

   private void enableBluetooth() { 
   if(bluetoothAdapter == null) {
     //bluetoothState.setText("Bluetooth NOT supported"); } 
   else if(!bluetoothAdapter.isEnabled()) {
     //bluetoothAdapter.enable(); 
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
   }

code to start scan 代码开始扫描

public void handleScanStart(View view) {
        foundDevices.clear();
        btArrayAdapter.clear();
        ble.startBleScan();
        scanButton.setEnabled(false);
       stopScanButton.setEnabled(true);
}

Enable Start Scan 启用开始扫描

  public void startBleScan() {
        if(getScanning()) {
           return;
       }

          enableBluetooth();
         scanning = true;


         ScanFilter.Builder filterBuilder = new ScanFilter.Builder();                     //TODO currently default, scans all devices
        ScanSettings.Builder settingsBuilder = new               ScanSettings.Builder();
        settingsBuilder.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY);
             List<ScanFilter> filters = new ArrayList<ScanFilter>();
                  filters.add(filterBuilder.build());
                   bluetoothLeScanner.startScan(filters, settingsBuilder.build(), scanCallback);

           Log.d(TAG, "Bluetooth is currently scanning...");
                      }

Below is the log file 以下是日志文件

    04-29 18:09:11.415: E/AndroidRuntime(26155): FATAL EXCEPTION: main
    04-29 18:09:11.415: E/AndroidRuntime(26155): Process: com.android.androidble5, PID: 26155
    04-29 18:09:11.415: E/AndroidRuntime(26155): java.lang.IllegalStateException: Could not execute method of the activity
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.view.View$1.onClick(View.java:4020)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.view.View.performClick(View.java:4780)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.view.View$PerformClick.run(View.java:19866)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.os.Handler.handleCallback(Handler.java:739)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.os.Handler.dispatchMessage(Handler.java:95)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.os.Looper.loop(Looper.java:135) 
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.app.ActivityThread.main(ActivityThread.java:5254)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at java.lang.reflect.Method.invoke(Native Method)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at java.lang.reflect.Method.invoke(Method.java:372)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    04-29 18:09:11.415: E/AndroidRuntime(26155): Caused by: java.lang.reflect.InvocationTargetException
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at java.lang.reflect.Method.invoke(Native Method)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at java.lang.reflect.Method.invoke(Method.java:372)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at android.view.View$1.onClick(View.java:4015)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    ... 10 more
    04-29 18:09:11.415: E/AndroidRuntime(26155): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.bluetooth.le.BluetoothLeScanner.startScan(java.util.List, android.bluetooth.le.ScanSettings, android.bluetooth.le.ScanCallback)' on a null object reference
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at com.android.androidble5.BluetoothUtility.startBleScan(BluetoothUtility.java:204)
    04-29 18:09:11.415: E/AndroidRuntime(26155):    at com.android.androidble5.MyActivity.handleScanStart(MyActivity.java:247)

The correct approach would be before starting the scan we have to check BluetoothLeScanner object null or not like we check for BluetoothAdapter 正确的方法是在开始扫描之前我们必须检查BluetoothLeScanner对象是否为null我们检查BluetoothAdapter

Sample 样品

if(bluetoothLeScanner != null){
             bluetoothLeScanner.startScan(filters,settingsBuilder.build(),scanCallback);

    }

You can tell a user that bluetooth is not enabled with a dialog, and then enable it for them when they hit OK (or do nothing if they hit Cancel) with code like this: 您可以告诉用户蓝牙没有启用对话框,然后在他们点击OK(或者如果他们点击取消时什么也不做)时使用这样的代码为他们启用它:

final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (!bluetoothAdapter.isEnabled()) {
 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("Bluetooth not enabled");
 builder.setMessage("Press OK to enable bluetooth");
 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
   Log.d(TAG, "Enabling bluetooth");
   bluetoothAdapter.enable();
  }
 });
 builder.setNegativeButton(android.R.string.cancel, null);
 builder.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM