2016-11-11 29 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。 –