android beam wifi,Android 无线发送

Android 无线发送Android Beam

06/06/2017

本文内容

Android Beam 是 Android 4.0 中引入的近场通信 (NFC) 技术,允许应用程序在紧密邻近时通过 NFC 共享信息。Android Beam is a Near Field Communication (NFC) technology introduced in Android 4.0 that allows applications to share information over NFC when in close proximity.

8c29868f400802984776e207a87448df.png8c29868f400802984776e207a87448df.png

Android Beam 的工作原理是,当两台设备在范围内时,通过 NFC 推送消息。Android Beam works by pushing messages over NFC when two devices are in range. 彼此相距约 4cm 的两台设备可以使用 Android Beam 共享数据。Devices about 4cm from each other can share data using Android Beam. 一台设备上的活动创建消息,并指定一个或多个可以处理消息推送的活动。An Activity on one device creates a message and specifies an Activity (or Activities) that can handle pushing it. 如果指定的活动位于前台,且两台设备处于范围内,Android Beam 就会将消息推送到第二台设备。When the specified Activity is in the foreground and the devices are in range, Android Beam will push the message to the second device. 接收设备上会调用包含消息数据的意向。On the receiving device, an Intent is invoked containing the message data.

Android 支持通过两种方式来设置使用 Android Beam 推送消息:Android supports two ways of setting messages with Android Beam:

SetNdefPushMessage - 在 Android Beam 启动前,应用程序可以调用 SetNdefPushMessage,以指定要通过 NFC 推送的 NdefMessage,以及推送消息的活动。SetNdefPushMessage - Before Android Beam is initiated, an application can call SetNdefPushMessage to specify an NdefMessage to push over NFC, and the Activity that is pushing it. 此机制最适用于在应用程序使用时消息未更改的情况。This mechanism is best used when a message doesn’t change while an application is in use.

SetNdefPushMessageCallback - 在 Android Beam 启动后,应用程序可以处理回拨来创建 NdefMessage。SetNdefPushMessageCallback - When Android Beam is initiated, an application can handle a callback to create an NdefMessage. 此机制允许延迟到两台设备处于范围内时创建消息。This mechanism allows for message creation to be delayed until devices are in range. 它支持消息可能根据应用程序中的操作而变化的方案。It supports scenarios where the message may vary based upon what’s happening in the application.

无论是哪种方式,为了使用 Android Beam 发送数据,应用程序会发送 NdefMessage,同时将数据打包到多个 NdefRecords 中。In either case, to send data with Android Beam, an application sends an NdefMessage, packaging the data in several NdefRecords. 接下来看看在能够触发 Android Beam 前必须处理的要点。Let’s take a look at the key points that must be addressed before we can trigger Android Beam. 首先,需要使用创建 NdefMessage 的回拨样式。First, we’ll work with the callback style of creating an NdefMessage.

创建消息Creating a Message

可以在活动的 OnCreate 方法中使用 NfcAdapter 注册回拨。We can register callbacks with an NfcAdapter in the Activity’s OnCreate method. 例如,假设名为 mNfcAdapter 的 NfcAdapter 在活动中声明为类变量,可以编写下面的代码,以创建用于构造消息的回拨:For example, assuming an NfcAdapter named mNfcAdapter is declared as a class variable in the Activity, we can write the following code to create the callback that will construct the message:

mNfcAdapter = NfcAdapter.GetDefaultAdapter (this);

mNfcAdapter.SetNdefPushMessageCallback (this, this);

用于实现 NfcAdapter.ICreateNdefMessageCallback 的活动传递到上面的 SetNdefPushMessageCallback 方法。The Activity, which implements NfcAdapter.ICreateNdefMessageCallback, is passed to the SetNdefPushMessageCallback method above. 在 Android Beam 启动时,系统会调用 CreateNdefMessage,活动可以从中构造 NdefMessage,如下所示:When Android Beam is initiated, the system will call CreateNdefMessage, from which the Activity can construct an NdefMessage as shown below:

public NdefMessage CreateNdefMessage (NfcEvent evt)

{

DateTime time = DateTime.Now;

var text = ("Beam me up!\n\n" + "Beam Time: " +

time.ToString ("HH:mm:ss"));

NdefMessage msg = new NdefMessage (

new NdefRecord[]{ CreateMimeRecord (

"application/com.example.android.beam",

Encoding.UTF8.GetBytes (text)) });

} };

return msg;

}

public NdefRecord CreateMimeRecord (String mimeType, byte [] payload)

{

byte [] mimeBytes = Encoding.UTF8.GetBytes (mimeType);

NdefRecord mimeRecord = new NdefRecord (

NdefRecord.TnfMimeMedia, mimeBytes, new byte [0], payload);

return mimeRecord;

}

接收消息Receiving a Message

在接收端,系统使用 ActionNdefDiscovered 操作调用意向,可以从中提取 NdefMessage,如下所示:On the receiving side, the system invokes an Intent with the ActionNdefDiscovered action, from which we can extract the NdefMessage as follows:

IParcelable [] rawMsgs = intent.GetParcelableArrayExtra (NfcAdapter.ExtraNdefMessages);

NdefMessage msg = (NdefMessage) rawMsgs [0];

有关使用 Android Beam 的完整代码示例(如下面的屏幕截图所示),请参阅示例库中的 Android Beam 演示。For a complete code example that uses Android Beam, shown running in the screenshot below, see the Android Beam demo in the Sample Gallery.

5a2be0e3d568317de65352fd6f11b89e.png5a2be0e3d568317de65352fd6f11b89e.png

相关链接Related Links

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值