2016-11-11 28 views
0

我試圖發現所有可用的藍牙設備並傳遞給其他活動。 但是,即使在查看Android文檔時,我也無法弄清楚爲什麼我無法發現任何設備,並且我的ArrayList保持空白。Android - 包含藍牙設備的ArrayList保持爲空

的OnClick執行此:

mBluetoothAdapter.startDiscovery(); 

我的廣播聽衆也工作,但沒有什麼是每次返回和ArrayList保持爲空。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     mDeviceList.add(device); 
     showToast("Found device " + device.getName()); 
    } 

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
     final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); 
     if (state == BluetoothAdapter.STATE_ON) { 
      showToast("Enabled"); 
      showEnabled(); 
     } 
    } 
    if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { 
     mDeviceList = new ArrayList<>(); 
     mProgressDlg.show(); 
    } 
    if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
     mProgressDlg.dismiss(); 

     Intent newIntent = new Intent(MainScreen.this, DeviceListActivity.class); 
     if (mDeviceList.isEmpty()){ 
      Log.d("onReceive: ", "EMPTY"); 
     } 
     newIntent.putParcelableArrayListExtra("device.list", mDeviceList); 

     startActivity(newIntent); 
    } 

} 

};

GIST

回答

0

回答了我自己的問題。

解決方法是通過應用程序權限打開位置。

顯然這是Android棉花糖需要的

+0

位置不應該是藍牙的問題。你確定這是解決方案嗎? –

+0

我將startDiscovery更改爲BluetoothAdapter.getDefaultAdapter()。startDiscovery();這沒有工作,所以我確保位置服務在設置>應用程序>位置 – Joseph

+0

右,所以你啓用了藍牙發現,而不是「啓用位置」意味着GPS。 –