NFC的安卓事儿(第八篇)

本文介绍了Android中NFC技术如何实现设备间的信息传递,重点讲解了Android Beam的功能和使用方法,包括setNdefPushMessage()和setNdefPushMessageCallback()的回调函数,以及在实际开发中需要注意的事项和步骤。
摘要由CSDN通过智能技术生成
如何实现Android Beam(设备间的数据分享)

最近很忙,抱歉没能及时更新,上一篇写了AAR技术,这允许我们能通过感应标签跳转到指定应用。其实我们只需要添加在NdefRecord中打入如下记录便可以了:

//以下以打开谷歌系统本身的音乐服务为例,你可以根据个人设备安装的应用情况设定相应应用包名
NdefRecord[] records = { mimeRecord,NdefRecord.createApplicationRecord("com.google.android.music")};

本次公开的Demo中,你需要先利用该Demo往标签打入数据,之后退出该应用再去感应相应标签时就可以自动打开指定的应用程序了。Demo的下载地址:

 http://download.csdn.net/detail/zeruiy/7255733

好了本篇将描述NFC在安卓设备中的另一种用途:实现设备间的信息传递!

安卓的分享功能旨在设备间的信息交互,在传递过程中要求两个设备间都要保持开启解锁状态。当要准备分享数据的设备靠近接收方的话,发送设备会弹出“点击分享”的按钮供用户选择。

在项目中可以通过以下方法来实现数据分享:

setNdefPushMessage() :将一个NdefMessage的数据对象作为分享的信息,当设备间靠近一定程度时将会自动将该数据传递出去。

setNdefPushMessageCallback()当设备间靠近时,你也可以动态生成一个NdefMessage的数据对象进行分享,所以该回调函数将会触发另一个回调的方法: createNdefMessage()。也就是说你可以在 createNdefMessage()中创建一个要准备分享的NdefMessage对象,一旦设备间靠近时便会触发执行了。

一个Activity只能在一次分享中传递一个Ndef的信息,所以当两个方法setNdefPushMessage() setNdefPushMessageCallback()都在项目中存在时你需要注意先后执行次序,通常来说系统会优先调用setNdefPushMessageCallback() 方法,要避免出现相应信息得不到传递的现象。

那么在实现数据分享的过程中需要注意哪些地方呢?

Ø 首先要准备传递数据的Activity必须处于打开状态,而接收方也需要处于解锁的状态。

Ø 需要传递的数据封装在NdefMessage对象中,你还需要对此数据进行压缩的前期处理。

Ø 并不是所有的设备都支持NFC上的数据分享,设备安装的系统一般来说要在Android 4.0以上为好。

Ø 注意,如果你的Activity支持数据分享(Android Beam)并且处于打开状态,那么标准的intent调度系统将会失效。不过如果你的Activity同时支持前台调度,那么在Activity打开状态时同样是可以扫描标签进行过滤功能的。

好吧,进行了一些Android Beam的介绍后我们应该对Android Beam的数据分享有了一定了解了,那么接下来理所当然的是如何进行实际开发了。一般来说我们需要完成以下步骤:

Ø创建一个你需要传递的的数据,当然是用NdefMessage的对象来封装了。

Ø对相应的回调函数进行声明,也就是上面提到的setNdefPushMessage() setNdefPushMessageCallback()这两个回调方法了(可以选择其中一个就行)。这需要在onCreate()中进行。setNdefPushMessage()的回调方法一般是来发送一些固定的信息,方便多次重复的传递操作,而setNdefPushMessageCallback()我们知道,可以动态生成相应数据进行传递,所以比较适合在不同环境下的传递操作(如果要传递的数据需要根据不同状况变化时)。

以下是官方文档的一个例子,实现了在一个Activity中的Android Beam操作:

package com.example.android.beam;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;
import java.nio.charset.Charset;


public class Beam extends Activity implements CreateNdefMessageCallback {
    NfcAdapter mNfcAdapter;
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView) findViewById(R.id.textView);
        // Check for available NFC Adapter
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if (mNfcAdapter == null) {
            Toast.makeText(this, "NFC is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        // Register callback
        mNfcAdapter.setNdefPushMessageCallback(this, this);
    }

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String text = ("Beam me up, Android!\n\n" +
                "Beam Time: " + System.currentTimeMillis());
        NdefMessage msg = new NdefMessage(
                new NdefRecord[] { createMime(
                        "application/vnd.com.example.android.beam", text.getBytes())
         /**
          * The Android Application Record (AAR) is commented out. When a device
          * receives a push with an AAR in it, the application specified in the AAR
          * is guaranteed to run. The AAR overrides the tag dispatch system.
          * You can add it back in to guarantee that this
          * activity starts when receiving a beamed message. For now, this code
          * uses the tag dispatch system.
          */
          //,NdefRecord.createApplicationRecord("com.example.android.beam")
        });
        return msg;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        textView = (TextView) findViewById(R.id.textView);
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        textView.setText(new String(msg.getRecords()[0].getPayload()));
    }
}

下一篇中我们将会进入NFC高级开发阶段,因此,我们将不局限于NDEF格式的数据读写!我们将了解不同标签的差别,从而可以实现更加自由的数据交互,请继续关注啦。

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值