当我尝试连接来自不同制造商的两部手机时,这些设备没有连接。我开发了一个应用程序,其中两个Xperia手机和蓝牙连接(编辑)很好,(与三星设备相同,它们之间)。但是,当我尝试连接到Sony Xperia和三星手机时,请勿连接。
当我制作这个应用程序时,我将BluetoothChatService类表格从Google蓝牙示例(版本8)复制到我的项目(我没有修改此文件),但我有上述问题。所以,我下载了第10个api版本的Google BluetoothChat示例,他们第一次连接,但下次没有。 问题是索尼Xperia手机没有记录来自三星手机的bluetooht设备信息和三星RECORDS这个信息。
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 examples,其中一部就解决了这个问题。
现在,我可以很好地连接Sony Xperia U和三星Galaxy SII,但是当我尝试从Samsung SII连接到Xperia U时,请不要连接,因为IOExpection中出现“拒绝连接”消息。
这是来自ConnectThread类的Google BluetoothChatService文件示例的副本。
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);
}