Hy, Мы работаем над многопользовательской игрой для Android через Bluetooth. Это многопользовательская игра LUDO, в которой 4 игрока соединяются друг с другом и играют в игру.
Мы застряли в связи с подключением 3-го и 4-го игрока.
private OnMaxConnectionsReachedListener maxConnectionsListener = new OnMaxConnectionsReachedListener() {
public void OnMaxConnectionsReached() {
}
};
private OnIncomingConnectionListener connectedListener = new OnIncomingConnectionListener() {
public void OnIncomingConnection(String device) {
//rivalDevice = device;
if(index < 1000)
{
clients[index] = device;
index++;
Log.d("Khawar", "Khawar"+device);
if(index == 1)
{
//blue.setText(clients[0]);
mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]);
}
else if(index == 2)
{
//blue.setText(clients[0]);
//red.setText(clients[1]);
mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]);
mConnection.sendMessage(clients[1], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]);
}
else if(index==3)
{
//blue.setText(clients[0]);
//red.setText(clients[1]);
//yellow.setText(clients[2]);
mConnection.sendMessage(clients[0], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
mConnection.sendMessage(clients[1], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
mConnection.sendMessage(clients[2], "ConnectionList--Ludofyp309509--"+server+"--Ludofyp309509--"+clients[0]+"--Ludofyp309509--"+clients[1]+"--Ludofyp309509--"+clients[2]);
}
}
//Toast.makeText(getBaseContext(), "Client found" + device , Toast.LENGTH_LONG).show();
// WindowManager w = getWindowManager();
// Display d = w.getDefaultDisplay();
// int width = d.getWidth();
//int height = d.getHeight();
}
};
private OnConnectionLostListener disconnectedListener = new OnConnectionLostListener() {
public void OnConnectionLost(String device) {
class displayConnectionLostAlert implements Runnable {
public void run() {
Builder connectionLostAlert = new Builder(self);
connectionLostAlert.setTitle("Connection lost");
connectionLostAlert
.setMessage("Your connection with the other player has been lost.");
connectionLostAlert.setPositiveButton("Ok", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
connectionLostAlert.setCancelable(false);
try {
connectionLostAlert.show();
} catch (BadTokenException e){
Log.v("TAG", "Exception throws here");
}
}
}
self.runOnUiThread(new displayConnectionLostAlert());
}
};
private OnConnectionServiceReadyListener serviceReadyListener = new OnConnectionServiceReadyListener() {
public void OnConnectionServiceReady() {
if (mType == 0) {
mConnection.startServer(1, connectedListener, maxConnectionsListener,
dataReceivedListener, disconnectedListener);
self.setTitle("LudoOverAndroid: " + mConnection.getName() + "-" + mConnection.getAddress());
server = mConnection.getAddress();
green.setText(server);
Log.v("1", server);
// Intent Game = new Intent(self, Board.class);
// .putExtra("TYPE", 1);
// startActivity(Game);
} else {
self.setTitle("LudoOverAndroid: " + mConnection.getName() + "-" + mConnection.getAddress());
Intent serverListIntent = new Intent(self, ServerListActivity.class);
startActivityForResult(serverListIntent, SERVER_LIST_RESULT_CODE);
clients[index] = mConnection.getAddress();
// Intent Game = new Intent(self, Board.class);
// .putExtra("TYPE", 1);
// startActivity(Game);
}
}
};
Выше приведен пример кода, в котором устанавливается соединение. Но в классе Connection Services у нас есть следующий код
public int connect(String srcApp, String device) throws RemoteException {
if (mApp.length() > 0) {
Log.v("a", "connection service->connect->mApp.length");
return Connection.FAILURE;
}
mApp = srcApp;
BluetoothDevice myBtServer = mBtAdapter.getRemoteDevice(device);
BluetoothSocket myBSock = null;
Log.v("a", "Yaah aya hay aik baar nahe bar bar :");
for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) {
for (int j = 0; j < 3 && myBSock == null; j++) {
myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
if (myBSock == null) {
try {
Log.v("a", "connection service->connect->myBSock==NULL thread sleep");
Thread.sleep(200);
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException in connect", e);
}
}
}
}
if (myBSock == null) {
Log.v("a", "connection service->connect->myBSock==NULLss");
return Connection.FAILURE;
}
mBtSockets.put(device, myBSock);
mBtDeviceAddresses.add(device);
Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(device));
mBtStreamWatcherThread.start();
mBtStreamWatcherThreads.put(device, mBtStreamWatcherThread);
return Connection.SUCCESS;
}
Мобильный телефон при подключении к 3-му или 4-му устройству возвращает myBSock==null. Но если коды работают правильно, он должен вернуть адрес устройства и добавить mBtDeviceAddresses.add(device); в список серверов.
Пожалуйста, помогите нам решить проблему.
заранее спасибо