mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
add temporary ping menuitem, and implement by sending message to first
device that's running our service. Logs show messages being received.
This commit is contained in:
parent
0337b97b57
commit
00d2bb2a56
3 changed files with 81 additions and 15 deletions
|
@ -4,6 +4,9 @@
|
|||
<!-- <item android:id="@+id/gamel_menu_view_hidden" -->
|
||||
<!-- android:title="@string/gamel_menu_view_hidden" -->
|
||||
<!-- /> -->
|
||||
<item android:id="@+id/gamel_menu_btping"
|
||||
android:title="bt ping"
|
||||
/>
|
||||
<item android:id="@+id/gamel_menu_newgame"
|
||||
android:title="@string/button_new_game"
|
||||
android:icon="@android:drawable/ic_menu_add"
|
||||
|
|
|
@ -23,46 +23,67 @@ package org.eehouse.android.xw4;
|
|||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothServerSocket;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BTConnection extends BroadcastReceiver {
|
||||
private static BluetoothAdapter s_btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
private static final byte PING = 1;
|
||||
private static final byte PONG = 2;
|
||||
|
||||
private static BluetoothAdapter s_btAdapter =
|
||||
BluetoothAdapter.getDefaultAdapter();
|
||||
private static BluetoothServerSocket s_serverSocket;
|
||||
|
||||
private class BTListener extends Thread {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
byte[] buffer = new byte[1024];
|
||||
for ( ; ; ) {
|
||||
try {
|
||||
s_serverSocket = s_btAdapter.
|
||||
listenUsingRfcommWithServiceRecord( XWApp.getAppName(),
|
||||
XWApp.getAppUUID() );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
DbgUtils.logf( "listenUsingRfcommWithServiceRecord=>%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<BluetoothDevice> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue