我有两个应用程序:一个在Android 7.1.2(一个摄像头)的设备中充当服务器,另一个在Android 7.0中充当客户端。 (三星Galaxy S7)。我进行了配对,因此它们都出现在彼此的发现方法中。
客户确实:
...
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mmSocket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
...
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
connectException.printStackTrace();
}
...
服务器确实:
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mmServerSocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("name",UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
...
BluetoothSocket socket = null;
boolean connected = false;
// Keep listening until exception occurs or a socket is returned.
while (true) {
if(!connected) {
try {
MyLog.d(TAG, "accepting");
socket = mmServerSocket.accept();
} catch (IOException e) {
MyLog.e(TAG, "Socket's accept() method failed");
break;
}
}
...
}
当我在两部普通Android手机中测试我的应用程序时,以及当我在手机中使用我的服务器而将我的相机用作客户端时,一切正常。
但如果我尝试在相机和手机中的客户端中运行服务器,那么相机会卡在
socket = mmServerSocket.accept();
并且客户端在
中返回IOExceptionmmSocket.connect();
java.io.IOException:读取失败,socket可能关闭或超时,读取 ret:-1
我想这与相机蓝牙不接受连接有关,但我不太了解它,当角色切换并且相机运行客户端应用程序时,一切正常。
有什么想法吗?
谢谢!