fix scan.

This commit is contained in:
Eric House 2012-01-25 18:56:40 -08:00
parent 5ca35254cc
commit da9f206f86
2 changed files with 33 additions and 37 deletions

View file

@ -60,6 +60,7 @@ public class BTConnection extends BroadcastReceiver {
private enum BTCmd { private enum BTCmd {
PING, PING,
PONG, PONG,
SCAN,
INVITE, INVITE,
INVITE_ACCPT, INVITE_ACCPT,
INVITE_DECL, INVITE_DECL,
@ -74,10 +75,12 @@ public class BTConnection extends BroadcastReceiver {
new HashMap<String, String>(); new HashMap<String, String>();
private static class BTQueueElem { private static class BTQueueElem {
// These should perhaps be in some subclasses....
BTCmd m_cmd; BTCmd m_cmd;
byte[] m_msg; byte[] m_msg;
String m_recipient; String m_recipient;
Handler m_handler; Handler m_handler;
ProgressDialog m_progress;
int m_gameID; int m_gameID;
public BTQueueElem( BTCmd cmd ) { m_cmd = cmd; } public BTQueueElem( BTCmd cmd ) { m_cmd = cmd; }
@ -87,9 +90,14 @@ public class BTConnection extends BroadcastReceiver {
public BTQueueElem( BTCmd cmd, byte[] buf, String target, int gameID ) { public BTQueueElem( BTCmd cmd, byte[] buf, String target, int gameID ) {
m_cmd = cmd; m_msg = buf; m_recipient = target; m_gameID = gameID; m_cmd = cmd; m_msg = buf; m_recipient = target; m_gameID = gameID;
} }
public BTQueueElem( BTCmd cmd, String dev, int gameID, Handler handler ) { public BTQueueElem( BTCmd cmd, String dev, int gameID,
Handler handler ) {
m_cmd = cmd; m_recipient = dev; m_gameID = gameID; m_handler = handler; m_cmd = cmd; m_recipient = dev; m_gameID = gameID; m_handler = handler;
} }
public BTQueueElem( BTCmd cmd, ProgressDialog progress,
Handler handler ) {
m_cmd = cmd; m_progress = progress; m_handler = handler;
}
} }
private static LinkedBlockingQueue<BTQueueElem> s_queue; private static LinkedBlockingQueue<BTQueueElem> s_queue;
@ -109,7 +117,10 @@ public class BTConnection extends BroadcastReceiver {
DbgUtils.logf( "run: got %s from queue", elem.m_cmd.toString() ); DbgUtils.logf( "run: got %s from queue", elem.m_cmd.toString() );
switch( elem.m_cmd ) { switch( elem.m_cmd ) {
case PING: case PING:
sendPings( elem ); sendPings( elem.m_handler );
break;
case SCAN:
doScan( elem );
break; break;
case INVITE: case INVITE:
sendInvite( elem ); sendInvite( elem );
@ -266,39 +277,16 @@ public class BTConnection extends BroadcastReceiver {
s_queue.add( new BTQueueElem( BTCmd.INVITE, devName, gameID, handler ) ); s_queue.add( new BTQueueElem( BTCmd.INVITE, devName, gameID, handler ) );
} }
private static class BTScanner extends AsyncTask<Void, Void, Void> {
private Handler m_handler;
private ProgressDialog m_progress;
public BTScanner( Context context, Handler handler ) {
super();
m_handler = handler;
String msg = context.getString( R.string.scan_progress );
m_progress = ProgressDialog.show( context, msg, null, true, true );
}
@Override
protected Void doInBackground( Void... unused )
{
synchronized( s_names ) {
s_names.clear();
}
// new PingThread( null ).run(); // same thread
return null;
}
@Override
protected void onPostExecute( Void unused )
{
m_progress.cancel();
m_handler.obtainMessage( SCAN_DONE ).sendToTarget();
}
}
public static void rescan( Context context, Handler handler ) public static void rescan( Context context, Handler handler )
{ {
new BTScanner( context, handler ).execute(); // This is really gross!!! But only the foreground thread can
// show/close a dialog so we have to pass it to the caller in
// the handler when done. Maybe the caller should put it up
// before calling us????
String msg = context.getString( R.string.scan_progress );
ProgressDialog progress = ProgressDialog.show( context, msg,
null, true, true );
s_queue.add( new BTQueueElem( BTCmd.SCAN, progress, handler ) );
} }
public static String[] listPairedWithXwords() public static String[] listPairedWithXwords()
@ -442,7 +430,7 @@ public class BTConnection extends BroadcastReceiver {
} }
} }
private static void sendPings( BTQueueElem elem ) private static void sendPings( Handler handler )
{ {
Set<BluetoothDevice> pairedDevs = s_btAdapter.getBondedDevices(); Set<BluetoothDevice> pairedDevs = s_btAdapter.getBondedDevices();
DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() ); DbgUtils.logf( "ping: got %d paired devices", pairedDevs.size() );
@ -470,9 +458,8 @@ public class BTConnection extends BroadcastReceiver {
if ( success ) { if ( success ) {
DbgUtils.logf( "got PONG from %s", dev.getName() ); DbgUtils.logf( "got PONG from %s", dev.getName() );
addAddr( dev ); addAddr( dev );
if ( null != elem.m_handler ) { if ( null != handler ) {
elem.m_handler handler.obtainMessage( GOT_PONG, dev.getName() )
.obtainMessage( GOT_PONG, dev.getName() )
.sendToTarget(); .sendToTarget();
} }
} }
@ -492,6 +479,13 @@ public class BTConnection extends BroadcastReceiver {
os.flush(); os.flush();
} }
private static void doScan( BTQueueElem elem )
{
sendPings( null );
elem.m_handler.obtainMessage( SCAN_DONE, elem.m_progress )
.sendToTarget();
}
private static void addAddr( BluetoothDevice dev ) private static void addAddr( BluetoothDevice dev )
{ {
synchronized( s_names ) { synchronized( s_names ) {

View file

@ -23,6 +23,7 @@ package org.eehouse.android.xw4;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -291,6 +292,7 @@ public class NewGameActivity extends XWActivity
case BTConnection.CONNECT_FAILED: case BTConnection.CONNECT_FAILED:
break; break;
case BTConnection.SCAN_DONE: case BTConnection.SCAN_DONE:
((ProgressDialog)msg.obj).cancel();
showDialog( PICK_BTDEV_DLG ); showDialog( PICK_BTDEV_DLG );
break; break;
} }