1

I'm making a bluetooth connection.

First I list the name and address of they in a ListView:

pairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());


pairedListView.setOnItemClickListener(mDeviceClickListener);

I create aמ onClick event for each found device. Now I need get the BluetoothDevice in the onClick event, not only the address..

    private AdapterView.OnItemClickListener mDeviceClickListener
                = new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {

String info = ((TextView) v).getText().toString();
            String address = info.substring(info.length() - 17);
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130

2 Answers2

0

Fast solution: Store all devices in array. When onItemClick is invoked the arg2 is actually the index in array which will be equal to index in your array.

Good solution: Extend ArrayAdapter and implement your custom logic.

Kamen Stoykov
  • 1,715
  • 3
  • 17
  • 31
0

Make a arrayList and store all the scanned devices in arrayList.

 ArrayList<BluetoothDevice> deviceArrayList; //store all the scanned devices on it.
 BluetoothDevice bluetoothDeviceSelected = null;
 String name,address;

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id)  {

    bluetoothDeviceSelected = deviceArrayList.get(position);
    name=bluetoothDeviceSelected.getName();
    address=bluetoothDeviceSelected.getAddress();
}
sumit singh
  • 588
  • 1
  • 5
  • 20