当我尝试连接不同制造商的两部手机时,这些设备无法连接。我开发了一个应用程序,它可以很好地连接两部 Xperia 手机和蓝牙(与三星设备相同,它们之间)。但是,当我尝试连接到索尼 Xperia 和三星手机时,请不要连接。
当我制作这个应用程序时,我将 BluetoothChatService 类从 Google BluetoothChat 示例(版本 8)复制到我的项目(我没有修改此文件),但我遇到了上述问题。所以,我下载了第 10 个 api 版本的 Google BluetoothChat 示例,他们第一次连接,但下一次没有。问题是索尼 Xperia 手机不记录来自三星手机的蓝牙设备信息,三星记录此信息。
01-18 11:29:07.036: D/BluetoothService(1744): B0:EC:71:E3:FB:41 bond state 11 -> 10 (9)
01-18 11:29:07.066: V/BluetoothEventRedirector(18829): Received android.bluetooth.device.action.BOND_STATE_CHANGED
01-18 11:29:07.066: E/CachedBluetoothDeviceManager(18829): Got bonding state changed for B0:EC:71:E3:FB:41, but we have no record of that device.
01-18 11:29:07.066: W/CachedBluetoothDeviceManager(18829): showUnbondMessage: Not displaying any message for reason:9
下一步是取消配对两部手机,所以我尝试了这些stackoverflow 示例,其中一个解决了这个问题。
现在,我可以很好地从 Sony Xperia U 连接到三星 Galaxy SII,但是当我尝试从三星 SII 连接到 Xperia U 时,由于 IOExpection 中出现“拒绝连接”消息而无法连接。
这是 Google BluetoothChatService 文件示例的 ConnectThread 类的副本。
public void run() {
Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
Log.i("INFO", e.getMessage());
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() " + mSocketType +
" socket during connection failure", e2);
}
connectionFailed();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice, mSocketType);
}
更新 1:
我一直在考虑这个问题,我已经评论了 BluetoothChatService 中的一些行,并且三星和 Xperia 设备之间的连接现在是正确的。连接建立了,但是它通过异常代码并完成了连接,所以我注释了异常代码。但是,当我尝试连接 Xperia 和 Galaxy Nexus 时,出现了问题。
ConnectThread 类中的 Run 方法是:
public void run() {
Log.i(TAG, "BEGIN mConnectThread SocketType:" + mSocketType);
setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
// This is a blocking call and will only return on a
// successful connection or an exception
mmSocket.connect();
} catch (IOException e) {
// Close the socket
// try {
// mmSocket.close();
// } catch (IOException e2) {
// Log.e(TAG, "unable to close() " + mSocketType +
// " socket during connection failure", e2);
// }
// connectionFailed();
// return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice, mSocketType);
}