Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.example.android.apis.nfc;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.app.Activity;
     22 import android.nfc.NdefMessage;
     23 import android.nfc.NdefRecord;
     24 import android.nfc.NfcAdapter;
     25 import android.os.Bundle;
     26 import android.widget.TextView;
     27 
     28 import java.nio.charset.Charset;
     29 import java.util.Locale;
     30 
     31 /**
     32  * An example of how to use the NFC foreground NDEF push APIs.
     33  */
     34 public class ForegroundNdefPush extends Activity {
     35     private NfcAdapter mAdapter;
     36     private TextView mText;
     37     private NdefMessage mMessage;
     38 
     39     public static NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) {
     40         byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
     41 
     42         Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
     43         byte[] textBytes = text.getBytes(utfEncoding);
     44 
     45         int utfBit = encodeInUtf8 ? 0 : (1 << 7);
     46         char status = (char) (utfBit + langBytes.length);
     47 
     48         byte[] data = new byte[1 + langBytes.length + textBytes.length];
     49         data[0] = (byte) status;
     50         System.arraycopy(langBytes, 0, data, 1, langBytes.length);
     51         System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
     52 
     53         return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
     54     }
     55 
     56     @Override
     57     public void onCreate(Bundle savedState) {
     58         super.onCreate(savedState);
     59 
     60         mAdapter = NfcAdapter.getDefaultAdapter(this);
     61 
     62         setContentView(R.layout.foreground_dispatch);
     63         mText = (TextView) findViewById(R.id.text);
     64         if (mAdapter != null) {
     65             mText.setText("Tap another Android phone with NFC to push 'NDEF Push Sample'");
     66         } else {
     67             mText.setText("This phone is not NFC enabled.");
     68         }
     69 
     70         // Create an NDEF message with some sample text
     71         mMessage = new NdefMessage(
     72                 new NdefRecord[] { newTextRecord("NDEF Push Sample", Locale.ENGLISH, true)});
     73     }
     74 
     75     @Override
     76     public void onResume() {
     77         super.onResume();
     78         if (mAdapter != null) mAdapter.enableForegroundNdefPush(this, mMessage);
     79     }
     80 
     81     @Override
     82     public void onPause() {
     83         super.onPause();
     84         if (mAdapter != null) mAdapter.disableForegroundNdefPush(this);
     85     }
     86 }
     87