连接到特定的蓝牙配对设备 [英] Connect to a specific bluetooth paired device

查看:693
本文介绍了连接到特定的蓝牙配对设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用所有配对的设备实现了一个列表,现在我想知道是否仅通过单击该项目就可以连接到其中一些设备.

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.

例如,如果我的列表中包含一个名为X的蓝牙设备,并且我想连接到它(使用我的应用程序),请单击它,并在设备和手机之间建立连接.

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

推荐答案

如果知道要配对的设备的名称,则可以使用等值比较.

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.
}

您还可以使用应用程序UUID

You can also use an app UUID

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

这意味着只有使用此UUID的设备才能使用您的协议进行连接,这是该应用程序的另一层安全保护.

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

在后一种情况下,我们依靠一台android设备充当BT服务器,另一台充当BT客户端.

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.

这篇关于连接到特定的蓝牙配对设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆