2

I've implemented a list with all of my paired devices, and now I'd like to know if it's possible to connect to some of them only with clicking on the item.

For example if my list contains a bluetooth device called X and I want to connect to it (with my app) click on it and the connection is stablished between device and my phone.

This is how I list my paired devices :

 myListView = (ListView) dialog.findViewById(R.id.BTList);
    BTArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    myListView.setAdapter(BTArrayAdapter);
    pairedDevices = myBluetoothAdapter.getBondedDevices();
    for(BluetoothDevice device : pairedDevices)
        BTArrayAdapter.add(device.getName()+ "\n" + device.getAddress());
Skizo-ozᴉʞS
  • 18,460
  • 17
  • 76
  • 134

1 Answers1

1

If you know the name of the device you wish to pair to you can use an equals comparison.

private static final String DEVICE_WE_WANT_TO MATCH = "X";

String devName = device.getName();
if(devName.equals(DEVICE_WE_WANT_TO MATCH)){
    // Connect.
}

You can also use an app UUID

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

This will mean that only devices using this UUID will connect using your protocol, it's an extra layer of security for the app.

In this, latter, case, we're relying on one android device to be acting as a BT server and the other as a BT client.

  • I'm gonna test it, but I asked it to know how to connect, the list I allready showed up... but thanks anyways for the reply ;) – Skizo-ozᴉʞS Sep 28 '16 at 13:58
  • If you have time/and you want me to show how to connect programmatically, go ahead, your help is always welcome :D – Skizo-ozᴉʞS Sep 28 '16 at 14:01
  • Hi @Yvette Colomb, maybe you can help me out on this [question](http://stackoverflow.com/questions/34997669/android-acceleration-down-smash)? Big price on it :P – Skizo-ozᴉʞS Feb 01 '17 at 13:48