2 /*global $, tizen, App, app */
4 (function () { // strict mode wrapper
7 App.NFCControl = function (app) {
12 App.NFCControl.prototype = {
14 nfcStateMemory: false,
16 init: function init() {
17 this.card = new App.NFCCard(this);
18 this.peer = new App.NFCPeer(this);
21 timeExpired: function timeExpired(monit) {
22 clearTimeout(this.app.timeOutHandler);
23 this.unsetTargetDetect();
24 this.unsetTagDetect();
25 this.app.ui.moveToStartPage(monit);
28 resolveContact: function resolveContact(contactsString) {
29 var separator = String.fromCharCode(30),
30 endOfText = String.fromCharCode(3),
37 contactsArray = contactsString.split(separator);
39 resolveData = function (value) {
43 return value.replace(endOfText, '…');
46 phone = resolveData(contactsArray[0]);
47 first = resolveData(contactsArray[1]);
48 last = resolveData(contactsArray[2]);
49 return {phone: phone, first: first, last: last};
52 fillRecordInfo: function fillRecordInfo(record) {
54 var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
55 this.app.ui.displayContact(contactsData);
61 readMessage: function readMessage(message) {
63 this.fillRecordInfo(message.records[0]);
65 console.error(e.message);
69 contact2NDEF: function contact2NDEF(contact) {
70 var t, a = [], i, newMessage = new tizen.NDEFMessage();
71 if (typeof contact === 'string') {
74 t = contact.convertToString("VCARD_30");
77 for (i = 0; i < a.length; i += 1) {
78 a[i] = t.charCodeAt(i);
80 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
84 phoneNumber2NDEF: function phoneNumber2NDEF(contact) {
85 var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage();
86 for (i = 0; i < length; i += 1) {
87 phoneNumberArray.push(contact.charCodeAt(i));
89 newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
93 convertNDEF2phoneNumber: function convertNDEF2phoneNumber(contact) {
94 var i, length = contact.length, phoneNumber = '';
95 for (i = 0; i < length; i += 1) {
96 phoneNumber += String.fromCharCode(contact[i]);
101 startNFC: function startNFC() {
102 var onPowerOn, onPowerOnFails;
104 onPowerOn = function () {
106 app.ui.loadStartPage();
108 onPowerOnFails = function (err) {
109 console.error('Power On error: ' + err.message);
113 this.nfcAdapter = tizen.nfc.getDefaultAdapter();
115 if (this.nfcAdapter.powered) {
116 this.nfcStateMemory = true;
119 this.nfcStateMemory = false;
120 this.nfcAdapter.setPowered(true, onPowerOn, onPowerOnFails);
123 console.error('setPowered(true) problem: ' + err.message);
126 console.error('getDefaultAdapter() method problem: ' + e.message);
130 stopNFC: function stopNFC() {
131 var onPowerOff, onPowerOffFails;
133 onPowerOff = function () {
134 tizen.application.getCurrentApplication().exit();
137 onPowerOffFails = function (err) {
138 console.error('Power Off error: ' + err.message);
139 tizen.application.getCurrentApplication().exit();
143 if (this.nfcStateMemory) {
146 this.nfcAdapter.setPowered(false, onPowerOff, onPowerOffFails);
149 console.error('setPowered(false) problem: ' + err);
153 unsetTargetDetect: function unsetTargetDetect() {
155 if (this.nfcTarget) {
156 this.nfcTarget.unsetReceiveNDEFListener();
157 this.nfcTarget = null;
159 console.warn("app.nfc.nfcTarget not set");
162 console.error('error: ' + error.message);
165 this.nfcAdapter.unsetPeerListener();
167 console.error('error: ' + e.message);
171 unsetTagDetect: function unsetTagDetect() {
173 this.nfcAdapter.unsetTagListener();
175 console.error('error: ' + error.message);
179 displayContact: function displayContact(obj) {
180 clearTimeout(this.app.timeOutHandler);
181 this.unsetTargetDetect();
182 this.unsetTagDetect();
183 this.app.ui.moveToContactPage(obj);
186 sucDetach: function sucDetach() {