基于钛的NFC应用程序仅在NFC代码位于索引.js中时才有效
NFC app based on Titanium works only if NFC code is in index.js
我对Appcelerator Titanium的ti.nfc module
有问题。
只有当 NFC 代码在索引中时,它才能正常工作.js如果我从索引.js传递到另一个页面,如登录.js使用以下代码:
var login = Alloy.createController("login").getView();
login.open();
我把相同的NFC代码从索引.js登录.js它不起作用。
这是有效的代码:
索引.js
var nfc = require('ti.nfc');
var nfcAdapter = null;
// Create the NFC adapter to be associated with this activity.
// There should only be ONE adapter created per activity.
nfcAdapter = nfc.createNfcAdapter({
onNdefDiscovered: handleNDEFDiscovery,
onTagDiscovered: handleTAGDiscovery,
onTechDiscovered: handleTECHDiscovery
});
function handleNDEFDiscovery(data){
//Ti.API.info('NDEF DISCOVERED:: ' + data.messages[0].records[0].getPayload());
// Our required payload fits in the first record of the first Ndef msg
var payload = data.messages[0].records[0].getPayload();
// First byte of payload is control byte
// Bits 5 to 0 of the control bytes contain
// length of language code succeeding the control byte
var langCodeLength = payload[0] & 0077;
// Received NFC text data starts after textOffset bytes in payload
var textOffset = langCodeLength + 1;
// Payload contains byte array which needs to be converted to a string
// nfc_text contains the exact text string sent by the sending NFC device
var nfc_text = payload.toString().substring(textOffset);
/* process nfc_text as required by application logic here */
}
function handleTAGDiscovery(data){
//Ti.API.info('TAG DISCOVERED:: ' + data.messages[0].records[0].getPayload());
}
function handleTECHDiscovery(data){
//Ti.API.info('TECH DISCOVERED:: ' + data.messages[0].records[0].getPayload());
var tag = data.tag;
alert('Tag ID: ' + tag.getId());
}
// Check for NFC support on device
if (!nfcAdapter.isEnabled()) {
Ti.API.info('NFC is not enabled on this device!');
}else{
// Tag scans are received by the current activity as new intents. We
// need to pass scan intents to the nfc adapter for processing.
var act = Ti.Android.currentActivity;
act.addEventListener('newintent', function(e) {
nfcAdapter.onNewIntent(e.intent);
});
// Since we want the app to only use NFC while active in the foreground
// We disable and enable foreground dispatch on app pause and resume respectively
act.addEventListener('resume', function(e) {
nfcAdapter.enableForegroundDispatch(dispatchFilter);
});
act.addEventListener('pause', function(e) {
nfcAdapter.disableForegroundDispatch();
});
// The foreground dispatch filter specifies the types of NFC
// messages the app wants to receive and handle. The only entry in our case
// specifies type ‘text/plain’ since we want to send and receive plain text
dispatchFilter = nfc.createNfcForegroundDispatchFilter({
intentFilters: [
{ action: nfc.ACTION_TECH_DISCOVERED },
],
techLists: [
[ "android.nfc.tech.NfcV" ]
]
});
// This enables foreground dispatch for the first time
nfcAdapter.enableForegroundDispatch(dispatchFilter);
}
Android tiapp.xml:
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="android.permission.NFC"/>
<application>
<activity
android:label="TagViewer" 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>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
</application>
</manifest>
</android>
有人可以帮助我吗?
多谢。
这很可能是使用 Ti.Android.currentActivity
的问题。尝试改用 Ti.Window.activity,窗口是登录视图的窗口(或 TabGroup)。
如果这解决了它,那么看起来我们在Ti.Android.currentActivity
中有一个错误和/或文档需要更新。
相关文章:
- 名称输入的索引
- 在jQuery中获取表的行索引
- 测试索引值是否等于某个数字的倍数
- 循环遍历数组中的特定索引
- 按照选项卡索引的顺序循环一个jQuery选择
- 在JavaScript中通过索引从对象数组中获取值
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- 在索引.html和应用.js [node.js] 之间共享变量
- 如何为高图中的区域线创建z索引
- 下拉列表在使用z索引放置在前面后停止工作
- 减去两个索引不同但值相同的整数
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- Javascript:根据对象的嵌套数组中的值,在数组中查找对象的索引
- 用javascript查询lucene索引
- 使用它更新集合中的嵌套数组's索引
- 如何将ng选项的索引作为angularJs中的值传递给模型
- Javascript获取所有锚链接的索引
- 多维关联数组的最后一个索引
- 传递给 PHP 表单时 JavaScript 动态变量上的未定义索引
- 基于钛的NFC应用程序仅在NFC代码位于索引.js中时才有效