У меня проблема с ti.nfc module
Appcelerator Titanium.
Это работает хорошо, только если код 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>
Кто-нибудь может мне помочь?
Большое спасибо.