如何在android中以编程方式发现蓝牙设备并在Listview中显示

Bha*_*ula -3 android listview bluetooth

我尝试并遵循了一些网络教程,但它不适用于新的 Android 版本。

我声明了所有蓝牙权限并使用了 Dexter 权限库。我遵循了几个答案,但它也没有显示可用的蓝牙设备名称

下面是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    scan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toast("starts scanning...");
            mBluetoothAdapter.startDiscovery();

        }
    });

    mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           String bluetoothDevice = mAdapter.getItem(i);
           toast(bluetoothDevice);
        }
    });

}
public void pairedDevicesListView(View view){
    mAdapter.clear();

    pairedDevices = mBluetoothAdapter.getBondedDevices();

    for (BluetoothDevice device : pairedDevices){
        mAdapter.add(device.getName() + "\n" + device.getAddress());

    }
}
}
Run Code Online (Sandbox Code Playgroud)

Ast*_*oid 5

To discover a device, first get the bluetooth adapter by calling BluetoothAdapter.getDefaultAdapter()

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)

To start discover, simply call the startDiscovery() from bluetooth adapter. This process is asynchronous so it will return immediately. To catch the discovery process, we can register a BroadcastReceiver with ACTION_FOUND, ACTION_DISCOVERY_STARTED, ACTION_DISCOVERY_STARTED. For each device found, the intent will carry extra field EXTRA_DEVICE containg the BluetoothDevice object.

IntentFilter filter = new IntentFilter();

filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, filter);
adapter.startDiscovery();
Run Code Online (Sandbox Code Playgroud)

The receiver:

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

            if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                //discovery starts, we can show progress dialog or perform other tasks
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                //discovery finishes, dismis progress dialog
            } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                       //bluetooth device found
                BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                showToast("Found device " + device.getName());
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

And, don’t forget to unregister the receiver on Activity’s onDestroy method:

@Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);

        super.onDestroy();
    }
Run Code Online (Sandbox Code Playgroud)