基于钛的NFC应用程序仅在NFC代码位于索引.js中时才有效

NFC app based on Titanium works only if NFC code is in index.js

本文关键字:NFC 索引 js 有效 于索引 应用程序 于钛 代码      更新时间:2023-09-26

我对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中有一个错误和/或文档需要更新。