如何正确显示蓝牙设备列表

how to correctly display a list of bluetooth devices

我有一个 activity,我想在其中显示已配对和未配对的蓝牙设备的列表。我不想要一个只显示列表的按钮,而是直接显示列表。 我正在按照 android 指南 https://github.com/googlesamples/android-BluetoothChat/blob/master/Application/src/main/java/com/example/android/bluetoothchat/DeviceListActivity.java 的示例进行操作,但我不明白为什么未配对的设备没有出现在列表中,而已配对的设备却正确出现在列表中。 这是我的代码

public class BluetoothListActivity extends AppCompatActivity {

    private static final int ENABLE_REQUEST = 0;
    private ListView newDevicesList;
    private ListView pairedDevicesList;
    private ArrayList<String> stringArrayList;

    private ArrayAdapter<String> newDevicesArrayAdapter;
    private ArrayAdapter<String> pairedDevicesArrayAdapter;

    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_list);

        if(bluetoothAdapter == null){
            Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show();
            finish();
        }

        if(bluetoothAdapter!= null){
            if(!bluetoothAdapter.isEnabled()){
                Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(i,ENABLE_REQUEST);
            }
        }

        //do discovery
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }

        bluetoothAdapter.startDiscovery();

        pairedDevicesArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        newDevicesArrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);

        pairedDevicesList = findViewById(R.id.pairedDevicesListView);
        newDevicesList = findViewById(R.id.newDevicesListView);

        pairedDevicesList.setAdapter(pairedDevicesArrayAdapter);
        newDevicesList.setAdapter(newDevicesArrayAdapter);


        // Register for broadcasts when a device is discovered
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        this.registerReceiver(broadcastReceiver, filter);

        // Register for broadcasts when discovery has finished
        filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        this.registerReceiver(broadcastReceiver,filter);

        //Set of paired devices
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
        if(pairedDevices.size() > 0 ){
            for(BluetoothDevice device : pairedDevices){
                pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }else{
            pairedDevicesArrayAdapter.add("No device found");
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ENABLE_REQUEST){
            if(resultCode != RESULT_OK){
                Toast.makeText(this, "You need to enable the bluetooth.", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                    newDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                if (newDevicesArrayAdapter.getCount() == 0) {
                    newDevicesArrayAdapter.add("No device found");
                }

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }
        unregisterReceiver(broadcastReceiver);
    }
}

找到解决办法。 您需要在运行时请求位置权限(ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION)