Javascript 只有当NFC代码在index.js中时,基于钛的NFC应用程序才有效
我对Appcelerator Titanium的Javascript 只有当NFC代码在index.js中时,基于钛的NFC应用程序才有效,javascript,android,nfc,appcelerator,appcelerator-titanium,Javascript,Android,Nfc,Appcelerator,Appcelerator Titanium,我对Appcelerator Titanium的ti.nfc模块有问题 只有当NFC代码在index.js中时,如果我使用以下代码从index.js传递到另一个页面(如login.js),它才能正常工作: var login = Alloy.createController("login").getView(); login.open(); 我把相同的NFC代码从index.js放到login.js,它不起作用 这是有效的代码: index.js var nfc = require('ti.n
ti.nfc模块有问题
只有当NFC代码在index.js中时,如果我使用以下代码从index.js传递到另一个页面(如login.js),它才能正常工作:
var login = Alloy.createController("login").getView();
login.open();
我把相同的NFC代码从index.js放到login.js,它不起作用
这是有效的代码:
index.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.Android.currentActivity
和/或文档需要更新中有一个bug。什么不起作用?您是否收到错误?当我将标签卡靠在手机背面时,我的OnTechDiscovery回调中没有收到标签发现事件。我试图用Ti.Window.activity替换Ti.Android.currentActivity,但它没有解决问题。那么您是否可以检查这是否是当前已知的问题?如果不是,创建一个记录单,链接到这个问题,但也在记录单中提供可复制的代码、步骤和环境信息。别忘了在这里放一个到票的链接,这样其他人就可以和你一起看了。