简体   繁体   English

android自动将蓝牙设备与特定名称配对

[英]android automaticly pair bluetooth device with specific name

I am looking for a way to scan for all bluetooth devices with certain names (lets say "SC_0001", "SC_0002", etc) and automatically pair them with my phone. 我正在寻找一种方法来扫描所有具有特定名称的蓝牙设备(例如说“ SC_0001”,“ SC_0002”等),然后自动将它们与手机配对。

i already created an app that can list all paired devices and let the user select one. 我已经创建了一个可以列出所有配对设备并让用户选择一个设备的应用程序。 but i dont want the user to have to pair all these devices manually (it whould take way too much time). 但我不希望用户必须手动配对所有这些设备(这将花费太多时间)。

You can use getname() method of the BluetoothDevice class 您可以使用BluetoothDevice类的getname()方法
According to the Documentation of getname() 根据getname()的文档

Get the friendly Bluetooth name of the remote device. 获取远程设备的友好蓝牙名称。

The local adapter will automatically retrieve remote names when performing a device scan, and will cache them. 本地适配器在执行设备扫描时将自动检索远程名称,并将其缓存。 This method just returns the name for this device from the cache. 该方法只是从缓存中返回该设备的名称。

Then, compare both the strings using str1.equals(str2) method. 然后,使用str1.equals(str2)方法比较两个字符串。
Edit 1 编辑1
Here is the way, you can get the list of unpaired devices. 这是您可以获取未配对设备列表的方法。

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

Here is the link to documentation. 这是文档的链接。 BluetoothDevices 蓝牙设备

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM