2015-04-29 85 views
2

您好,我嘗試掃描Android棒棒糖的藍牙設備。它工作正常,但如果關閉bluetotooth然後運行該應用程序它會崩潰,然後給彈出以啓用bluetooth.Ideally喜歡它應該給彈出以啓用藍牙,如果它被關閉。如果在android棒棒糖中關閉藍牙,Android應用崩潰

這是方法,這應該啓用藍牙:

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

代碼開始掃描

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

啓用開始掃描

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..."); 
         } 

下面是日誌文件

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) 
+1

您BluetoothLeScanner似乎並沒有被initalized掃描前。請添加您的方法的代碼。 –

+0

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

+0

這種方法來啓用藍牙 – Deepak

回答

3

的正確的做法是開始,我們必須檢查BluetoothLeScanner對象爲空或不喜歡我們檢查BluetoothAdapter

樣品

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

    } 
0

你可以告訴大家,藍牙是不是有一個對話框中啓用用戶,然後爲其啓用他們,當他們點擊確定(或者什麼也不做,如果他們打取消)使用如下代碼:

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(); 
} 
+0

它工作正常在android kitkat。在棒棒糖中,應用程序首先崩潰,然後顯示彈出窗口以啓用藍牙。 – Deepak

+0

請編輯您的問題,上面的代碼摘錄顯示'MyActivity.handleScanStart' – davidgyoung