首页 > 代码库 > NFC技术:使用Android Beam技术传输文本(一)

NFC技术:使用Android Beam技术传输文本(一)

技术分享

技术分享

  1 //实现两部Android手机文本传输
  2 //4.0以上
  3 //方法:将两部安卓手机背对背,一部手机输入文本,点击屏幕发送,另一部接收显示
  4 
  5 public class MainActivity extends Activity implements
  6         CreateNdefMessageCallback, OnNdefPushCompleteCallback {
  7 
  8     private NfcAdapter mnfcAdapter;
  9     private EditText mBeamText;
 10     private PendingIntent mPendingIntent;
 11 
 12     @Override
 13     protected void onCreate(Bundle savedInstanceState) {
 14         super.onCreate(savedInstanceState);
 15         setContentView(R.layout.fragment_main);
 16         mBeamText = (EditText) findViewById(R.id.editText1);
 17         mnfcAdapter = mnfcAdapter.getDefaultAdapter(this);
 18         mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
 19                 getClass()), 0);
 20 
 21         // 指定要传输文本的回调
 22         mnfcAdapter.setNdefPushMessageCallback(this, this);
 23         // 传输完成调用
 24         mnfcAdapter.setOnNdefPushCompleteCallback(this, this);
 25     }
 26 
 27     @Override
 28     protected void onResume() {
 29         // TODO Auto-generated method stub
 30         super.onResume();
 31         if (mnfcAdapter != null) {
 32             mnfcAdapter.enableForegroundDispatch(this, mPendingIntent, null,
 33                     null);
 34         }
 35     }
 36 
 37     @Override
 38     protected void onPause() {
 39         // TODO Auto-generated method stub
 40         super.onPause();
 41         if (mnfcAdapter != null) {
 42             mnfcAdapter.disableForegroundDispatch(this);
 43         }
 44     }
 45 
 46     // 显示接收到的数据
 47     void processIntent(Intent intent) {
 48 
 49         Parcelable[] rawMsgs = intent
 50                 .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
 51         NdefMessage message = (NdefMessage) rawMsgs[0];
 52         String text = TextRecord.parse(message.getRecords()[0]).getText();
 53         Toast.makeText(this, text, 0).show();
 54     }
 55 
 56     @Override
 57     protected void onNewIntent(Intent intent) {
 58         // TODO Auto-generated method stub
 59         // super.onNewIntent(intent);
 60         processIntent(intent);
 61     }
 62 
 63     @Override
 64     public void onNdefPushComplete(NfcEvent event) {
 65         // TODO Auto-generated method stub
 66         Log.i("----->>", "完成");
 67     }
 68 
 69     @Override
 70     public NdefMessage createNdefMessage(NfcEvent event) {
 71         // TODO Auto-generated method stub
 72 
 73         String text = mBeamText.getText().toString().trim();
 74         if ("".equals(text)) {
 75             text = "默认文本";
 76             // 当手机靠近另一部手机,使它自动打开计算器
 77             // NdefMessage ndefMessage = new NdefMessage(
 78             // new NdefRecord[] { NdefRecord
 79             // .createApplicationRecord("com.android.calculator2") });
 80             //
 81 
 82             NdefMessage ndefMessage = new NdefMessage(
 83                     new NdefRecord[] { creatTextRecord(text) });
 84             return ndefMessage;
 85         }
 86 
 87         return null;
 88     }
 89 
 90     public NdefRecord creatTextRecord(String text) {
 91 
 92         byte[] langBytes = Locale.CHINA.getLanguage().getBytes(
 93                 Charset.forName("US-ASCII"));
 94         Charset utfEncoding = Charset.forName("UTF-8");
 95         byte[] textBytes = text.getBytes(utfEncoding);
 96         int utfBit = 0;
 97         char status = (char) (utfBit + langBytes.length);
 98         byte[] data = http://www.mamicode.com/new byte[1 + langBytes.length + textBytes.length];
 99         data[0] = (byte) status;
100         System.arraycopy(langBytes, 0, data, 1, langBytes.length);
101         System.arraycopy(textBytes, 0, data, 1 + langBytes.length,
102                 textBytes.length);
103 
104         NdefRecord ndefRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
105                 NdefRecord.RTD_TEXT, new byte[0], data);
106         return ndefRecord;
107 
108     }
109 
110 }

 

NFC技术:使用Android Beam技术传输文本(一)