您可以通过尝试连接到Android设备上通常可用的标准UUID来确定设备是否在范围内。如果连接呼叫:
- 失败,则远程设备超出范围或其蓝牙被禁用。
- 成功,那么远程设备在范围内,你应该关闭连接,然后尝试连接到你的应用程序的UUID ...如果失败,那么没有侦听套接字...如果成功,那么所有好。
示例代码:
public BluetoothSocket connect(BluetoothDevice remoteDevice) throws IOException
{
OPP_UUID = UUID.fromString("00001105-0000-1000-8000-00805f9b34fb");
// check if remote device is in range...throw exception if out of range
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(OPP_UUID);
socket.connect();
socket.close();
}
catch(IOException ex)
{
throw new IOException("out of range",ex);
}
// try to connect to service on remote device...throw exception if UUID
// is not available
try
{
BluetoothSocket socket = remoteDevice
.createRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
return socket;
}
catch(IOException ex)
{
throw new IOException("no listening server socket",ex);
}
}
我以前BluetoothDevice.getUuids()
让我的Android设备上的一个可用的UUID。它给了我这个名单:
0000110a-0000-1000-8000-00805f9b34fb - Advanced Audio Distribution Profile (0x110a)
00001105-0000-1000-8000-00805f9b34fb - Object Push Profile (0x1105) // least invasive one...it seems
00001115-0000-1000-8000-00805f9b34fb - Personal Area Networking Profile (0x1115)
0000112f-0000-1000-8000-00805f9b34fb - Phonebook Access (0x112f) // this works!
00001112-0000-1000-8000-00805f9b34fb - Headset - Audio Gateway (0x1112)
0000111f-0000-1000-8000-00805f9b34fb - Handsfree Audio Gateway (0x111f)
00001132-0000-1000-8000-00805f9b34fb - Message Access Profile (0x1132) // this works too!
00000000-0000-1000-8000-00805f9b34fb - Base UUID (0x0000) // couldn't get this to work :(
Standard UUIDs from Bluetooth spec.