Iryna D
Iryna D

Reputation: 85

Pair two bluetooth devices by clicking on a listView item, Android

I'm relatively new to programming on java for android and can't figure out how to pair two devices by clicking on an item of a listView that contains a list of newly discovered devices.

I've already created a listView containing a set of newly discovered devices and here is a part of my code for a click event:`

public class MainActivity extends AppCompatActivity {

ListView newListView; // listView containing newly discovered devices
ArrayAdapter<String> mNewDevicesArrayAdapter;
BluetoothAdapter mBluetoothAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {


    /* Variables definition */

    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    newListView = (ListView) findViewById(R.id.new_lv);

    // New Devices List View item click
    newListView.setClickable(true);

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

        // some code must go here, but I can't figure out which one 


        }
    });

   }

Thanks in advance for your help!!!

Upvotes: 0

Views: 1282

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191738

You have a list of strings, yes, in the adapter?

You can get a BluetoothDevice object from the BluetoothAdapter

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

        String address = mDevicesAdatper.getItem(position);
        BluetoothDevice btDevice = mBluetoothAdapter.getRemoteDevice(address);

        // TODO: Pair

    }
});

Much of the specifics for handling paired devices is in the documentation

Upvotes: 1

Related Questions