bluetoothSerial.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*global cordova*/
  2. module.exports = {
  3. connect: function (macAddress, pin, success, failure) {
  4. cordova.exec(success, failure, "BluetoothSerial", "connect", [macAddress, pin || ""]);
  5. },
  6. // Android only - see http://goo.gl/1mFjZY
  7. connectInsecure: function (macAddress, pin, success, failure) {
  8. cordova.exec(success, failure, "BluetoothSerial", "connectInsecure", [macAddress, pin || ""]);
  9. },
  10. disconnect: function (success, failure) {
  11. cordova.exec(success, failure, "BluetoothSerial", "disconnect", []);
  12. },
  13. // list bound devices
  14. list: function (success, failure) {
  15. cordova.exec(success, failure, "BluetoothSerial", "list", []);
  16. },
  17. isEnabled: function (success, failure) {
  18. cordova.exec(success, failure, "BluetoothSerial", "isEnabled", []);
  19. },
  20. isConnected: function (success, failure) {
  21. cordova.exec(success, failure, "BluetoothSerial", "isConnected", []);
  22. },
  23. // the number of bytes of data available to read is passed to the success function
  24. available: function (success, failure) {
  25. cordova.exec(success, failure, "BluetoothSerial", "available", []);
  26. },
  27. // read all the data in the buffer
  28. read: function (success, failure) {
  29. cordova.exec(success, failure, "BluetoothSerial", "read", []);
  30. },
  31. // reads the data in the buffer up to and including the delimiter
  32. readUntil: function (delimiter, success, failure) {
  33. cordova.exec(success, failure, "BluetoothSerial", "readUntil", [delimiter]);
  34. },
  35. // writes data to the bluetooth serial port
  36. // data can be an ArrayBuffer, string, integer array, or Uint8Array
  37. write: function (data, success, failure) {
  38. // convert to ArrayBuffer
  39. if (typeof data === 'string') {
  40. data = stringToArrayBuffer(data);
  41. } else if (data instanceof Array) {
  42. // assuming array of interger
  43. data = new Uint8Array(data).buffer;
  44. } else if (data instanceof Uint8Array) {
  45. data = data.buffer;
  46. }
  47. cordova.exec(success, failure, "BluetoothSerial", "write", [data]);
  48. },
  49. // calls the success callback when new data is available
  50. subscribe: function (delimiter, success, failure) {
  51. cordova.exec(success, failure, "BluetoothSerial", "subscribe", [delimiter]);
  52. },
  53. // removes data subscription
  54. unsubscribe: function (success, failure) {
  55. cordova.exec(success, failure, "BluetoothSerial", "unsubscribe", []);
  56. },
  57. // calls the success callback when new data is available with an ArrayBuffer
  58. subscribeRawData: function (success, failure) {
  59. successWrapper = function (data) {
  60. // Windows Phone flattens an array of one into a number which
  61. // breaks the API. Stuff it back into an ArrayBuffer.
  62. if (typeof data === 'number') {
  63. var a = new Uint8Array(1);
  64. a[0] = data;
  65. data = a.buffer;
  66. }
  67. success(data);
  68. };
  69. cordova.exec(successWrapper, failure, "BluetoothSerial", "subscribeRaw", []);
  70. },
  71. // removes data subscription
  72. unsubscribeRawData: function (success, failure) {
  73. cordova.exec(success, failure, "BluetoothSerial", "unsubscribeRaw", []);
  74. },
  75. // clears the data buffer
  76. clear: function (success, failure) {
  77. cordova.exec(success, failure, "BluetoothSerial", "clear", []);
  78. },
  79. // reads the RSSI of the *connected* peripherial
  80. readRSSI: function (success, failure) {
  81. cordova.exec(success, failure, "BluetoothSerial", "readRSSI", []);
  82. },
  83. showBluetoothSettings: function (success, failure) {
  84. cordova.exec(success, failure, "BluetoothSerial", "showBluetoothSettings", []);
  85. },
  86. enable: function (success, failure) {
  87. cordova.exec(success, failure, "BluetoothSerial", "enable", []);
  88. },
  89. discoverUnpaired: function (success, failure) {
  90. cordova.exec(success, failure, "BluetoothSerial", "discoverUnpaired", []);
  91. },
  92. setDeviceDiscoveredListener: function (notify) {
  93. if (typeof notify != 'function')
  94. throw 'BluetoothSerial.setDeviceDiscoveredListener: Callback not a function';
  95. cordova.exec(notify, null, "BluetoothSerial", "setDeviceDiscoveredListener", []);
  96. },
  97. clearDeviceDiscoveredListener: function () {
  98. cordova.exec(null, null, "BluetoothSerial", "clearDeviceDiscoveredListener", []);
  99. },
  100. setName: function (newName) {
  101. cordova.exec(null, null, "BluetoothSerial", "setName", [newName]);
  102. },
  103. setDiscoverable: function (discoverableDuration) {
  104. cordova.exec(null, null, "BluetoothSerial", "setDiscoverable", [discoverableDuration]);
  105. }
  106. };
  107. var stringToArrayBuffer = function (str) {
  108. var ret = new Uint8Array(str.length);
  109. for (var i = 0; i < str.length; i++) {
  110. ret[i] = str.charCodeAt(i);
  111. }
  112. return ret.buffer;
  113. };