Connect to a specific bluetooth paired device

半腔热情 提交于 2019-12-23 01:35:19

问题


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

回答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.



来源:https://stackoverflow.com/questions/35627257/connect-to-a-specific-bluetooth-paired-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!