블루투스 채팅 예제를 이용해 블루투스 다중 접속 코드를 짜고 있습니다. 7개의 UUID를 사용하고 여러대의 다른 폰에서 한 폰으로 접속하려하는데 문제가 있습니다. 처음 접속은 문제 없이 같은 UUID를 사용하여 연결됩니다. 그런데 2번째 부터는 서버는 2번째 UUID(1번은 처음 접속에 사용)를 사용하는데 클라이언트는 첫번째 UUID를 사용하면서 연결이 안됩니다. 아예 실패가 뜨면 반복문으로 될때까지 시도하게 코드를 짰는데 2번째 클라이언트는 연결이 안됐는데 연결됐다고 뜨면서 통신 자체가 안됩니다. 도와주세요ㅜㅜ 코드는 길어서 안올려지네요ㅠㅠ 우선 문제가 되는 connect부분만 올리겠습니다.
/**
* Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect
*/
public synchronized void connect(BluetoothDevice device) {
if (D) Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Create a new thread and attempt to connect to each UUID one-by-one.
for (int i = 0; i < 7; i++) {
UUID_CHECK[i] = 1;
try {
mConnectThread = new ConnectThread(device, mUuids.get(i));
mConnectThread.start();
setState(STATE_CONNECTING);
Log.d(TAG, "ConnectedThread i :" + i);
break;
} catch (Exception e) {
}
}
}
/**
* Start the ConnectedThread to begin managing a Bluetooth connection
* @param socket The BluetoothSocket on which the connection was made
* @param device The BluetoothDevice that has been connected
*/
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (D) Log.d(TAG, "connected");
//Commented out all the cancellations of existing threads, since we want multiple connections.
/*
// Cancel the thread that completed the connection
if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}
// Cancel the accept thread because we only want to connect to one device
if (mAcceptThread != null) {mAcceptThread.cancel(); mAcceptThread = null;}
*/
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Add each connected thread to an array
mConnThreads.add(mConnectedThread);
// Send the name of the connected device back to the UI Activity
Message msg = mHandler.obtainMessage(MasterActivity.MESSAGE_DEVICE_NAME);
Bundle bundle = new Bundle();
bundle.putString(MasterActivity.DEVICE_NAME, device.getName());
msg.setData(bundle);
mHandler.sendMessage(msg);
setState(STATE_CONNECTED);
}
/**
* This thread runs while attempting to make an outgoing connection
* with a device. It runs straight through; the connection either
* succeeds or fails.
*/
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private UUID tempUuid;
public ConnectThread(BluetoothDevice device, UUID uuidToTry) {
mmDevice = device;
BluetoothSocket tmp = null;
tempUuid = uuidToTry;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(uuidToTry);
} catch (IOException e) {
Log.e(TAG, "create() failed", e);
}
mmSocket = tmp;
}
public void run() {
Log.i(TAG, "BEGIN mConnectThread");
setName("ConnectThread");
// 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) {
if (tempUuid.toString().contentEquals(mUuids.get(6).toString())) {
connectionFailed();
}
// Close the socket
try {
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "unable to close() socket during connection failure", e2);
}
// Start the service over to restart listening mode
BluetoothChatService.this.start();
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice);
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MasterActivity.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost_info();
//connectionLost();
break;
}
}
}