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);
}
}
}
位置情報はBluetoothで問題にはなりません。それが解決策だと確信していますか? –
startDiscoveryをBluetoothAdapter.getDefaultAdapter()に変更しました。startDiscovery();それではうまくいかなかったので、設定>アプリ>ロケーション – Joseph
でロケーションサービスが有効になっていることを確認しました。これで、Bluetoothを有効にしました。 –