Hi, I am using the Titanium NFC module "TagWriter", and I'm getting an error when I attempt to write to the tag. Please note in order to get the module to work I am using Titanium SDK 3.2.2 and Android's API 19.
Here is the error I am receiving:
[WARN] : W/System.err: java.io.IOException [WARN] : W/System.err: at android.nfc.tech.BasicTagTechnology.connect(BasicTagTechnology.java:85) [WARN] : W/System.err: at android.nfc.tech.Ndef.connect(Ndef.java:72) [WARN] : W/System.err: at ti.nfc.tech.TagTechnologyProxy.connect(TagTechnologyProxy.java:83) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Object.nativeFireEvent(Native Method) [WARN] : W/System.err: at org.appcelerator.kroll.runtime.v8.V8Object.fireEvent(V8Object.java:64) [WARN] : W/System.err: at org.appcelerator.kroll.KrollProxy.doFireEvent(KrollProxy.java:884) [WARN] : W/System.err: at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1107) [WARN] : W/System.err: at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:327) [WARN] : W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98) [WARN] : W/System.err: at android.os.Looper.loop(Looper.java:136) [WARN] : W/System.err: at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112) [INFO] : ALERT: (KrollRuntimeThread) [7092,37141] Error: Java Exception occurredHere is my code:
tiapp.xml
<?xml version="1.0" encoding="UTF-8"?> <ti:app xmlns:ti="http://ti.appcelerator.org"> <id>com.kerrin.TagWriter</id> <name>TagWriter</name> <version>1.0</version> <publisher>Kerrin</publisher> <url>http://</url> <description>undefined</description> <copyright>2015 by Kerrin</copyright> <icon>appicon.png</icon> <fullscreen>false</fullscreen> <navbar-hidden>false</navbar-hidden> <analytics>true</analytics> <guid>c028c8b4-3f31-4344-9873-a003f1b69eae</guid> <property name="ti.ui.defaultunit" type="string">dp</property> <property name="ti.android.useLegacyWindow" type="bool">true</property> <ios> <plist> <dict> <key>UISupportedInterfaceOrientations~iphone</key> <array> <string>UIInterfaceOrientationPortrait</string> </array> <key>UISupportedInterfaceOrientations~ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> <key>UIRequiresPersistentWiFi</key> <false/> <key>UIPrerenderedIcon</key> <false/> <key>UIStatusBarHidden</key> <false/> <key>UIStatusBarStyle</key> <string>UIStatusBarStyleDefault</string> </dict> </plist> </ios> <android xmlns:android="http://schemas.android.com/apk/res/android"> <uses-sdk>19</uses-sdk> <manifest> <application> <activity android:configChanges="keyboardHidden|orientation" android:label="TagWriter" android:launchMode="singleTask" android:name=".TagwriterActivity" android:theme="@style/Theme.Titanium"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity> </application> </manifest> </android> <mobileweb> <precache/> <splash> <enabled>true</enabled> <inline-css-images>true</inline-css-images> </splash> <theme>default</theme> </mobileweb> <modules> <module platform="android">ti.nfc</module> </modules> <deployment-targets> <target device="android">true</target> <target device="blackberry">false</target> <target device="ipad">false</target> <target device="iphone">false</target> <target device="mobileweb">false</target> </deployment-targets> <sdk-version>3.2.2.GA</sdk-version> <plugins> <plugin version="1.0">ti.alloy</plugin> </plugins> </ti:app>
index.js
/* * NFC Tag Writer Example Application * * This application demonstrates how to use the NFC module to write to NFC tags. * * Before running this application, add code similar to the following to your application's * tiapp.xml file. Note the following: * - The activity name for your application may be different * - android:launchMode="singleTask" is needed so that new intents that result from * NFC message dispatching do not start a new activity in your application * * <android xmlns:android="http://schemas.android.com/apk/res/android"> * <manifest> * <application> * <activity android:name=".TagwriterActivity" * android:label="TagWriter" android:theme="@style/Theme.Titanium" * android:configChanges="keyboardHidden|orientation" * android:launchMode="singleTask"> * <intent-filter> * <action android:name="android.intent.action.MAIN" /> * <category android:name="android.intent.category.LAUNCHER" /> * </intent-filter> * </activity> * </application> * </manifest> * </android> */ var nfc = require('ti.nfc'); var nfcAdapter = null; var dispatchFilter = null; var scannedTag = null; $.index.addEventListener('open', function(e) { // Must wait until the activity has been opened before setting up NFC setupNfc(); }); // Force the default message into the data area onClear(); $.index.open(); function setupNfc() { // Create the NFC adapter to be associated with this activity. // There should only be ONE adapter created per activity. nfcAdapter = nfc.createNfcAdapter({ onTagDiscovered: handleDiscovery }); // It's possible that the device does not support NFC. Check it here // before we go any further. if (!nfcAdapter.isEnabled()) { alert('NFC is not enabled on this device'); return; } // All tag scans are received by the activity as a new intent. Each // scan intent needs to be passed to the nfc adapter for processing. var act = Ti.Android.currentActivity; act.addEventListener('newintent', function(e) { nfcAdapter.onNewIntent(e.intent); }); // To enable NFC dispatching only while the application is in the foreground, // the application must signal the module whenever the application state changes. act.addEventListener('resume', function(e) { nfcAdapter.enableForegroundDispatch(dispatchFilter); }); act.addEventListener('pause', function(e) { nfcAdapter.disableForegroundDispatch(); }); // This application is only interested in receiving NFC messages while // in the foreground. So the dispatch filter must be defined to identify // what types of NFC messages to receive. dispatchFilter = nfc.createNfcForegroundDispatchFilter({ intentFilters: [ { action: nfc.ACTION_TAG_DISCOVERED} ] }); } function handleDiscovery(e) { // A recognized NCF message was discovered and routed to this application. // Just display the contents of the messages in the scroll view. $.tagData.value = JSON.stringify(e, function(key, value) { if(key === 'source') { return undefined; } else { return value; } }, 2); // Save off the scanned tag scannedTag = e.tag; // Determine if tag supports the format we are looking for in order to write $.writeButton.enabled = scannedTag.techList.indexOf(nfc.TECH_NDEF) >= 0; } function onWrite(e) { var tech = nfc.createTagTechnologyNdef({ tag: scannedTag }); // We checked when the tag was scanned that it supported the necessary tag type (Ndef in this case). if (!tech.isValid()) { alert("Failed to create Ndef tag type"); return; } // Attempt to write an Ndef record to the tag try { tech.connect(); // It's possible that the tag is not writable, so we need to check first. if (!tech.isWritable()) { alert ("Tag is not writable"); } else { // Create a new message to write to the tag var date = new Date(); var textRecord = nfc.createNdefRecordText({ text: "Titanium NFC module. Tag updated on " + date.toLocaleString() + "!!!" }); var msg = nfc.createNdefMessage({ records: [ textRecord ] }); // For good measure, confirm that the message is not too big for the tag var blob = msg.toByte(); if (blob.length > tech.getMaxSize()) { alert("Tag capacity is " + tech.getMaxSize() + ", but message size is " + blob.length); } else { // Write to the tag tech.writeNdefMessage(msg); alert("Tag successfully updated!"); onClear(); } } } catch (e) { alert("Error: " + e.message); $.writeButton.enabled = false; } finally { if (tech.isConnected()) { tech.close(); } } } function onClear(e) { $.tagData.value = "This application will only receive or write NFC data when it is in the foreground.\n\nThis application will write to discovered tags that support the Ndef format.\n\nScan a tag and tap on 'Write' to write to the tag."; }thanks!