2 /*global $, tizen, App, app */
4 (function () { // strict mode wrapper
7 App.NFCControl = function (app) {
8 console.log('NFCControl: ', app);
13 App.NFCControl.prototype = {
15 nfcStateMemory: false,
17 init: function init() {
18 this.card = new App.NFCCard(this);
19 this.peer = new App.NFCPeer(this);
22 timeExpired: function timeExpired(monit) {
23 console.log('timeExpired: ', this);
24 clearTimeout(this.app.timeOutHandler);
25 console.log(this.nfcTarget, 'this.nfcTarget');
26 this.unsetTargetDetect();
27 this.unsetTagDetect();
28 this.app.ui.moveToStartPage(monit);
31 resolveContact: function resolveContact(contactsString) {
32 var separator = String.fromCharCode(30),
33 endOfText = String.fromCharCode(3),
40 contactsArray = contactsString.split(separator);
42 resolveData = function (value) {
46 return value.replace(endOfText, '…');
49 phone = resolveData(contactsArray[0]);
50 first = resolveData(contactsArray[1]);
51 last = resolveData(contactsArray[2]);
52 return {phone: phone, first: first, last: last};
55 fillRecordInfo: function fillRecordInfo(record) {
57 var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
58 this.app.ui.displayContact(contactsData);
64 readMessage: function readMessage(message) {
65 console.log('readMessage', message);
67 this.fillRecordInfo(message.records[0]);
69 console.log(e.message);
73 contact2NDEF: function contact2NDEF(contact) {
74 var t, a = [], i, newMessage = new tizen.NDEFMessage();
75 if (typeof contact === 'string') {
78 t = contact.convertToString("VCARD_30");
81 for (i = 0; i < a.length; i += 1) {
82 a[i] = t.charCodeAt(i);
84 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
88 phoneNumber2NDEF: function phoneNumber2NDEF(contact) {
89 var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage();
90 for (i = 0; i < length; i += 1) {
91 phoneNumberArray.push(contact.charCodeAt(i));
93 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
97 convertNDEF2phoneNumber: function convertNDEF2phoneNumber(contact) {
98 var i, length = contact.length, phoneNumber = '';
99 for (i = 0; i < length; i += 1) {
100 phoneNumber += String.fromCharCode(contact[i]);
105 startNFC: function startNFC() {
106 var onPowerOn, onPowerOnFails;
108 onPowerOn = function () {
110 app.ui.loadStartPage();
112 onPowerOnFails = function (err) {
113 console.log('Power On error: ' + err.message);
117 this.nfcAdapter = tizen.nfc.getDefaultAdapter();
119 if (this.nfcAdapter.powered) {
120 console.log('this.nfcAdapter.powered');
121 this.nfcStateMemory = true;
124 console.log('!this.nfcAdapter.powered');
125 this.nfcStateMemory = false;
126 this.nfcAdapter.setPowered(true, onPowerOn, onPowerOnFails);
129 console.log('setPowered(true) problem: ' + err);
132 console.log('getDefaultAdapter() method problem: ' + e);
136 stopNFC: function stopNFC() {
137 var onPowerOff, onPowerOffFails;
139 onPowerOff = function () {
140 tizen.application.getCurrentApplication().exit();
143 onPowerOffFails = function (err) {
144 console.log('Power Off error: ' + err.message);
145 tizen.application.getCurrentApplication().exit();
149 if (this.nfcStateMemory) {
150 console.log('this.nfcStateMemory');
153 console.log('!this.nfcStateMemory');
154 this.nfcAdapter.setPowered(false, onPowerOff, onPowerOffFails);
157 console.log('setPowered(false) problem: ' + err);
161 unsetTargetDetect: function unsetTargetDetect() {
162 console.log('unsetTargetDetect: ', this);
164 if (this.nfcTarget) {
165 this.nfcTarget.unsetReceiveNDEFListener();
166 this.nfcTarget = null;
168 console.warn("app.nfc.nfcTarget not set");
171 console.log('error: ' + error.message);
174 this.nfcAdapter.unsetPeerListener();
176 console.log('error: ' + e.message);
180 unsetTagDetect: function unsetTagDetect() {
182 this.nfcAdapter.unsetTagListener();
184 console.log('error: ' + error.message);
188 displayContact: function displayContact(obj) {
189 clearTimeout(this.app.timeOutHandler);
190 this.unsetTargetDetect();
191 this.unsetTagDetect();
192 this.app.ui.moveToContactPage(obj);
195 sucDetach: function sucDetach() {
196 console.log('sucDetach');