| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*global cordova*/
- module.exports = {
- connect: function (macAddress, pin, success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "connect", [macAddress, pin || ""]);
- },
- // Android only - see http://goo.gl/1mFjZY
- connectInsecure: function (macAddress, pin, success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "connectInsecure", [macAddress, pin || ""]);
- },
- disconnect: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "disconnect", []);
- },
- // list bound devices
- list: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "list", []);
- },
- isEnabled: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "isEnabled", []);
- },
- isConnected: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "isConnected", []);
- },
- // the number of bytes of data available to read is passed to the success function
- available: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "available", []);
- },
- // read all the data in the buffer
- read: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "read", []);
- },
- // reads the data in the buffer up to and including the delimiter
- readUntil: function (delimiter, success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "readUntil", [delimiter]);
- },
- // writes data to the bluetooth serial port
- // data can be an ArrayBuffer, string, integer array, or Uint8Array
- write: function (data, success, failure) {
- // convert to ArrayBuffer
- if (typeof data === 'string') {
- data = stringToArrayBuffer(data);
- } else if (data instanceof Array) {
- // assuming array of interger
- data = new Uint8Array(data).buffer;
- } else if (data instanceof Uint8Array) {
- data = data.buffer;
- }
- cordova.exec(success, failure, "BluetoothSerial", "write", [data]);
- },
- // calls the success callback when new data is available
- subscribe: function (delimiter, success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "subscribe", [delimiter]);
- },
- // removes data subscription
- unsubscribe: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "unsubscribe", []);
- },
- // calls the success callback when new data is available with an ArrayBuffer
- subscribeRawData: function (success, failure) {
- successWrapper = function (data) {
- // Windows Phone flattens an array of one into a number which
- // breaks the API. Stuff it back into an ArrayBuffer.
- if (typeof data === 'number') {
- var a = new Uint8Array(1);
- a[0] = data;
- data = a.buffer;
- }
- success(data);
- };
- cordova.exec(successWrapper, failure, "BluetoothSerial", "subscribeRaw", []);
- },
- // removes data subscription
- unsubscribeRawData: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "unsubscribeRaw", []);
- },
- // clears the data buffer
- clear: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "clear", []);
- },
- // reads the RSSI of the *connected* peripherial
- readRSSI: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "readRSSI", []);
- },
- showBluetoothSettings: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "showBluetoothSettings", []);
- },
- enable: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "enable", []);
- },
- discoverUnpaired: function (success, failure) {
- cordova.exec(success, failure, "BluetoothSerial", "discoverUnpaired", []);
- },
- setDeviceDiscoveredListener: function (notify) {
- if (typeof notify != 'function')
- throw 'BluetoothSerial.setDeviceDiscoveredListener: Callback not a function';
- cordova.exec(notify, null, "BluetoothSerial", "setDeviceDiscoveredListener", []);
- },
- clearDeviceDiscoveredListener: function () {
- cordova.exec(null, null, "BluetoothSerial", "clearDeviceDiscoveredListener", []);
- },
- setName: function (newName) {
- cordova.exec(null, null, "BluetoothSerial", "setName", [newName]);
- },
- setDiscoverable: function (discoverableDuration) {
- cordova.exec(null, null, "BluetoothSerial", "setDiscoverable", [discoverableDuration]);
- }
- };
- var stringToArrayBuffer = function (str) {
- var ret = new Uint8Array(str.length);
- for (var i = 0; i < str.length; i++) {
- ret[i] = str.charCodeAt(i);
- }
- return ret.buffer;
- };
|