我正在使用BluetoothChat示例(3.1)代码示例来测试两个Samsung Galaxy Tabs之间的通信。我知道之前已经多次询问过,但是当我这样做时,我仍然感到困惑:
BluetoothSocket mmSocket
...
mmSocket.connect();
这会导致拒绝连接。我已经多次尝试配对和取消配对程序的内外,没有任何结果。有没有人有任何想法?
更新(从答案中添加):
客户端和客户端的代码服务器是一样的。这些实际上是两个三星Galaxy Tabs(3.1)。尝试连接到远程设备时发生错误。
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
if (secure) {
//tmp = device.createRfcommSocketToServiceRecord(
//MY_UUID_SECURE);
// tmp = device.createRfcommSocketToServiceRecord(
// UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
} else {
Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] {int.class});
tmp = (BluetoothSocket)m.invoke(device, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
} catch (Exception e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
}
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 {
Log.e("Ali", "Error interacting with remote device. Here is the cause: "+ e.getMessage() );
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);
}
听力部分如下:
private class AcceptThread extends Thread {
// The local server socket
private final BluetoothServerSocket mmServerSocket;
private String mSocketType;
public AcceptThread(boolean secure) {
BluetoothServerSocket tmp = null;
mSocketType = secure ? "Secure":"Insecure";
// Create a new listening server socket
try {
if (secure) {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE,UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} else {
tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(
NAME_INSECURE, UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run() {
if (D) Log.d(TAG, "Socket Type: " + mSocketType +
"BEGIN mAcceptThread" + this);
setName("AcceptThread" + mSocketType);
BluetoothSocket socket = null;
// Listen to the server socket if we're not connected
while (mState != STATE_CONNECTED) {
try {
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e("Ali", "Error while connecting to device because: "+ e.getStackTrace());
break;
}
// If a connection was accepted
if (socket != null) {
synchronized (BluetoothChatService.this) {
switch (mState) {
case STATE_LISTEN:
case STATE_CONNECTING:
// Situation normal. Start the connected thread.
connected(socket, socket.getRemoteDevice(),
mSocketType);
break;
case STATE_NONE:
case STATE_CONNECTED:
// Either not ready or already connected. Terminate new socket.
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close unwanted socket", e);
}
break;
}
}
}
}
if (D) Log.i(TAG, "END mAcceptThread, socket Type: " + mSocketType);
}
最后,你有了logcat:
答案 0 :(得分:0)
有不同的理由被拒绝: - 错误的rfcomm端口 - 黑名单 - 错误的另一台设备的mac地址