1 /*jslint plusplus: true, sloppy: true, todo: true, vars: true, browser: true, devel: true, maxerr: 999 */
2 /*global $, tizen, app */
6 function ClientModel(parent) {
12 (function () { // strict mode wrapper
14 ClientModel.prototype = {
17 * API module initialisation
19 init: function ClientModel_init() {},
21 searchServer: function ClientModel_searchServer() {
22 var discoverDevicesSuccessCallback = {
23 onstarted: function () {
24 this.client.setDiscovering(true);
26 ondevicefound: function (device) {
27 this.client.addDeviceToList(device);
29 ondevicedisappeared: function (address) {},
30 onfinished: function (devices) {
31 this.client.setDiscovering(false);
35 this.client.adapter.discoverDevices(discoverDevicesSuccessCallback, function (e) { this.client.setDiscovering(false); });
38 stopServerSearching: function ClientModel_stopServerSearching(callback) {
39 if (this.client.getDiscovering()) {
40 this.client.adapter.stopDiscovery(function () {
41 this.client.setDiscovering(false);
42 if (typeof callback === 'function') {
45 }.bind(this), function (e) {
46 console.error("Error while stopDiscovery:" + e.message);
48 } else if (typeof callback === 'function') {
53 startBonding: function ClientModel_startBonding(address, callback) {
54 this.client.adapter.createBonding(address, function (device) { callback(device); }, function (error) { console.error('bondError: ' + error.message); });
57 connectToService: function ClientModel_connectToService(device, serviceUUID, successCallback, errorCallback) {
58 this.client.chatServerDevice = device;
60 device.connectToServiceByUUID(serviceUUID, successCallback, errorCallback, 'RFCOMM');
62 console.error('connectToServiceByUUID ERROR: ' + error.message);
66 sendPing: function ClientModel_sendPing(name, socket) {
67 var sendTextMsg, messageObj, messageObjToString, i, len;
69 messageObj = {name: encodeURIComponent(name), text: '', ping: true, bye: false};
70 messageObjToString = JSON.stringify(messageObj);
71 len = messageObjToString.length;
73 for (i = 0; i < len; i += 1) {
74 sendTextMsg[i] = messageObjToString.charCodeAt(i);
77 if (socket !== null && socket.state === "OPEN") {
78 socket.writeData(sendTextMsg);
81 console.error('sendPing: ' + error.message);
85 sendMessage: function ClientModel_sendMessage(name, socket, message, callback) {
86 var sendTextMsg = [], messageObj, messageObjToString, i, len;
87 name = encodeURIComponent(name);
88 message = encodeURIComponent(message);
89 messageObj = {name: name, text: message, ping: false, bye: false};
90 messageObjToString = JSON.stringify(messageObj);
91 len = messageObjToString.length;
92 for (i = 0; i < len; i += 1) {
93 sendTextMsg[i] = messageObjToString.charCodeAt(i);
96 if (socket !== null && socket.state === "OPEN") {
97 socket.writeData(sendTextMsg);
101 console.error('sendMessage: ' + error.message);
105 sendBye: function ClientModel_sendBye(name, socket) {
106 var sendTextMsg = [], messageObj, messageObjToString, i, len;
107 name = encodeURIComponent(name);
108 messageObj = {name: name, text: '', ping: false, bye: true};
109 messageObjToString = JSON.stringify(messageObj);
110 len = messageObjToString.length;
111 for (i = 0; i < len; i += 1) {
112 sendTextMsg[i] = messageObjToString.charCodeAt(i);
115 if (socket !== null && socket.state === "OPEN") {
116 socket.writeData(sendTextMsg);
119 console.error('sendBye: ' + error.message);
123 destroyBonding: function ClientModel_destroyBonding(device, restartCallback, showStartButtonsCallback) {
124 if (device !== null) {
125 if (device.isBonded) {
126 this.client.adapter.destroyBonding(device.address, function () {
129 }, function (error) { console.error('ClientModel_destroyBonding: ' + error); });
135 this.stopServerSearching();
136 showStartButtonsCallback();