Dave
Dave

Reputation: 117

How can I get the Bluetooth details in listview?

I have created a simple bluetooth application. How can I put the information of the paired device such as name and address when I click on the listview and stored it into an array?

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);
}
}

Upvotes: 0

Views: 106

Answers (1)

Amy
Amy

Reputation: 4032

Try this:

lv.setOnItemClickListener(mDeviceClickListener);

mDeviceClickListener declaration.

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);   
        }
    };

Upvotes: 2

Related Questions