ListViewの近くにある使用可能なすべてのデバイスを一覧表示しようとしています。Android BluetoothDevice.Action_foundブロードキャストが受信されなかった
マイマニフェストには、BLUETOOTH、BLUETOOTH_ADMIN、およびACCESS_COARSE_LOCATIONの権限が含まれています。
問題はACTION_DISCOVERY_STARTEDで、ACTION_DISCOVERY_FINISHEDは受信されましたが、ACTION_FOUNDは受信されません。私は自分のコンピュータのブルートゥースを発見可能にし、私の電話のブルートゥースデバイスはそれを発見することができますが、私のアプリはできません。近くのすべてのデバイスにも同じことが適用されます。私は自分の質問のために述べたように
private BluetoothAdapter mBluetoothAdapter;
private ArrayList<String> mDeviceList;
private ListView mListView;
private ProgressDialog progressDialog;
private static final int REQUEST_ENABLE_BT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_list);
//list to hold all devices found
mDeviceList = new ArrayList<>();
//list to display all devices found
mListView = (ListView) findViewById(R.id.availItems);
mListView.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mDeviceList));
//local bluetooth device
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// If the adapter is null, then Bluetooth is not supported
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
}else if(!mBluetoothAdapter.isEnabled()){
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDeviceList.add(device.getName() + "\n" + device.getAddress());
}
}
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Scanning...");
progressDialog.setTitle("Looking for devices...");
progressDialog.setCancelable(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Button findButton = (Button) findViewById(R.id.findButton);
findButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
});
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
progressDialog.show();
}else if (BluetoothDevice.ACTION_FOUND.equals(action) && mBluetoothAdapter.isDiscovering()) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName() + "\n" + device.getAddress());
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
progressDialog.dismiss();
}
}
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
@Override
public void onPause() {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
}
super.onPause();
}
@Override
public void onDestroy() {
unregisterReceiver(mReceiver);
mListView = null;
mBluetoothAdapter = null;
mDeviceList = null;
super.onDestroy();
}
私は今同じ問題を抱えています!あなたはそれを修正しましたか? –
いいえ私はそれを理解していない、私が見つけた他のすべての関連問題は私のために働かなかった解決策を持っていた。 – Aaron