Commit d323e8c7 authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- check if it is a valid move

- fix bugs
parent e1b27138
......@@ -52,7 +52,7 @@ public class LobbyActivity extends Activity {
if (bundle.containsKey(BLEHandler.BLE_EXTRA_DATA) && bundle.containsKey(BLEHandler.BLE_EXTRA_DATA_ADDRESS)) {
String name = bundle.getString(BLEHandler.BLE_EXTRA_DATA);
final String addr = bundle.getString(BLEHandler.BLE_EXTRA_DATA_ADDRESS);
String msg = name + " " + getResources().getString(R.string.periphfound);
String msg = " \"" + name + "\" " + getResources().getString(R.string.periphfound);
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(msg).setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
......@@ -71,7 +71,7 @@ public class LobbyActivity extends Activity {
case BLEHandler.BLE_CONNECTION_PERIPHL_FOUND_CENTRAL_ACTION: {
Bundle bundle = intent.getExtras();
String name = bundle.getString(BLEHandler.BLE_EXTRA_DATA);
String msg = getResources().getString(R.string.centalfoundp1) + name + getResources().getString(R.string.centalfoundp2);
String msg = getResources().getString(R.string.centalfoundp1) + " \"" + name + "\" " + getResources().getString(R.string.centalfoundp2);
new AlertDialog.Builder(LobbyActivity.this).setTitle(getResources().getString(R.string.title)).setMessage(msg).setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
......
......@@ -169,7 +169,15 @@ public class GameManager {
broadcastStatus(GAMEMANAGER_START_NEW_GAME);
}
public boolean isValidTile(int tileNum) {
return !(localPlayer.tileNumPlayed.contains(tileNum) || remotePlayer.tileNumPlayed.contains(tileNum));
}
public void playerMove(int playerId, int tileNum) {
if (!isValidTile(tileNum)) {
return;
}
if (isHost) {
numFilledBoardSpots++;
if (localPlayer.playerId == playerId) {
......
......@@ -234,6 +234,10 @@ public class BLEHandler {
} else {
setIsAdvertise(false);
if (m_bluetoothGattServer != null) {
if (m_centralDevice != null) {
m_bluetoothGattServer.cancelConnection(m_centralDevice);
m_centralDevice = null;
}
m_bluetoothGattServer.close();
m_bluetoothGattServer = null;
}
......@@ -272,7 +276,7 @@ public class BLEHandler {
public int getConnectionCount() {
if (isCentral()) {
return m_peripheralDevices.size();
return m_peripheralDevices == null ? 0 : m_peripheralDevices.size();
} else {
return m_centralDevice == null ? 0 : 1;
}
......@@ -303,22 +307,25 @@ public class BLEHandler {
public void stopScan() {
if (m_bluetoothAdapter != null && m_bluetoothAdapter.isEnabled()) {
scanLeDevice(false);
m_scanHandler.removeCallbacks(m_scanRunnable);
}
}
private Runnable m_scanRunnable = new Runnable() {
@Override
public void run() {
if (m_isScanningOrAdvertising) {
m_isScanningOrAdvertising = false;
m_LEScanner.stopScan(m_scanCallback);
broadcastStatus(BLE_CONNECTION_AUTO_STOP_SCAN_ACTION);
}
}
};
private void scanLeDevice(final boolean enable) {
if (enable && !m_isScanningOrAdvertising) {
// Stops scanning after a pre-defined scan period.
m_scanHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (m_isScanningOrAdvertising) {
m_isScanningOrAdvertising = false;
m_LEScanner.stopScan(m_scanCallback);
broadcastStatus(BLE_CONNECTION_AUTO_STOP_SCAN_ACTION);
}
}
}, SCAN_PERIOD);
m_scanHandler.postDelayed(m_scanRunnable, SCAN_PERIOD);
// android.os.Build.VERSION.SDK_INT> 21
m_isScanningOrAdvertising = true;
......@@ -537,9 +544,9 @@ public class BLEHandler {
i.putExtra(BLE_EXTRA_DATA, characteristic.getValue());
i.putExtra(BLE_EXTRA_DATA_ADDRESS, gatt.getDevice().getAddress());
if (m_currentActivity == null) {
Log.d(TAG, "onCharacteristicChanged: current activity is null, sleep 1000");
Log.d(TAG, "onCharacteristicChanged: current activity is null, sleep 700ms");
try {
Thread.sleep(1000);
Thread.sleep(700);
} catch (Exception ex) {
ex.printStackTrace();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment