pass bt address as well as name, and use when available -- since OS

doesn't provide ready name->addr translation.
This commit is contained in:
Eric House 2012-02-01 06:50:19 -08:00
parent afac1cc5bd
commit 5e330595fc
3 changed files with 39 additions and 30 deletions

View file

@ -68,6 +68,8 @@ public class BTService extends Service {
private static final String CMD_STR = "CMD"; private static final String CMD_STR = "CMD";
private static final String MSG_STR = "MSG"; private static final String MSG_STR = "MSG";
private static final String TARGET_STR = "TRG"; private static final String TARGET_STR = "TRG";
private static final String ADDR_STR = "ADR";
private static final String GAMEID_STR = "GMI"; private static final String GAMEID_STR = "GMI";
private static final String LANG_STR = "LNG"; private static final String LANG_STR = "LNG";
@ -94,19 +96,23 @@ public class BTService extends Service {
BTCmd m_cmd; BTCmd m_cmd;
byte[] m_msg; byte[] m_msg;
String m_recipient; String m_recipient;
String m_addr;
int m_gameID; int m_gameID;
int m_lang; int m_lang;
int m_nPlayersT; int m_nPlayersT;
int m_nPlayersH; int m_nPlayersH;
public BTQueueElem( BTCmd cmd ) { m_cmd = cmd; } public BTQueueElem( BTCmd cmd ) { m_cmd = cmd; }
public BTQueueElem( BTCmd cmd, String target, int gameID, public BTQueueElem( BTCmd cmd, String targetName, String targetAddr,
int lang, int nPlayersT, int nPlayersH ) { int gameID, int lang, int nPlayersT,
this( cmd, null, target, gameID ); int nPlayersH ) {
this( cmd, null, targetName, targetAddr, gameID );
m_lang = lang; m_nPlayersT = nPlayersT; m_nPlayersH = nPlayersH; m_lang = lang; m_nPlayersT = nPlayersT; m_nPlayersH = nPlayersH;
} }
public BTQueueElem( BTCmd cmd, byte[] buf, String target, int gameID ) { public BTQueueElem( BTCmd cmd, byte[] buf, String targetName,
m_cmd = cmd; m_msg = buf; m_recipient = target; m_gameID = gameID; String targetAddr, int gameID ) {
m_cmd = cmd; m_msg = buf; m_recipient = targetName;
m_addr = targetAddr; m_gameID = gameID;
} }
} }
@ -146,13 +152,14 @@ public class BTService extends Service {
context.startService( intent ); context.startService( intent );
} }
public static void inviteRemote( Context context, String host, int gameID, public static void inviteRemote( Context context, String hostName,
int lang, int nPlayersT, int nPlayersH ) int gameID, int lang, int nPlayersT,
int nPlayersH )
{ {
Intent intent = new Intent( context, BTService.class ); Intent intent = new Intent( context, BTService.class );
intent.putExtra( CMD_STR, INVITE ); intent.putExtra( CMD_STR, INVITE );
intent.putExtra( GAMEID_STR, gameID ); intent.putExtra( GAMEID_STR, gameID );
intent.putExtra( TARGET_STR, host ); intent.putExtra( TARGET_STR, hostName );
intent.putExtra( LANG_STR, lang ); intent.putExtra( LANG_STR, lang );
intent.putExtra( NTO_STR, nPlayersT ); intent.putExtra( NTO_STR, nPlayersT );
intent.putExtra( NHE_STR, nPlayersH ); intent.putExtra( NHE_STR, nPlayersH );
@ -160,17 +167,19 @@ public class BTService extends Service {
context.startService( intent ); context.startService( intent );
} }
public static int enqueueFor( Context context, byte[] buf, String target, public static int enqueueFor( Context context, byte[] buf,
String targetName, String targetAddr,
int gameID ) int gameID )
{ {
Intent intent = new Intent( context, BTService.class ); Intent intent = new Intent( context, BTService.class );
intent.putExtra( CMD_STR, SEND ); intent.putExtra( CMD_STR, SEND );
intent.putExtra( MSG_STR, buf ); intent.putExtra( MSG_STR, buf );
intent.putExtra( TARGET_STR, target ); intent.putExtra( TARGET_STR, targetName );
intent.putExtra( ADDR_STR, targetAddr );
intent.putExtra( GAMEID_STR, gameID ); intent.putExtra( GAMEID_STR, gameID );
context.startService( intent ); context.startService( intent );
DbgUtils.logf( "got %d bytes for %s, gameID %d", buf.length, target, DbgUtils.logf( "got %d bytes for %s (%s), gameID %d", buf.length,
gameID ); targetName, targetAddr, gameID );
return buf.length; return buf.length;
} }
@ -209,19 +218,22 @@ public class BTService extends Service {
case INVITE: case INVITE:
int gameID = intent.getIntExtra( GAMEID_STR, -1 ); int gameID = intent.getIntExtra( GAMEID_STR, -1 );
String target = intent.getStringExtra( TARGET_STR ); String target = intent.getStringExtra( TARGET_STR );
String addr = addrFor( target );
int lang = intent.getIntExtra( LANG_STR, -1 ); int lang = intent.getIntExtra( LANG_STR, -1 );
int nPlayersT = intent.getIntExtra( NTO_STR, -1 ); int nPlayersT = intent.getIntExtra( NTO_STR, -1 );
int nPlayersH = intent.getIntExtra( NHE_STR, -1 ); int nPlayersH = intent.getIntExtra( NHE_STR, -1 );
m_queue.add( new BTQueueElem( BTCmd.INVITE, target, gameID, m_queue.add( new BTQueueElem( BTCmd.INVITE, target, addr,
lang, nPlayersT, nPlayersH) ); gameID, lang,
nPlayersT, nPlayersH ) );
break; break;
case SEND: case SEND:
byte[] buf = intent.getByteArrayExtra( MSG_STR ); byte[] buf = intent.getByteArrayExtra( MSG_STR );
target = intent.getStringExtra( TARGET_STR ); target = intent.getStringExtra( TARGET_STR );
addr = intent.getStringExtra( ADDR_STR );
gameID = intent.getIntExtra( GAMEID_STR, -1 ); gameID = intent.getIntExtra( GAMEID_STR, -1 );
if ( -1 != gameID ) { if ( -1 != gameID ) {
m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf, target, m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf, target,
gameID ) ); addr, gameID ) );
} }
break; break;
default: default:
@ -376,8 +388,7 @@ public class BTService extends Service {
private void sendMsg( BTQueueElem elem ) private void sendMsg( BTQueueElem elem )
{ {
try { try {
BluetoothDevice dev = BluetoothDevice dev = m_adapter.getRemoteDevice( elem.m_addr );
m_adapter.getRemoteDevice( addrFor( elem.m_recipient ) );
BluetoothSocket socket = BluetoothSocket socket =
dev.createRfcommSocketToServiceRecord( XWApp.getAppUUID() ); dev.createRfcommSocketToServiceRecord( XWApp.getAppUUID() );
if ( null != socket ) { if ( null != socket ) {
@ -510,7 +521,9 @@ public class BTService extends Service {
int nPlayersH = is.readInt(); int nPlayersH = is.readInt();
BluetoothDevice host = socket.getRemoteDevice(); BluetoothDevice host = socket.getRemoteDevice();
GameUtils.makeNewBTGame( context, gameID, host.getName(), CommsAddrRec addr = new CommsAddrRec( context, host.getName(),
host.getAddress() );
GameUtils.makeNewBTGame( context, gameID, addr,
lang, nPlayersT, nPlayersH ); lang, nPlayersT, nPlayersH );
addAddr( host ); addAddr( host );
@ -585,8 +598,9 @@ public class BTService extends Service {
public int transportSend( byte[] buf, final CommsAddrRec addr, int gameID ) public int transportSend( byte[] buf, final CommsAddrRec addr, int gameID )
{ {
String target = addr.bt_hostName; m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf,
m_queue.add( new BTQueueElem( BTCmd.MESG_SEND, buf, target, gameID ) ); addr.bt_hostName, addr.bt_btAddr,
gameID ) );
return buf.length; return buf.length;
} }

View file

@ -390,7 +390,7 @@ public class CommsTransport implements TransportProcs,
case COMMS_CONN_BT: case COMMS_CONN_BT:
String hostName = m_addr.bt_hostName; String hostName = m_addr.bt_hostName;
nSent = BTService.enqueueFor( m_context, buf, m_addr.bt_hostName, nSent = BTService.enqueueFor( m_context, buf, m_addr.bt_hostName,
gameID ); m_addr.bt_btAddr, gameID );
break; break;
default: default:
Assert.fail(); Assert.fail();

View file

@ -384,7 +384,7 @@ public class GameUtils {
int[] lang, int[] lang,
int nPlayersT, int nPlayersH, int nPlayersT, int nPlayersH,
String inviteID, int gameID, String inviteID, int gameID,
boolean isHost, String btHost ) boolean isHost )
{ {
long rowid = -1; long rowid = -1;
@ -398,8 +398,6 @@ public class GameUtils {
} }
if ( isHost ) { if ( isHost ) {
gi.serverRole = DeviceRole.SERVER_ISSERVER; gi.serverRole = DeviceRole.SERVER_ISSERVER;
} else if ( null != btHost ) {
addr.bt_hostName = btHost;
} }
// Will need to add a setNPlayers() method to gi to make this // Will need to add a setNPlayers() method to gi to make this
// work // work
@ -424,7 +422,7 @@ public class GameUtils {
addr.ip_relay_invite = room; addr.ip_relay_invite = room;
return makeNewMultiGame( context, addr, lang, nPlayersT, return makeNewMultiGame( context, addr, lang, nPlayersT,
nPlayersH, inviteID, 0, false, null ); nPlayersH, inviteID, 0, false );
} }
public static long makeNewNetGame( Context context, String room, public static long makeNewNetGame( Context context, String room,
@ -441,17 +439,14 @@ public class GameUtils {
} }
public static long makeNewBTGame( Context context, int gameID, public static long makeNewBTGame( Context context, int gameID,
String hostName, int lang, CommsAddrRec addr, int lang,
int nPlayersT, int nPlayersH ) int nPlayersT, int nPlayersH )
{ {
long rowid = -1; long rowid = -1;
CommsAddrRec addr =
new CommsAddrRec( context,
CommsAddrRec.CommsConnType.COMMS_CONN_BT );
int[] langa = { lang }; int[] langa = { lang };
return makeNewMultiGame( context, addr, langa, nPlayersT, nPlayersH, return makeNewMultiGame( context, addr, langa, nPlayersT, nPlayersH,
null, gameID, null == hostName, hostName ); null, gameID, null == addr );
} }
public static void launchInviteActivity( Context context, public static void launchInviteActivity( Context context,