From 0337b97b5737aa4f0d9494faa53b16ffbf9cd2df Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 18 Jan 2012 18:30:53 -0800 Subject: [PATCH] begin bluetooth work. Add new class that logs when BT comes on and goes off. Sketch out always-on listening socket, but it's not called yet. --- xwords4/android/XWords4/AndroidManifest.xml | 9 ++ .../org/eehouse/android/xw4/BTConnection.java | 112 ++++++++++++++++++ .../src/org/eehouse/android/xw4/XWApp.java | 10 ++ 3 files changed, 131 insertions(+) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/BTConnection.java diff --git a/xwords4/android/XWords4/AndroidManifest.xml b/xwords4/android/XWords4/AndroidManifest.xml index 842b9203a..01042c70c 100644 --- a/xwords4/android/XWords4/AndroidManifest.xml +++ b/xwords4/android/XWords4/AndroidManifest.xml @@ -31,6 +31,9 @@ + + + @@ -138,6 +141,12 @@ + + + + + + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTConnection.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTConnection.java new file mode 100644 index 000000000..bc3d3115f --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTConnection.java @@ -0,0 +1,112 @@ +/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */ +/* + * Copyright 2012 by Eric House (xwords@eehouse.org). All rights + * reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothServerSocket; +import android.bluetooth.BluetoothSocket; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import java.io.InputStream; + +public class BTConnection extends BroadcastReceiver { + private static BluetoothAdapter s_btAdapter = BluetoothAdapter.getDefaultAdapter(); + private static BluetoothServerSocket s_serverSocket; + + private class BTListener extends Thread { + @Override + public void run() { + byte[] buffer = new byte[1024]; + for ( ; ; ) { + try { + s_serverSocket = s_btAdapter. + listenUsingRfcommWithServiceRecord( XWApp.getAppName(), + XWApp.getAppUUID() ); + } catch ( java.io.IOException ioe ) { + DbgUtils.logf( "listenUsingRfcommWithServiceRecord=>%s", ioe.toString() ); + break; + } + + BluetoothSocket socket = null; + InputStream inStream = null; + try { + socket = s_serverSocket.accept(); // blocks + inStream = socket.getInputStream(); + int nRead = inStream.read( buffer ); + DbgUtils.logf( "read %d bytes from BT socket", nRead ); + socket.close(); + } catch ( java.io.IOException ioe ) { + DbgUtils.logf( "accept=>%s", ioe.toString() ); + break; + } + + // now handle what's on the socket + + } // for ( ; ; ) + + if ( null != s_serverSocket ) { + try { + s_serverSocket.close(); + } catch ( java.io.IOException ioe ) { + DbgUtils.logf( "close()=>%s", ioe.toString() ); + } + } + } + } + + private BTListener m_listener = null; + + @Override + public void onReceive( Context context, Intent intent ) { + int newState = intent.getIntExtra( BluetoothAdapter.EXTRA_STATE, -1 ); + String asString = null; + switch ( newState ) { + case -1: + break; + case BluetoothAdapter.STATE_OFF: + asString = "STATE_OFF"; + break; + case BluetoothAdapter.STATE_TURNING_ON: + asString = "STATE_TURNING_ON"; + break; + case BluetoothAdapter.STATE_ON: + asString = "STATE_ON"; + m_listener = new BTListener(); + m_listener.start(); + break; + case BluetoothAdapter.STATE_TURNING_OFF: + asString = "STATE_TURNING_OFF"; + if ( null != m_listener ) { + m_listener.stop(); + m_listener = null; + } + break; + } + + if ( null != asString ) { + DbgUtils.logf( "onReceive: new BT state = %s", asString ); + } + + } // onReceive + +} diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java index feee91e61..940a10576 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java @@ -21,6 +21,7 @@ package org.eehouse.android.xw4; import android.app.Application; +import java.util.UUID; public class XWApp extends Application { @@ -38,4 +39,13 @@ public class XWApp extends Application { RelayReceiver.RestartTimer( this ); } + + public static UUID getAppUUID() { + return UUID.fromString( "d0837107-421f-11e1-b86c-0800200c9a66" ); + } + + public static String getAppName() { + return "Crosswords"; + } + }