diff --git a/xwords4/android/XWords4/res/menu/games_list_menu.xml b/xwords4/android/XWords4/res/menu/games_list_menu.xml index fd91b60d7..c12368203 100644 --- a/xwords4/android/XWords4/res/menu/games_list_menu.xml +++ b/xwords4/android/XWords4/res/menu/games_list_menu.xml @@ -4,6 +4,9 @@ + %s", ioe.toString() ); - break; - } + try { + s_serverSocket = s_btAdapter. + listenUsingRfcommWithServiceRecord( XWApp.getAppName(), + XWApp.getAppUUID() ); + } catch ( java.io.IOException ioe ) { + DbgUtils.logf( "listenUsingRfcommWithServiceRecord=>%s", + ioe.toString() ); + s_serverSocket = null; + } + while ( null != s_serverSocket ) { BluetoothSocket socket = null; InputStream inStream = null; + int nRead = 0; try { + DbgUtils.logf( "run: calling accept()" ); socket = s_serverSocket.accept(); // blocks + DbgUtils.logf( "run: accept() returned" ); inStream = socket.getInputStream(); - int nRead = inStream.read( buffer ); + nRead = inStream.read( buffer ); DbgUtils.logf( "read %d bytes from BT socket", nRead ); + + // now handle what's on the socket + if ( 0 < nRead && buffer[0] == PING ) { + DbgUtils.logf( "got PING!!!" ); + OutputStream os = socket.getOutputStream(); + os.write( PONG ); + os.flush(); + } + socket.close(); } catch ( java.io.IOException ioe ) { DbgUtils.logf( "accept=>%s", ioe.toString() ); - break; + DbgUtils.logf( "trying again..." ); + continue; } - // now handle what's on the socket - - } // for ( ; ; ) + } if ( null != s_serverSocket ) { try { @@ -70,6 +91,7 @@ public class BTConnection extends BroadcastReceiver { } catch ( java.io.IOException ioe ) { DbgUtils.logf( "close()=>%s", ioe.toString() ); } + s_serverSocket = null; } } } @@ -109,4 +131,40 @@ public class BTConnection extends BroadcastReceiver { } // onReceive + // Test whether there's another device running Crosswords and it + // can respond. + private static class PingThread extends Thread { + public void run() { + BluetoothDevice xwdev = null; + UUID myUUID = XWApp.getAppUUID(); + + Set pairedDevs = s_btAdapter.getBondedDevices(); + DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() ); + for ( BluetoothDevice dev : pairedDevs ) { + BluetoothSocket socket = null; + try { + socket = dev.createRfcommSocketToServiceRecord( myUUID ); + DbgUtils.logf( "PingThread: got socket to device %s", + dev.getName() ); + socket.connect(); + DbgUtils.logf( "PingThread: connected" ); + OutputStream os = socket.getOutputStream(); + os.write( PING ); + DbgUtils.logf( "PingThread: wrote" ); + os.flush(); + socket.close(); + DbgUtils.logf( "PingThread: closed" ); + break; + } catch ( java.io.IOException ioe ) { + DbgUtils.logf( "PingThread: %s", ioe.toString() ); + } + } + } + } + + public static void ping( Context context ) { + PingThread pt = new PingThread(); + pt.start(); + } + } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index 090c5debb..db45898d8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -514,6 +514,11 @@ public class GamesList extends XWListActivity Intent intent; switch (item.getItemId()) { + + case R.id.gamel_menu_btping: + BTConnection.ping( this ); + break; + case R.id.gamel_menu_newgame: startNewGameActivity(); break;