前言

最近没怎么写东西了,主要是在了解Beam这个东东。这方面的教程真的非常有限,找了不少资料目前还没看到一篇能够让一个新手看一遍就知道实现一个Beam功能都需要那些步骤的。而且,都是用的官方的例子,稍加注释。偶尔找到一些高手写的文章,奈何水平有限看的云里雾里的。没办法,只好去复习官方文档。

都说现在Android方面的书没有什么很好的。我的体会是,书讲的都很全面,力求把每个方面都介绍一遍。这样带来的缺点是知识太浅,不够灵活。不过,想想书名也便释然了——“××Android 入门基础×× ”再加一些修饰词。唉~,入门~

正文:

好了进入正题。看官方文档有些吃力,主要都是英文的,概念方面还有不足。但是坚持看下来还是有很大的收获的。

先摘取一部分官方文档:

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. SeeenableForegroundNdefPush() for more information.

You can enable Android Beam for your application by calling one of the two methods:

An activity can only push one NDEF message at a time, so setNdefPushMessageCallback() takes precedence over setNdefPushMessage() 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). The com.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:

  1. Create an NdefMessage that contains the NdefRecords that you want to push onto the other device.
  2. Call setNdefPushMessage() with a NdefMessage or call setNdefPushMessageCallback passing in aNfcAdapter.CreateNdefMessageCallback object in the onCreate() 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也没有想像中来的麻烦。

如果需要完整的工程文件,请留言联系我。

Android Beam 详细实现步骤的更多相关文章

  1. Android slidingmenu详细解释 滑动的优化

    Android slidingmenu 详细解释 性能优化 转载请注明:   http://blog.csdn.net/aaawqqq 简单介绍 SlidingMenu 是github 上Androi ...

  2. vs2010如何安装mvc3,怎样安装,详细的步骤,从哪下载?请看这篇文章。

    vs2010如何安装mvc3,怎样安装,详细的步骤,从哪下载?请看这篇文章. 安装步骤:vs2010 -> vs2010sp1 -> AspNetMVC3Setup -> AspNe ...

  3. yii2 rbac权限控制详细操作步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_rbac_description本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 ...

  4. yii2权限控制rbac之详细操作步骤

    本篇的主题是 rbac权限控制的详细操作步骤,注意是操作步骤哦,关于配置与rbac的搭建,我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程说的再清楚不过了. 但是,在很多人的反馈下,说是 ...

  5. APNs详细使用步骤

    1. 什么是推送通知 消息通知分本地通知和远程推送通知,是没有运行在前台的应用程序可以让它们的用户获得相关消息通知的方式.消息通知可能是一条消息,即将发生的日历事件,或远程服务器的新数据.当被操作系统 ...

  6. Unity中加入Android项目的Build步骤

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简介: 有的项目需要在Android中加入Unity功能,例如ANDROID应用中嵌入Un ...

  7. 14、NFC技术:使用Android Beam技术传输文本

    Android Beam的基本理念 Android Beam的基本理念就是两部(只能是两部)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部NFC设备.在传递数据的过 ...

  8. NFC(13)使用Android Beam技术传输文件

    注意 Android Beam技术传输文件时nfc只负责连接两个手机,而传输文件实际是用蓝牙模块.且目前接收文件功能只是系统完成,不用自写个接收程序. 传输文件相关的重要api 从Android4.1 ...

  9. NFC(12)使用Android Beam技术传输文本数据及它是什么

    Android Beam技术是什么 Android Beam的基本理念就是两部(只能是1对1,不可像蓝牙那样1对多)NFC设备靠近时(一般是背靠背),通过触摸一部NFC设备的屏幕,将数据推向另外一部N ...

随机推荐

  1. 《C和指针》章节后编程练习解答参考——6.3

    <C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...

  2. PPT2010小技巧 教你如何快捷抠图

    相信不少人在做PPT时,都有想插入个漂亮个性图案的想法,但是往往手头上的图片都不太令人满意,需要“裁剪”一下才能达不到自己想要的效果.这时大部分人可能会PS,但是相比起今天要分享给大家的方法,步骤就显 ...

  3. 在开发项目中有些常用的的实用代码(ps:平时看着无关紧要的,却很容易忘记)

    1,在客户端使用Cookie document.cookie = "key=1"; document.cookie = "name=zhangsan"; coo ...

  4. Ajax、Comet、HTML 5 Web Sockets技术比较分析

    最近因为考虑研究B/S结构网站即时消息处理 参考了 JAVA怎么样实现即时消息提醒http://bbs.csdn.net/topics/330015611http://www.ibm.com/deve ...

  5. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...

  6. 第 5 章 工厂方法模式【Factory Method Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> 女娲补天的故事大家都听说过吧,今天不说这个,说女娲创造人的故事,可不是“造人”的工作,这个词被现代人滥用了.这个故事是说 ...

  7. rabbitmq 测试

    --*-- import pika import datetime connection = pika.BlockingConnection(pika.ConnectionParameters( 'l ...

  8. Discuz <= 7.2 SQL注入漏洞详情

    在<高级PHP应用程序漏洞审核技术>[1]一文里的"魔术引号带来的新的安全问题"一节里,有 提到通过提取魔术引号产生的“\”字符带来的安全问题,同样这个问题在这里又一次 ...

  9. 怎么清除SVN密码,以及重置eclipse中svn插件密码

    如何清除SVN密码,以及重置eclipse中svn插件密码? 清除SVN客户端密码方法: 邮件选择TortoiseSVN中的settings选项---Saved Data---右边会发现有个Authe ...

  10. Unity给力插件之LittleLocalization

    从网上随便下载了一个多语言的插件,原理很简单,自己就可以写一个.不过本身插件做的算比较全面,拿来就能用,所以精简了一下,然后重新打了个包. 为了快速使用插件,在此把步骤列出来. 1.创建空物体,添加L ...