如何在列表视图中获取蓝牙详细信息?

时间:2015-12-19 09:00:12

标签: android listview bluetooth

我创建了一个简单的蓝牙应用程序。当我点击列表视图并将其存储到数组中时,如何将配对设备的信息(如名称和地址)放入其中?

public class BluetoothConnection extends Activity {

Button listbt;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;
ArrayList<String> list = new ArrayList<String>();

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

    listbt=(Button)findViewById(R.id.listbutton);

    BA = BluetoothAdapter.getDefaultAdapter();
    lv = (ListView)findViewById(R.id.listView);

}

public void list(View v){
    pairedDevices = BA.getBondedDevices();

    for(BluetoothDevice bt : pairedDevices)
    {
        list.add(bt.getName() + "\n" + bt.getAddress());
    }
    Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show();

    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
    lv.setAdapter(adapter);
}
}

1 个答案:

答案 0 :(得分:2)

试试这个:

lv.setOnItemClickListener(mDeviceClickListener);

mDeviceClickListener声明。

private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            // Get the device MAC address, which is the last 17 chars in the View
            String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);   
        }
    };