我正在寻找一种方法来扫描所有具有特定名称的蓝牙设备(比如“SC_0001”、“SC_0002”等)并自动将它们与我的手机配对。
我已经创建了一个可以列出所有配对设备并让用户选择一个的应用程序。但我不希望用户必须手动配对所有这些设备(这会花费太多时间)。
您可以根据 getname() 的文档getname()
使用BluetoothDevice 类的方法
获取远程设备的友好蓝牙名称。
本地适配器将在执行设备扫描时自动检索远程名称,并将它们缓存起来。此方法仅从缓存中返回此设备的名称。
然后,使用方法比较两个字符串str1.equals(str2)
。
编辑 1
这是获取未配对设备列表的方法。
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{
// compare device.getName() and your string here, if satisfied, add them to the list-view.
tv.setText(device.getName() + "\n" + device.getAddress());
这是文档的链接。蓝牙设备