df46512a5049bc7fe6bca97fd90867d2f7fe8679
[apps/web/sample/ContactsExchanger.git] / project / js / app.nfc.peer.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global $, tizen, App, app, localStorage*/
19
20 (function () { // strict mode wrapper
21     'use strict';
22
23     /**
24      * Constructs NFCPeer
25      * @constructor
26      * @param {NFCControl} nfc
27      */
28     App.NFCPeer = function (nfc) {
29         this.nfc = nfc;
30     };
31
32     App.NFCPeer.prototype = {
33
34         sucSendToTarget: function nfc_peer_sucSendToTarget() {},
35
36         errSendToTarget: function nfc_peer_errSendToTarget(e) {
37             this.nfc.timeExpired('Send problem! ' + e.message);
38         },
39
40         setReceiveFromTarget: function nfc_peer_setReceiveFromTarget() {
41             try {
42                 if (!this.nfc.nfcTarget) {
43                     console.warn("app.nfc.nfcTarget not set");
44                     return;
45                 }
46                 this.nfc.nfcTarget.unsetReceiveNDEFListener();
47                 this.nfc.nfcTarget.setReceiveNDEFListener(
48                     this.nfc.readMessage.bind(this.nfc)
49                 );
50             } catch (error) {
51                 console.error('setReceiveFromTarget error: ' + error.message);
52             }
53         },
54
55         sucTargetAttach: function nfc_peer_sucTargetAttach(target) {
56             var newMessage = null,
57                 fullContact = this.nfc.prepareForNFC(localStorage);
58             this.nfc.nfcTarget = target;
59             this.setReceiveFromTarget();
60
61             try {
62                 newMessage = this.nfc.phoneNumber2NDEF(fullContact);
63                 target.sendNDEF(
64                     newMessage,
65                     this.sucSendToTarget.bind(this),
66                     this.errSendToTarget.bind(this)
67                 );
68             } catch (e) {
69                 console.error("NDEFMessage problem: " + e.message);
70             }
71         },
72
73         setTargetDetect: function nfc_peer_setTargetDetect() {
74             var successCallbacks = {
75                 onattach: this.sucTargetAttach.bind(this),
76                 ondetach: this.nfc.sucDetach.bind(this.nfc)
77             };
78
79             try {
80                 this.nfc.nfcAdapter.unsetPeerListener();
81                 this.nfc.nfcAdapter.setPeerListener(successCallbacks);
82             } catch (error) {
83                 console.error(error.message);
84             }
85         }
86     };
87
88 }());