From cc9f2b793e7dcde3d9eb00f3c68e1f19eac482f5 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 3 Feb 2012 17:29:40 -0800 Subject: [PATCH] enhance receiver to get BT on/off and connect attempts and to start bt service in response. --- .../org/eehouse/android/xw4/BTReceiver.java | 21 +++++++++++++++++++ .../org/eehouse/android/xw4/BTService.java | 15 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTReceiver.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTReceiver.java index f47642506..a42001529 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTReceiver.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTReceiver.java @@ -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; + } + } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java index 1077683ce..41c64016f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTService.java @@ -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(); }