enhance receiver to get BT on/off and connect attempts and to start

bt service in response.
This commit is contained in:
Eric House 2012-02-03 17:29:40 -08:00
parent 6982545f59
commit cc9f2b793e
2 changed files with 36 additions and 0 deletions

View file

@ -23,6 +23,8 @@ package org.eehouse.android.xw4;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
public class BTReceiver extends BroadcastReceiver {
@ -30,5 +32,24 @@ public class BTReceiver extends BroadcastReceiver {
public void onReceive( Context context, Intent intent )
{
DbgUtils.logf( "BTReceiver.onReceive()" );
String action = intent.getAction();
DbgUtils.logf( "BTReceiver.onReceive(action=%s)", action );
if ( action.equals( BluetoothDevice.ACTION_ACL_CONNECTED ) ) {
BTService.startService( context );
} else if ( action.equals( BluetoothAdapter.ACTION_STATE_CHANGED ) ) {
int newState =
intent.getIntExtra( BluetoothAdapter.EXTRA_STATE, -1 );
switch ( newState ) {
case BluetoothAdapter.STATE_OFF:
BTService.radioChanged( context, false );
break;
case BluetoothAdapter.STATE_ON:
BTService.radioChanged( context, true );
break;
case BluetoothAdapter.STATE_TURNING_ON:
case BluetoothAdapter.STATE_TURNING_OFF:
break;
}
}
}
}

View file

@ -64,11 +64,13 @@ public class BTService extends Service {
private static final int SCAN = 1;
private static final int INVITE = 2;
private static final int SEND = 3;
private static final int RADIO = 4;
private static final String CMD_STR = "CMD";
private static final String MSG_STR = "MSG";
private static final String TARGET_STR = "TRG";
private static final String ADDR_STR = "ADR";
private static final String RADIO_STR = "RDO";
private static final String GAMEID_STR = "GMI";
@ -140,6 +142,14 @@ public class BTService extends Service {
}
}
public static void radioChanged( Context context, boolean cameOn )
{
Intent intent = new Intent( context, BTService.class );
intent.putExtra( CMD_STR, RADIO );
intent.putExtra( RADIO_STR, cameOn );
context.startService( intent );
}
public static void rescan( Context context ){
Intent intent = new Intent( context, BTService.class );
intent.putExtra( CMD_STR, SCAN );
@ -237,6 +247,11 @@ public class BTService extends Service {
addr, gameID ) );
}
break;
case RADIO:
boolean cameOn = intent.getBooleanExtra( RADIO_STR, false );
BTEvent evt = cameOn? BTEvent.BT_ENABLED : BTEvent.BT_DISABLED;
sendResult( evt );
break;
default:
Assert.fail();
}