2016-07-14 90 views
1

我试图查看是否除了Bluetooth之外,与任何设备配对。 要看到,如果Bluetooth是,我使用以下命令:检查蓝牙配对

BluetoothAdapter.getDefaultAdapter().isEnabled() 

我需要知道如果BluetoothAdapter,与任何设备配对。

谢谢你,我希望你的答案

编辑

如果我使用:

BluetoothAdapter.getDefaultAdapter().getBondedDevices(); 

和大小()> 0,这是否意味着配对?或者已经存储设备?

编辑

对不起,但我需要的是不要让配对的设备列表中,但如果一些已经在某些时候配对的设备连接到我的智能手机

回答

1

可以使用查询配对设备这样的:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
// If there are paired devices 
if (pairedDevices.size() > 0) { 
    // Loop through paired devices 
    for (BluetoothDevice device : pairedDevices) { 
     // Add the name and address to an array adapter to show in a ListView 
     mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
    } 
} 

之前进行设备发现,它的价值查询组配对的设备,看是否期望的设备是已知的。为此,请调用getBondedDevices()。这将返回代表配对设备的一组Bluetooth设备。例如,您可以查询所有配对的设备,然后使用ArrayAdapter向用户显示每个设备的名称。

作为官方文档描述here和简单的蓝牙聊天应用程序的好样品here

+0

是的,那是看,但如果当你调用'getBondedDevices()'函数返回你是什么,你已经配对的设备/保存,或者连接是否建立。 也许你需要的不是这个匹配,而是如果移动“蓝牙”开启并连接到另一个设备 –

+1

这只会返回设备上保存的配对设备列表,以便在尝试发送某些数据之前与之配对。您允许他们与您通信,您不必再次发现它们,并且再次配对,因为您已经这样做了,通过此代码,您可以获得有关这些设备的所有必要数据,以便再次与它们通信(例如MAC地址)。 – Tinko

+1

您通过此代码获得的配对设备不必靠近您,或者他们可以禁用其蓝牙,因此此通话不会让您在您附近启用已配对的设备,“等待”您连接到它们。它只会返回您之前通信的设备列表。 – Tinko

1

获取已经配对的设备列表使用此:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     if (pairedDevices == null || pairedDevices.size() == 0) { 
      Toast.makeText(getApplicationContext(),"No Paired Devices Found",Toast.LENGTH_SHORT).show(); 
     } else { 
      ArrayList<BluetoothDevice> list = new ArrayList<BluetoothDevice>(); 
      list.addAll(pairedDevices);   
     } 
+0

好的,但有了那些返回我的getBondedDevices()函数的设备列表,我得到的设备列表已经在某些时候已经配对了吗? 我需要知道的是,我在列表中获得的任何这些设备是否连接到我的智能手机 –

+0

好酷,一旦我们配对,然后我们知道设备名称和mac地址,使用该mack地址与蓝牙套接字连接其他device.Kindly引用此链接,你会得到一些想法https://androidcookbook.com/Recipe.seam;jsessionid=9B476BA317AA36E2CB0D6517ABE60A5E?recipeId=1665 –

+0

好的,谢谢@HariHaran,但我想要的是检查是否有连接智能手机和蓝牙设备,请勿在两台设备之间建立连接 –