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                 console.log('NFCCard: ', nfcControl);
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.log("This Tag doesn't support NDEF");
27                         }
28                 },
29
30                 setTagDetectRead: function setTagDetectRead() {
31                         /*
32                         var err, suc;
33                         err = function (e) {
34                                 console.log('Tag Listen Error: ' + e.message);
35                         };
36                         */
37                         var suc = { onattach: this.sucTagReadAttach.bind(this), ondetach: this.app.nfc.sucDetach.bind(this.app.nfc) };
38                         try {
39                                 //nfcAdapter.setTagListener(suc, err);
40                                 app.nfc.nfcAdapter.setTagListener(suc);
41                         } catch (error) {
42                                 console.log('error: ' + error.message);
43                         }
44                 },
45
46
47         /* card write */
48
49                 sucSend: function sucSend() {
50                         console.log('sucSend');
51                         app.nfc.timeExpired('Send success!');
52                 },
53
54                 errSend: function errSend(e) {
55                         console.log('errSend');
56                         app.nfc.timeExpired('Send problem! ' + e.message);
57                 },
58
59                 sucTagWriteAttach: function sucTagWriteAttach(tag) {
60                         var newMessage = null,
61                                 separator = String.fromCharCode(30),
62                                 endOfText = String.fromCharCode(3),
63                                 fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName,
64                                 prepareForNFC;
65
66                         prepareForNFC = this.prepareForNFC || function prepareForNFC(contact) {
67                                 if (contact.length > 31) {
68                                         contact = contact.substring(0, 31);
69                                         if (contact[29] !== separator) {
70                                                 contact = contact.substring(0, 30) + endOfText;
71                                         }
72                                 }
73                                 return contact;
74                         };
75
76                         fullContact = prepareForNFC(fullContact);
77
78                         try {
79                                 newMessage = this.app.nfc.phoneNumber2NDEF(fullContact);
80                         } catch (err) {
81                                 console.log("NDEFMessage problem: " + err.message);
82                         }
83
84                         if (tag.isSupportedNDEF) {
85                                 tag.writeNDEF(newMessage, this.sucSend.bind(this), this.errSend.bind(this));
86                         } else {
87                                 console.log("This Tag doesn't support NDEF");
88                         }
89                 },
90
91                 setTagDetectWrite: function setTagDetectWrite() {
92                         /*
93                         var err, suc;
94                         err = function (e) {
95                                 console.log('Tag Listen Error: ' + e.message);
96                         };
97                         */
98                         var suc = { onattach: this.sucTagWriteAttach.bind(this), ondetach: this.app.nfc.sucDetach };
99                         try {
100                                 //nfcAdapter.setTagListener(suc, err);
101                                 app.nfc.nfcAdapter.setTagListener(suc);
102                         } catch (error) {
103                                 console.log('error: ' + error.message);
104                         }
105                 }
106
107         };
108
109 }());