安卓NFC开发学习笔记_第1页
安卓NFC开发学习笔记_第2页
安卓NFC开发学习笔记_第3页
安卓NFC开发学习笔记_第4页
安卓NFC开发学习笔记_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、大家学习android开发建议首选android开发文档,该文档在你下载的sdk中,路径:/sdk/docs/index.html目前NFC应用的大的框架上的理解:我使用的API LEVEL是19,支持的API有三个:android.nfc,android.nfc.cardemulator,android.nfc.techNFC在手机上的应用大体分为两类:读卡器和卡android.nfc.cardemulator接口是为NFC作为卡应用提供的接口,在较低版本的API上是没有的android.nfc.tech,android.nfc接口是为NFC作为读卡器应用提供的接口首先说作为卡,nfc有两种

2、实现方式,一个是使用NFC芯片作为卡,另一个是使用SIM作为卡Figure 1. NFC card emulation with a secure element.至于从读卡器发送的指令到底是传递到NFC芯片还是SIM由NFC Controler控制,图中Secure Element是指SIM,Host-CPU指NFC芯片android提供HostApduService用于NFC芯片,OffHostApduService用于SIM芯片,传递方向在res/xml文件中通过AID来控制ps:Host-Based Card Emulator 简称为HCE代码实现:AndroidManifest.xm

3、l 中 配置service,因为作为卡实现的话,NFC功能是作为service存在的 res/xml/apduservice.xml 中配置service响应的AID 配置文件完成后编写service的处理方法:NFCService需要继承HostApduService,如果需要与Activity通信,建议采用广播方式也可以自己实现观察者模式,只是这样就需要持有Activity的引用,感觉不太好NFCService.javapublic class NFCService extends HostApduService private Intent intent = new Intent(mun

4、ication.RECEIVER); Override public void onCreate() /启动Acivity Intent i = new Intent(); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);/需要启动的Activity不是当前Activity的时候需要用FLAG_ACTIVITY_NEW_TASK i.setAction(com.apdu.nfc); getApplication().startActivity(i); Toast.makeText(getApplicationContext(), Service启动, Toa

5、st.LENGTH_LONG).show(); Override public byte processCommandApdu(byte commandApdu, Bundle extras) /当注册的AID被选中后,后续指令被分发到这个处理函数中 byte sw = new byte(byte)0x90,(byte)0x00; byte response = new byte5; if (commandApdu0=(byte)0x00 &commandApdu1=(byte)0xA4& commandApdu2=(byte)0x04 & commandApdu4=(byte)0x07& c

6、ommandApdu5=(byte)0xF0) return sw; else /apdu处理逻辑 switch(commandApdu1) case (byte)0xA8: break; case (byte)0xAE: break; default: return sw; intent.putExtra(command, commandApdu); intent.putExtra(response, response); sendBroadcast(intent); /利用广播与Activity通信 return response; /SW值需要包含在response中 Override

7、public void onDeactivated(int reason) if (reason=HostApduService.DEACTIVATION_DESELECTED) Toast.makeText(getApplicationContext(), 已选择其它应用, Toast.LENGTH_LONG).show(); else Toast.makeText(getApplicationContext(), 连接断开, Toast.LENGTH_LONG).show(); Override public void onDestroy() Toast.makeText(getAppli

8、cationContext(), Service关闭, Toast.LENGTH_LONG).show(); super.onDestroy(); 框架搭建好剩余的事情就很简单了,apdu的处理逻辑在processCommandApdu方法中实现即可以上是Host-CPU方式的实现,SIM方式,API介绍中说该方式没有提供可供操作的API,也就是说Android不会监听SIM卡与读卡器之间的通信所以NFCOffService 只需要实现onBind接口,这样绑定该Service的Activity可以对NFCOffService进行有限操作public class NFCOffService e

9、xtends OffHostApduService Override public IBinder onBind(Intent intent) / TODO Auto-generated method stub return null; 上面没有提到的就是,如果你需要使用NFC,需要在Manifest中申请NFC权限: 现在来说说NFC芯片作为读卡器的应用场景以及实现android.nfc.tech,android.nfc接口是为NFC作为读卡器应用提供的接口接口定义了三种Action Tags:ACTION_NDEF_DISCOVERED,ACTION_TECH_DISCOVERED,ACT

10、ION_TAG_DISCOVERED。当你在Manifest文件中将Activity的action-filter设置为这三个Tag中的一种或几种时,NFC响应事件会按照如图流程处理我的理解是ACTION_NDEF_DISCOVERED 是用于两台NFC手机之间传输文件的ACTION_TECH_DISCOVERED,ACTION_TAG_DISCOVERED才是用于NFC与卡进行通讯的所以开发第一步是在Manifest中配置你的Action: TECH_DISCOVERED还需要配置meta-data,meta-data的作用相当于补充说明或者一些配置信息nfc_tech_filter.xml

11、android.nfc.tech.IsoDep android.nfc.tech.NfcA android.nfc.tech.NfcB android.nfc.tech.NfcF android.nfc.tech.NfcV android.nfc.tech.Ndef android.nfc.tech.NdefFormatable android.nfc.tech.MifareClassic android.nfc.tech.MifareUltralight 当然API中说明你可以将多个tech写在一个tech-list中,我做了尝试,这样做会引出一个问题,在程序未启动的情况下当手机刷卡时不会自

12、动打开程序如果想要自动打开需要按照上面这种写法,tech的个数可以根据你想要支持的卡类型进行调整配置完成后,可以开始编写自己的Activity的java代码了在onCreate方法中,需要获取NfcAdapter的引用,从名字可以看出这是一个适配器 NfcAdapter nfcAdapter; PendingIntent pendingIntent; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.pos

13、_main); dc = (Button)findViewById(R.id.button4DC); ecc = (Button)findViewById(R.id.button4ECC); qpboc = (Button)findViewById(R.id.button4QPBOC); logWindow=(TextView)findViewById(Rmunication4Financy); nfcAdapter=NfcAdapter.getDefaultAdapter(this); pendingIntent = PendingIntent.getActivity( this, 0, n

14、ew Intent(this, getClass().addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); onNewIntent(getIntent(); 这里使用PendingIntent,该Intent与普通的Intent不同的是它是有一个延迟启动的功能,它启动时会回调onNewIntent函数,这样能够实现NFC与Activity的交互pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass().addFlags(Intent.FLAG_ACTIVIT

15、Y_SINGLE_TOP), 0);的含义是将Intent传递给this Activity在onPause与onResume中需要添加代码 public void onPause() super.onPause(); nfcAdapter.disableForegroundDispatch(this); public void onResume() super.onResume(); nfcAdapter.enableForegroundDispatch(this, pendingIntent, FILTERS, TECHLISTS); enableForegroundDispatch的作用是

16、,当NFC事件发生时如果当前Activity不是注册了NFC action-filter的Activity,手机会显示注册了NFC事件的Activity供用户选择如果当前Activity注册了NFC action 则将事件优先交由当前Activity处理。onNewIntent实现: Override public void onNewIntent(Intent intent) Parcelable p = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); if (p=null) return; Tag nfcTag = (Tag)p; fina

17、l IsoDep isodep = IsoDep.get(nfcTag);/ final NfcA isodep = NfcA.get(nfcTag); final byte cmd = (byte) 0x00, / CLA Class (byte) 0xB4, / INS Instruction (byte) 0x04, / P1 Parameter 1 (byte) 0x00, / P2 Parameter 2 (byte) 0x00, / Le ; try isodep.connect(); byte reaponse=null; logWindow.append(00B4040000+

18、n); try reaponse = isodep.getHistoricalBytes(); logWindow.append(Util.bytes2HexString(reaponse)+n); reaponse =isodep.transceive(cmd); logWindow.append(Util.bytes2HexString(reaponse)+n); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); catch (IOException e1) / TODO Auto-generated

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论