2016-11-11 16 views
0

利用可能なすべてのBluetoothデバイスを検出し、別のアクティビティに渡そうとしています。 しかし、Androidのドキュメントを見ても、デバイスが見つからず、ArrayListが空のままになっている理由を理解できません。Android - Bluetoothデバイスを含むArrayListは空のままです

クリック時には、これを実行します。

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 Marshmallowに必要です。

+0

位置情報はBluetoothで問題にはなりません。それが解決策だと確信していますか? –

+0

startDiscoveryをBluetoothAdapter.getDefaultAdapter()に変更しました。startDiscovery();それではうまくいかなかったので、設定>アプリ>ロケーション – Joseph

+0

でロケーションサービスが有効になっていることを確認しました。これで、Bluetoothを有効にしました。 –

関連する問題