Tizen 2.0 Release
[samples/web/ContactsExchanger.git] / js / app.nfc.peer.js
1 /*jslint devel: true*/
2 /*global $, tizen, App, app */
3
4 (function () { // strict mode wrapper
5         'use strict';
6
7         App.NFCPeer = function (nfcControl) {
8                 this.app = nfcControl.app;
9         };
10
11         App.NFCPeer.prototype = {
12
13                 sucSendToTarget: function sucSendToTarget() {
14                         //timeExpired('Send success!');
15                 },
16
17                 errSendToTarget: function errSendToTarget(e) {
18                         this.app.nfc.timeExpired('Send problem! ' + e.message);
19                 },
20
21                 errorMessage: function errorMessage(e) {
22                         console.log('Target Receiving Error: ' + e.message);
23                 },
24
25                 setReceiveFromTarget: function setReceiveFromTarget() {
26                         try {
27                                 if (this.app.nfc.nfcTarget) {
28                                         this.app.nfc.nfcTarget.setReceiveNDEFListener(this.app.nfc.readMessage.bind(this.app.nfc), this.errorMessage.bind(this));
29                                 } else {
30                                         console.warn("app.nfc.nfcTarget not set");
31                                 }
32                         } catch (error) {
33                                 console.log('setReceiveFromTarget error: ' + error.message);
34                         }
35                 },
36
37                 sucTargetAttach: function sucTargetAttach(target) {
38                         var newMessage = null,
39                                 separator = String.fromCharCode(30),
40                                 endOfText = String.fromCharCode(3),
41                                 fullContact = localStorage.phoneNumber + separator + localStorage.firstName + separator + localStorage.lastName;
42                         console.log('sucTargetAttach: ', target);
43                         this.app.nfc.nfcTarget = target;
44                         this.setReceiveFromTarget();
45
46                         try {
47                                 newMessage = this.app.nfc.phoneNumber2NDEF(fullContact);
48                         } catch (err) {
49                                 console.log("NDEFMessage problem: " + err.message);
50                         }
51
52                         try {
53                                 target.sendNDEF(newMessage, this.sucSendToTarget.bind(this), this.errSendToTarget.bind(this));
54                         } catch (e) {
55                                 console.log("NDEFMessage problem: " + e.message);
56                         }
57                 },
58
59                 setTargetDetect: function setTargetDetect() {
60                         var err = function (e) {
61                                 console.log('Target Listen Error: ' + e.message);
62                         },
63                                 suc = { onattach: this.sucTargetAttach.bind(this), ondetach: this.app.nfc.sucDetach.bind(this.app.nfc) };
64                         try {
65                                 app.nfc.nfcAdapter.setPeerListener(suc, err);
66                         } catch (error) {
67                                 console.log('error: ' + error.message);
68                         }
69                 }
70         };
71
72 }());