diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 8d6095ec9..c84f9ad2e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -574,6 +574,8 @@ public class BoardActivity extends XWActivity m_haveInvited = intent.getBooleanExtra( GameUtils.INVITED, false ); m_overNotShown = true; + NFCUtils.register( this ); // Don't seem to need to unregister... + setBackgroundColor(); setKeepScreenOn(); } // onCreate diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java index c50290151..55bdf16a6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java @@ -26,6 +26,7 @@ import android.content.Intent; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; +import android.nfc.NfcEvent; import android.nfc.NfcManager; import android.os.Parcelable; import java.nio.charset.Charset; @@ -33,6 +34,7 @@ import java.nio.charset.Charset; public class NFCUtils { private static boolean s_inSDK = 14 <= Integer.valueOf( android.os.Build.VERSION.SDK ); + private static String s_data = null; public static boolean nfcAvail( Context context ) { @@ -46,19 +48,31 @@ public class NFCUtils { return result; } + public static void register( final Activity activity ) + { + NfcAdapter.CreateNdefMessageCallback cb = + new NfcAdapter.CreateNdefMessageCallback() { + public NdefMessage createNdefMessage( NfcEvent event ) { + NdefMessage msg = null; + if ( null != s_data ) { + msg = makeMessage( activity, s_data ); + } + return msg; + } + }; + + NfcManager manager = + (NfcManager)activity.getSystemService( Context.NFC_SERVICE ); + NfcAdapter adapter = manager.getDefaultAdapter(); + adapter.setNdefPushMessageCallback( cb, activity ); + } + public static void buildAndPush( Activity activity, String data ) { - NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity ); - String mimeType = activity.getString( R.string.xwords_nfc_mime ); - NdefMessage msg = new NdefMessage( new NdefRecord[] { - new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeType - .getBytes(Charset.forName("US-ASCII")), - new byte[0], - data.getBytes(Charset.forName("US-ASCII"))) - ,NdefRecord. - createApplicationRecord( activity.getPackageName() ) - }); - nfcAdapter.setNdefPushMessage( msg, activity ); + s_data = data; + // NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity ); + // NdefMessage msg = makeMessage( activity, data ); + // nfcAdapter.setNdefPushMessage( msg, activity ); } public static String getFromIntent( Intent intent ) @@ -76,4 +90,18 @@ public class NFCUtils { return result; } + + private static NdefMessage makeMessage( Activity activity, String data ) + { + String mimeType = activity.getString( R.string.xwords_nfc_mime ); + NdefMessage msg = new NdefMessage( new NdefRecord[] { + new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeType + .getBytes(Charset.forName("US-ASCII")), + new byte[0], + data.getBytes(Charset.forName("US-ASCII"))) + ,NdefRecord. + createApplicationRecord( activity.getPackageName() ) + }); + return msg; + } }