前言
最近没怎么写东西了,主要是在了解Beam这个东东。找到一些高手写的文章,奈何水平有限看的云里雾里的。没办法,只好去复习官方文档。
正文:
先摘取一部分官方文档:
Beaming NDEF Messages to Other Devices
Android Beam allows simple peer-to-peer data exchange between two Android-powered devices. The application that wants to beam data to another device must be in the foreground and the device receiving the data must not be locked. When the beaming device comes
in close enough contact with a receiving device, the beaming device displays the "Touch to Beam" UI. The user can then choose whether or not to beam the message to the receiving device.Note: Foreground NDEF pushing was available at API level 10, which provides similar functionality to Android Beam. These APIs have since been deprecated, but are available to support older devices. See
enableForegroundNdefPush()
for
more information.You can enable Android Beam for your application by calling one of the two methods:
setNdefPushMessage()
:
Accepts anNdefMessage
to
set as the message to beam. Automatically beams the message when two devices are in close enough proximity.setNdefPushMessageCallback()
:
Accepts a callback that contains acreateNdefMessage()
which
is called when a device is in range to beam data to. The callback lets you create the NDEF message only when necessary.An activity can only push one NDEF message at a time, so
setNdefPushMessageCallback()
takes
precedence oversetNdefPushMessage()
if
both are set. To use Android Beam, the following general guidelines must be met:
- The activity that is beaming the data must be in the foreground. Both devices must have their screens unlocked.
- You must encapsulate the data that you are beaming in an
NdefMessage
object.- The NFC device that is receiving the beamed data must support the
com.android.npp
NDEF push protocol or NFC Forum's SNEP (Simple NDEF Exchange Protocol).
Thecom.android.npp
protocol is required for devices on API level 9 (Android 2.3) to API level 13 (Android 3.2).com.android.npp
and
SNEP are both required on API level 14 (Android 4.0) and later.Note: If your activity enables Android Beam and is in the foreground, the standard intent dispatch system is disabled. However, if your activity also enables foreground
dispatching, then it can still scan tags that match the intent filters set in the foreground dispatching.To enable Android Beam:
- Create an
NdefMessage
that
contains theNdefRecord
s
that you want to push onto the other device.- Call
setNdefPushMessage()
with
aNdefMessage
or
callsetNdefPushMessageCallback
passing
in aNfcAdapter.CreateNdefMessageCallback
object
in theonCreate()
method of your activity. These methods require at least one activity that you want to enable with Android Beam, along with an optional list of other activities to activate.In general, you normally use
setNdefPushMessage()
if
your Activity only needs to push the same NDEF message at all times, when two devices are in range to communicate. You usesetNdefPushMessageCallback
when
your application cares about the current context of the application and wants to push an NDEF message depending on what the user is doing in your application.
主要是理解上面这部分东西,多看即便会有不一样的感觉。
下面是我按照官方的例子调试出来的一篇Beam代码,功能和官方的一样。我还在完善,先贴出来,以后更新。
package com.example.beamtest; /** * Time: 2012.8.11 * Author: kehr * Aim: Test Android beam */ import java.nio.charset.Charset; 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.view.Window; import android.view.WindowManager; import android.widget.EditText; import android.widget.Toast; public class Beam extends Activity implements CreateNdefMessageCallback { NfcAdapter mNfcAdapter; // TextView mEditText; EditText mEditText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // fullscreen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // no title requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.beam_layout); mEditText = (EditText) findViewById(R.id.beam_input_EditText); // 检测设备是否支持NFC mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { Toast.makeText(this, "你的设备不支持", Toast.LENGTH_LONG).show(); // 检测NFC是否开启 } else if (mNfcAdapter.isEnabled()) { Toast.makeText(this, "NFC开启!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "NFC未开启!请手动设置~", Toast.LENGTH_LONG).show(); } // 注册回调函数 mNfcAdapter.setNdefPushMessageCallback(this, this); } // 发送消息------------------------------------------------------------------------- // 这是CreateNdefMessageCallback中需要实现的方法 @Override public NdefMessage createNdefMessage(NfcEvent event) { // TODO Auto-generated method stub // 获取文本框中的内容 String text = mEditText.getText().toString(); NdefMessage msg = new NdefMessage(new NdefRecord[] { createMimeRecord( "application/com.example.android.beam", text.getBytes()) }); return msg; } public NdefRecord createMimeRecord(String mimeType, byte[] payload) { byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII")); NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload); return mimeRecord; } // 消息发送完成的处理------------------------------------------------ //------------不是主要功能,官方例子里面有,我给去掉了----------------待实现…… // 处理接收的消息---------------------------------------------------------------------- // 第一步,接收Intent @Override protected void onNewIntent(Intent intent) { // super.onNewIntent(intent); setIntent(intent); } // 第二步,判断Intent @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) { processIntent(getIntent()); } } // 第三步。处理Intent void processIntent(Intent intent) { 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 mEditText.setText(new String(msg.getRecords()[0].getPayload())); } }
上面这是Beam实现的主代码,运行起来还需要一些其它的配置。
其实初步理解后发现,beam也没有想像中来的麻烦。
如果需要完整的工程文件,请留言联系我。