{"id":13,"date":"2011-01-14T17:05:24","date_gmt":"2011-01-14T20:05:24","guid":{"rendered":"http:\/\/gibraltarsf.com\/blog\/?p=13"},"modified":"2015-06-07T08:55:09","modified_gmt":"2015-06-07T11:55:09","slug":"writing-tags-with-nexus-s","status":"publish","type":"post","link":"http:\/\/gibraltarsf.com\/blog\/?p=13","title":{"rendered":"Writing tags with Nexus S"},"content":{"rendered":"<p>As <a href=\"http:\/\/www.youtube.com\/watch?v=AKOWK2dR4Dg\">said by Google CEO Eric Schmidt<\/a> two month ago,\u00a0the operating system\u00a0Android 2.3\u00a0\u00a0includes support for near field communication. But,\u00a0at least in <a href=\"http:\/\/developer.android.com\/reference\/android\/nfc\/package-descr.html\">terms of the API<\/a> and <a href=\"http:\/\/developer.android.com\/resources\/samples\/NFCDemo\/index.html\">the sample code<\/a> they supply, this version\u00a0only includes tag reading capability. However,\u00a0the\u00a0 PN544 NFC chip\u00a0inside the\u00a0Nexus S\u00a0supports read and write operations so, at\u00a0the hardware level, all the functionality you need\u00a0to support a full range of\u00a0NFC\u00a0services is in the handset. At the software level,\u00a0there will be updates to the SDK rolled out on a phased basis that will\u00a0enable developers to write NFC applications for mobile payments, p2p\u00a0and other applications on Android Gingerbread devices.<!--more--><\/p>\n<p>We&#8217;ve been analyzing the\u00a0 <a href=\"http:\/\/source.android.com\/source\/download.html\">Android kernel source code<\/a> and we found\u00a0a lot of\u00a0&#8220;@hide&#8221; tags in the NFC java classes. These are in:<\/p>\n<p style=\"padding-left: 30px;\">frameworks\/base\/core\/java\/android\/nfc<br \/>\nframeworks\/base\/core\/java\/com\/android\/internal\/nfc<br \/>\npackages\/apps\/Nfc (the android service running in the background)<br \/>\npackages\/apps\/Tag (the application used to read and store tags)<\/p>\n<p>And the hardware abstraction layer from NXP is in:<\/p>\n<p style=\"padding-left: 30px;\">external\/libnfc-nxp<\/p>\n<p>Let&#8217;s see an example, the publised API for the class NCFAdapter has only two public methods:<\/p>\n<p style=\"padding-left: 30px;\"><em>getDefaultAdapter()<\/em>: Get a handle to the default NFC Adapter on this Android device.<br \/>\n<em>isEnabled()<\/em>: Return true if this NFC Adapter has any features enabled.<\/p>\n<p>But also has this public methods with the @hide tag:<\/p>\n<p style=\"padding-left: 30px;\"><em>enable()<\/em>: Enable NFC hardware.<br \/>\n<em>disable()<\/em>: Disable NFC hardware.<br \/>\n<em>createRawTagConnection()<\/em>: Create a raw tag connection to the default Target.<br \/>\n<em>createNdefTagConnection()<\/em>: Create an NDEF tag connection to the default Target .<\/p>\n<p>Interesting, isn&#8217;t it?<\/p>\n<p>The &#8220;@hide&#8221; tag is used to mark public APIs that are not to be exported in the SDK. This is used by the Android team to have APIs accessible across packages without having them available to applications. APIs marked with @hide are considered private to the Android platform and can change at any time, so you cannot rely on them. So, with this in mind, we can confirm\u00a0that\u00a0there is some\u00a0extended NFC functionality\u00a0hidden\u00a0in\u00a0Android 2.3 that can be\u00a0accessed,\u00a0i.e.\u00a0with reflection techniques.\u00a0Let&#8217;s see how we can do this.<\/p>\n<p>To write tags we&#8217;ll use the write() method from the INfcTag interface, so lets dive from NfcAdapter to the INfcTag.\u00a0First we need a handle to the NFC adapter within the Nexus S:<br \/>\n<code><br \/>\nNfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter();<br \/>\n<\/code><br \/>\nThen we need a handle to the actually detected tag, this is done by getting the the data within the intent received when tag was discovered:<br \/>\n<code><br \/>\nParcelable nfcTag = intent.getParcelableExtra(\"android.nfc.extra.TAG\");<br \/>\nField f = nfcTag.getClass().getDeclaredField(\"mServiceHandle\");<br \/>\nf.setAccessible(true);<br \/>\nObject mServiceHandle = f.get(nfcTag);<br \/>\n<\/code><br \/>\nNow, we can create a raw connection to the tag, this is a low-level connection to a tag target. We&#8217;ll use this object to get later a handle to the INfcTag<br \/>\n<code><br \/>\nClass tag = Class.forName(\"android.nfc.Tag\");<br \/>\nMethod createRawTagConnection = nfcAdapter.getClass().getMethod(\"createRawTagConnection\", tag);<br \/>\nObject rawTagConnection = createRawTagConnection.invoke(nfcAdapter, nfcTag);<br \/>\n<\/code><br \/>\nNow we can get the handle to the INfcTag interface (mTagService).<br \/>\n<code><br \/>\nField f = rawTagConnection.getClass().getDeclaredField(\"mTagService\");<br \/>\nf.setAccessible(true);<br \/>\nObject mTagService = f.get(rawTagConnection);<br \/>\n<\/code><br \/>\nBefore we can write to the tag we connect our interface to it.<br \/>\n<code><br \/>\nMethod connect = mTagService.getClass().getMethod(\"connect\", Integer.TYPE);<br \/>\nconnect.invoke(mTagService, mServiceHandle.intValue());<br \/>\n<\/code><br \/>\nFinally, we create the NdefTagRecord, this could be a smart poster, an uri, simple text or whatever record we want to write into the tag. This record will be encapsulated into an NdefMessage, so when we invoke the write() method we use this ndefMessage as parameter:<br \/>\n<code><br \/>\nMethod write = mTagService.getClass().getMethod(\"write\", Integer.TYPE, NdefMessage.class);<br \/>\nwrite.invoke(mTagService, mServiceHandle, ndefMessage);<br \/>\n<\/code><br \/>\n&#8230; and that&#8217;s it!<br \/>\nThe write() method will work as long as you use a tag pre-formatted with any NFC Forum type tag with default public keys.<br \/>\nWe developed an application using this code to write tags. Below you can see a demo of this app writing an URI type tag:<\/p>\n<p><iframe loading=\"lazy\" title=\"NFC - Writing tags with Nexus S\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Jjw72UekRJ0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As said by Google CEO Eric Schmidt two month ago,\u00a0the operating system\u00a0Android 2.3\u00a0\u00a0includes support for near field communication. But,\u00a0at least in terms of the API and the sample code they supply, this version\u00a0only includes tag reading capability. However,\u00a0the\u00a0 PN544 NFC chip\u00a0inside the\u00a0Nexus S\u00a0supports read and write operations so, at\u00a0the hardware level, all the functionality you [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14,3,4,6],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-android-2-3","tag-nexus-s","tag-nfc","tag-tag"],"_links":{"self":[{"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":52,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":310,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/310"}],"wp:attachment":[{"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/gibraltarsf.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}