Java 区别于;“超出范围”;或;在范围内,但没有侦听服务器套接字“;?(蓝牙) 问题
我如何区分无法与远程Android设备建立蓝牙连接,因为:Java 区别于;“超出范围”;或;在范围内,但没有侦听服务器套接字“;?(蓝牙) 问题,java,android,sockets,bluetooth,android-bluetooth,Java,Android,Sockets,Bluetooth,Android Bluetooth,我如何区分无法与远程Android设备建立蓝牙连接,因为: 场景1:远程设备超出范围,或其蓝牙功能被禁用 场景2:远程设备在范围内,但远程设备上没有可接受我的连接的服务器套接字 我试过的 我无法区分连接时抛出的异常,因为它在两种情况下都抛出相同的异常: java.io.IOException: read failed, socket might closed or timeout, read ret -1 我无法使用fetchUUIDSWithDP()检查远程设备是否支持我的UUID,因
- 场景1:远程设备超出范围,或其蓝牙功能被禁用
- 场景2:远程设备在范围内,但远程设备上没有可接受我的连接的服务器套接字
java.io.IOException: read failed, socket might closed or timeout, read ret -1
fetchUUIDSWithDP()
检查远程设备是否支持我的UUID,因为它在两种情况下的行为方式相同…:
此API是异步的,发送{@link#ACTION_UUID}意图,远程端支持UUID。如果在获取SDP记录时出错,或者如果该过程需要很长时间,则会将{@link#ACTION_UUID}意图与缓存中当前存在的UUID一起发送
根据研究,它的行为似乎也有点不可预测sdpSearch
来区分两者,因为这是在API 23中添加的,我希望能够支持API 19您可以通过尝试连接到通常在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);
}
}
我曾经在我的一台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 :(