Revert "Updated application sources"
[apps/web/sample/ContactsExchanger.git] / project / 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         /**
8          * Constructs NFCPeer
9          * @constructor
10          * @param {NFCControl} nfc
11          */
12         App.NFCPeer = function (nfc) {
13                 this.nfc = nfc;
14         };
15
16         App.NFCPeer.prototype = {
17
18                 sucSendToTarget: function nfc_peer_sucSendToTarget() {
19                 },
20
21                 errSendToTarget: function nfc_peer_errSendToTarget(e) {
22                         this.nfc.timeExpired('Send problem! ' + e.message);
23                 },
24
25                 setReceiveFromTarget: function nfc_peer_setReceiveFromTarget() {
26                         try {
27                                 if (!this.nfc.nfcTarget) {
28                                         console.warn("app.nfc.nfcTarget not set");
29                                         return;
30                                 }
31                                 this.nfc.nfcTarget.setReceiveNDEFListener(
32                                         this.nfc.readMessage.bind(this.nfc)
33                                 );
34                         } catch (error) {
35                                 console.error('setReceiveFromTarget error: ' + error.message);
36                         }
37                 },
38
39                 sucTargetAttach: function nfc_peer_sucTargetAttach(target) {
40                         var newMessage = null,
41                                 fullContact = this.nfc.prepareForNFC(localStorage);
42
43                         this.nfc.nfcTarget = target;
44                         this.setReceiveFromTarget();
45
46                         try {
47                                 newMessage = this.nfc.phoneNumber2NDEF(fullContact);
48                                 target.sendNDEF(
49                                         newMessage,
50                                         this.sucSendToTarget.bind(this),
51                                         this.errSendToTarget.bind(this)
52                                 );
53                         } catch (e) {
54                                 console.error("NDEFMessage problem: " + e.message);
55                         }
56                 },
57
58                 setTargetDetect: function nfc_peer_setTargetDetect() {
59                         var successCallbacks = {
60                                         onattach: this.sucTargetAttach.bind(this),
61                                         ondetach: this.nfc.sucDetach.bind(this.nfc)
62                                 };
63
64                         try {
65                                 this.nfc.nfcAdapter.setPeerListener(successCallbacks);
66                         } catch (error) {
67                                 console.error(error.message);
68                         }
69                 }
70         };
71
72 }());