Tizen 2.0 Release
[samples/web/ContactsExchanger.git] / js / app.nfc.card.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app */
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         App.NFCCard = function (nfcControl) {
8                 this.app = nfcControl.app;
9
10         };
11
12         App.NFCCard.prototype = {
13
14                 prepareForNFC: null,
15
16         /* card read */
17
18                 readMessageErr: function readMessageErr(e) {
19                         app.nfc.timeExpired('Read error!');
20                 },
21
22                 sucTagReadAttach: function sucTagReadAttach(tag) {
23                         if (tag.isSupportedNDEF) {
24                                 tag.readNDEF(this.app.nfc.readMessage.bind(this.app.nfc), this.readMessageErr.bind(this));
25                         } else {
26                                 console.warn("This Tag doesn't support NDEF");
27                         }
28                 },
29
30                 setTagDetectRead: function setTagDetectRead() {
31                         var suc = { onattach: this.sucTagReadAttach.bind(this), ondetach: this.app.nfc.sucDetach.bind(this.app.nfc) };
32                         try {
33                                 app.nfc.nfcAdapter.setTagListener(suc);
34                         } catch (error) {
35                                 console.error('error: ' + error.message);
36                         }
37                 },
38
39
40         /* card write */
41
42                 sucSend: function sucSend() {
43                         app.nfc.timeExpired('Send success!');
44                 },
45
46                 errSend: function errSend(e) {
47                         console.warn('errSend');
48                         app.nfc.timeExpired('Send problem! ' + e.message);
49                 },
50
51                 sucTagWriteAttach: function sucTagWriteAttach(tag) {
52                         var newMessage = null,
53                                 separator = String.fromCharCode(30),
54                                 endOfText = String.fromCharCode(3),
55                                 fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName,
56                                 prepareForNFC;
57
58                         prepareForNFC = this.prepareForNFC || function prepareForNFC(contact) {
59                                 if (contact.length > 31) {
60                                         contact = contact.substring(0, 31);
61                                         if (contact[29] !== separator) {
62                                                 contact = contact.substring(0, 30) + endOfText;
63                                         }
64                                 }
65                                 return contact;
66                         };
67
68                         fullContact = prepareForNFC(fullContact);
69
70                         try {
71                                 newMessage = this.app.nfc.phoneNumber2NDEF(fullContact);
72                         } catch (err) {
73                                 console.error("NDEFMessage problem: " + err.message);
74                         }
75
76                         if (tag.isSupportedNDEF) {
77                                 tag.writeNDEF(newMessage, this.sucSend.bind(this), this.errSend.bind(this));
78                         } else {
79                                 console.warn("This Tag doesn't support NDEF");
80                         }
81                 },
82
83                 setTagDetectWrite: function setTagDetectWrite() {
84                         var suc = { onattach: this.sucTagWriteAttach.bind(this), ondetach: this.app.nfc.sucDetach };
85                         try {
86                                 app.nfc.nfcAdapter.setTagListener(suc);
87                         } catch (error) {
88                                 console.error('error: ' + error.message);
89                         }
90                 }
91
92         };
93
94 }());