avoid crashes when BT turned off

Test at enqueueWork() and onHandleWorkImpl(). Seems to do it. Maybe
turning it off mid-send will still crash...
This commit is contained in:
Eric House 2019-02-08 22:22:03 -08:00
parent 601eaf395c
commit 2afce586a3

View file

@ -292,17 +292,15 @@ public class BTService extends XWJIService {
private static void enqueueWork( Context context, Intent intent ) private static void enqueueWork( Context context, Intent intent )
{ {
enqueueWork( context, BTService.class, sJobID, intent ); if ( BTEnabled() ) {
Log.d( TAG, "called enqueueWork(cmd=%s)", enqueueWork( context, BTService.class, sJobID, intent );
cmdFrom( intent, BTAction.values() ) ); }
} }
public static void onACLConnected( Context context ) public static void onACLConnected( Context context )
{ {
Log.d( TAG, "onACLConnected()" );
enqueueWork( context, enqueueWork( context,
getIntentTo( context, BTAction.ACL_CONN ) ); getIntentTo( context, BTAction.ACL_CONN ) );
} }
public static void radioChanged( Context context, boolean cameOn ) public static void radioChanged( Context context, boolean cameOn )
@ -417,115 +415,117 @@ public class BTService extends XWJIService {
@Override @Override
void onHandleWorkImpl( Intent intent, XWJICmds jicmd, long timestamp ) void onHandleWorkImpl( Intent intent, XWJICmds jicmd, long timestamp )
{ {
BTAction cmd = (BTAction)jicmd; if ( BTEnabled() ) {
switch( cmd ) { BTAction cmd = (BTAction)jicmd;
case ACL_CONN: // just forces onCreate to run switch( cmd ) {
break; case ACL_CONN: // just forces onCreate to run
break;
case START_BACKGROUND: case START_BACKGROUND:
noteLastUsed( this ); // prevent timer from killing immediately noteLastUsed( this ); // prevent timer from killing immediately
setTimeoutTimer(); setTimeoutTimer();
break; break;
case SCAN: case SCAN:
int timeout = intent.getIntExtra( SCAN_TIMEOUT_KEY, -1 ); int timeout = intent.getIntExtra( SCAN_TIMEOUT_KEY, -1 );
add( new BTQueueElem( BTCmd.SCAN, timeout ) ); add( new BTQueueElem( BTCmd.SCAN, timeout ) );
break; break;
case INVITE: case INVITE:
String jsonData = intent.getStringExtra( GAMEDATA_KEY ); String jsonData = intent.getStringExtra( GAMEDATA_KEY );
NetLaunchInfo nli = NetLaunchInfo.makeFrom( this, jsonData ); NetLaunchInfo nli = NetLaunchInfo.makeFrom( this, jsonData );
Log.i( TAG, "handleCommand: nli: %s", nli ); Log.i( TAG, "handleCommand: nli: %s", nli );
String btAddr = intent.getStringExtra( ADDR_KEY ); String btAddr = intent.getStringExtra( ADDR_KEY );
add( new BTQueueElem( BTCmd.INVITE, nli, btAddr ) ); add( new BTQueueElem( BTCmd.INVITE, nli, btAddr ) );
break; break;
case PINGHOST: case PINGHOST:
btAddr = intent.getStringExtra( ADDR_KEY ); btAddr = intent.getStringExtra( ADDR_KEY );
int gameID = intent.getIntExtra( GAMEID_KEY, 0 ); int gameID = intent.getIntExtra( GAMEID_KEY, 0 );
add( new BTQueueElem( BTCmd.PING, btAddr, gameID ) ); add( new BTQueueElem( BTCmd.PING, btAddr, gameID ) );
break; break;
case SEND: case SEND:
byte[] buf = intent.getByteArrayExtra( MSG_KEY ); byte[] buf = intent.getByteArrayExtra( MSG_KEY );
btAddr = intent.getStringExtra( ADDR_KEY ); btAddr = intent.getStringExtra( ADDR_KEY );
gameID = intent.getIntExtra( GAMEID_KEY, -1 );
if ( -1 != gameID ) {
add( new BTQueueElem( BTCmd.MESG_SEND, buf,
btAddr, gameID ) );
}
break;
case RADIO:
boolean cameOn = intent.getBooleanExtra( RADIO_KEY, false );
MultiEvent evt = cameOn? MultiEvent.BT_ENABLED
: MultiEvent.BT_DISABLED;
mHelper.postEvent( evt );
if ( cameOn ) {
GameUtils.resendAllIf( this, CommsConnType.COMMS_CONN_BT );
} else {
ConnStatusHandler.updateStatus( this, null,
CommsConnType.COMMS_CONN_BT,
false );
stopListener();
// stopSender();
stopSelf();
}
break;
case REMOVE:
gameID = intent.getIntExtra( GAMEID_KEY, -1 );
btAddr = intent.getStringExtra( ADDR_KEY );
add( new BTQueueElem( BTCmd.MESG_GAMEGONE, btAddr, gameID ) );
break;
case MAKE_OR_NOTIFY:
int socketRef = intent.getIntExtra( SOCKET_REF, -1 );
BluetoothSocket socket = socketForRef( socketRef );
if ( null == socket ) {
Log.e( TAG, "socket didn't survive into onHandleWork()" );
} else {
nli = (NetLaunchInfo)intent.getSerializableExtra( NLI_KEY );
BluetoothDevice host = socket.getRemoteDevice();
BTCmd response = makeOrNotify( nli, host.getName(), host.getAddress() );
writeBack( socket, response );
closeForRef( socketRef );
}
break;
case RECEIVE_MSG:
socketRef = intent.getIntExtra( SOCKET_REF, -1 );
socket = socketForRef( socketRef );
if ( null != socket ) {
gameID = intent.getIntExtra( GAMEID_KEY, -1 ); gameID = intent.getIntExtra( GAMEID_KEY, -1 );
buf = intent.getByteArrayExtra( MSG_KEY ); if ( -1 != gameID ) {
BluetoothDevice host = socket.getRemoteDevice(); add( new BTQueueElem( BTCmd.MESG_SEND, buf,
CommsAddrRec addr = new CommsAddrRec( host.getName(), btAddr, gameID ) );
host.getAddress() ); }
XWServiceHelper.ReceiveResult rslt break;
= mHelper.receiveMessage( this, gameID, case RADIO:
m_btMsgSink, boolean cameOn = intent.getBooleanExtra( RADIO_KEY, false );
buf, addr ); MultiEvent evt = cameOn? MultiEvent.BT_ENABLED
: MultiEvent.BT_DISABLED;
BTCmd response = rslt == XWServiceHelper.ReceiveResult.GAME_GONE ? mHelper.postEvent( evt );
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT; if ( cameOn ) {
writeBack( socket, response ); GameUtils.resendAllIf( this, CommsConnType.COMMS_CONN_BT );
closeForRef( socketRef ); } else {
} ConnStatusHandler.updateStatus( this, null,
break; CommsConnType.COMMS_CONN_BT,
false );
case POST_GAME_GONE: stopListener();
socketRef = intent.getIntExtra( SOCKET_REF, -1 ); // stopSender();
socket = socketForRef( socketRef ); stopSelf();
if ( null != socket ) { }
break;
case REMOVE:
gameID = intent.getIntExtra( GAMEID_KEY, -1 ); gameID = intent.getIntExtra( GAMEID_KEY, -1 );
mHelper.postEvent( MultiEvent.MESSAGE_NOGAME, gameID ); btAddr = intent.getStringExtra( ADDR_KEY );
writeBack( socket, BTCmd.MESG_ACCPT ); add( new BTQueueElem( BTCmd.MESG_GAMEGONE, btAddr, gameID ) );
closeForRef( socketRef ); break;
}
break; case MAKE_OR_NOTIFY:
int socketRef = intent.getIntExtra( SOCKET_REF, -1 );
BluetoothSocket socket = socketForRef( socketRef );
if ( null == socket ) {
Log.e( TAG, "socket didn't survive into onHandleWork()" );
} else {
nli = (NetLaunchInfo)intent.getSerializableExtra( NLI_KEY );
BluetoothDevice host = socket.getRemoteDevice();
BTCmd response = makeOrNotify( nli, host.getName(), host.getAddress() );
writeBack( socket, response );
closeForRef( socketRef );
}
break;
case RECEIVE_MSG:
socketRef = intent.getIntExtra( SOCKET_REF, -1 );
socket = socketForRef( socketRef );
if ( null != socket ) {
gameID = intent.getIntExtra( GAMEID_KEY, -1 );
buf = intent.getByteArrayExtra( MSG_KEY );
BluetoothDevice host = socket.getRemoteDevice();
CommsAddrRec addr = new CommsAddrRec( host.getName(),
host.getAddress() );
XWServiceHelper.ReceiveResult rslt
= mHelper.receiveMessage( this, gameID,
m_btMsgSink,
buf, addr );
BTCmd response = rslt == XWServiceHelper.ReceiveResult.GAME_GONE ?
BTCmd.MESG_GAMEGONE : BTCmd.MESG_ACCPT;
writeBack( socket, response );
closeForRef( socketRef );
}
break;
case POST_GAME_GONE:
socketRef = intent.getIntExtra( SOCKET_REF, -1 );
socket = socketForRef( socketRef );
if ( null != socket ) {
gameID = intent.getIntExtra( GAMEID_KEY, -1 );
mHelper.postEvent( MultiEvent.MESSAGE_NOGAME, gameID );
writeBack( socket, BTCmd.MESG_ACCPT );
closeForRef( socketRef );
}
break;
default: default:
Assert.fail(); Assert.fail();
}
} }
} // onHandleWorkImpl() } // onHandleWorkImpl()