1

我想在我在布局中创建的列表中列出所有配对的设备。这段代码有什么问题?我刚刚创建了这个函数来显示所有绑定的设备

void tooth_scan() {
    ListView listView = (ListView) findViewById(R.id.pairList);
    Set<BluetoothDevice> pairedDevices = bAdapter.getBondedDevices();
    devicesPaired = new String[pairedDevices.size()];
    int count = 0;
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            devicesPaired[count] = device.getName();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1,
                devicesPaired);
        listView.setAdapter(adapter);
    }
}
4

1 回答 1

3
public void showPairedDevices()
    {
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            ArrayList<String> bluetoothDevices = new ArrayList<String>();
            for (BluetoothDevice device : pairedDevices) {

                String deviceName = device.getName(); // Get BT name
                String deviceAddress = device.getAddress(); // Get MAC
                ListView listView = (ListView)findViewById(R.id.paired_list);

                bluetoothDevices.add(deviceName);

                final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,bluetoothDevices);
                listView.setAdapter(adapter);

            }

        }
    }
于 2017-06-08T09:19:37.480 回答