Comparing version 0.1.4 to 0.1.5
@@ -32,8 +32,3 @@ /* @license | ||
// AMD. Register as an anonymous module. | ||
if (root.navigator.bluetooth) { | ||
// Return existing web-bluetooth | ||
define(root.navigator.bluetooth); | ||
} else { | ||
define(['es6-promise', 'es6-map', 'bluetooth.helpers'], factory); | ||
} | ||
define(['es6-promise', 'es6-map', 'bluetooth.helpers'], factory); | ||
} else if (typeof exports === 'object') { | ||
@@ -45,3 +40,3 @@ // Node. Does not work with strict CommonJS | ||
// Assume Promise exists or has been poly-filled | ||
root.bleat = root.navigator.bluetooth || factory(root.Promise, root.Map, root.bleatHelpers); | ||
root.bleat = factory(root.Promise, root.Map, root.bleatHelpers); | ||
} | ||
@@ -48,0 +43,0 @@ }(this, function(Promise, Map, helpers) { |
@@ -53,2 +53,2 @@ /* @license | ||
*/ | ||
function(a,b){"function"==typeof define&&define.amd?a.navigator.bluetooth?define(a.navigator.bluetooth):define(["es6-promise","es6-map","bluetooth.helpers"],b):"object"==typeof exports?module.exports=b(Promise,Map,require("./bluetooth.helpers")):a.bleat=a.navigator.bluetooth||b(a.Promise,a.Map,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(a,b){return function(c){a(b+": "+c)}}function e(a,b){b&&Object.keys(b).forEach(function(c){b[c]&&a.hasOwnProperty(c)&&("[object Object]"===Object.prototype.toString.call(a[c])?e(a[c],b[c]):"[object Map]"===Object.prototype.toString.call(a[c])&&"[object Object]"===Object.prototype.toString.call(b[c])?Object.keys(b[c]).forEach(function(d){a[c].set(d,b[c][d])}):a[c]=b[c])})}function f(a){return function(b,c,d){a.indexOf(b)<0||(this.__events||(this.__events={}),this.__events[b]||(this.__events[b]=[]),this.__events[b].push(c))}}function g(a,b,c){if(this.__events&&this.__events[a]){var d=this.__events[a].indexOf(b);d>=0&&this.__events[a].splice(d,1),0===this.__events[a].length&&delete this.__events[a],0===Object.keys(this.__events).length&&delete this.__events}}function h(a){this.__events&&this.__events[a.type]&&(a.target=this,this.__events[a.type].forEach(function(b){"function"==typeof b&&b(a)}))}function i(a,b,d){var e=!1;return a.filters.forEach(function(a){if(!a.name||a.name===b.name){if(a.namePrefix){if(a.namePrefix.length>b.name.length)return;if(a.namePrefix!==b.name.substr(0,a.namePrefix.length))return}if(a.services){var f=a.services.map(c.getServiceUUID);if(!f.every(function(a){return b.uuids.indexOf(a)>-1}))return;d=d.concat(f)}e=!0}}),!!e&&b}function j(b){return new a(function(a,e){if(null!==o)return e("requestDevice error: request in progress");if(!b.acceptAllDevices&&!b.deviceFound){if(!b.filters||0===b.filters.length)return e(new TypeError("requestDevice error: no filters specified"));if(b.filters.some(function(a){return 0===Object.keys(a).length}))return e(new TypeError("requestDevice error: empty filter specified"));if(b.filters.some(function(a){return void 0!==a.namePrefix&&""===a.namePrefix}))return e(new TypeError("requestDevice error: empty namePrefix specified"))}var f=[];b.filters&&b.filters.forEach(function(a){a.services&&(f=f.concat(a.services.map(c.getServiceUUID)))}),f=f.filter(function(a,b,c){return c.indexOf(a)===b});var g=!1;m.startScan(f,function(d){function e(b){k().then(function(){a(b)})}function f(){e(j)}var h=[];if(b.filters&&(d=i(b,d,h)),d){g=!0,b.optionalServices&&(h=h.concat(b.optionalServices.map(c.getServiceUUID))),d._allowedServices=h.filter(function(a,b,c){return c.indexOf(a)===b});var j=new p(d);b.deviceFound&&!b.deviceFound(j,f)||e(j)}},function(){o=setTimeout(function(){k().then(function(){g||e("requestDevice error: no devices found")})},b.scanTime||l)},d(e,"requestDevice error"))})}function k(){return new a(function(a,b){o&&(clearTimeout(o),o=null,m.stopScan()),a()})}var l=10240,m=null,n={},o=null,p=function(a){this._handle=null,this._allowedServices=[],this.id="unknown",this.name=null,this.adData={appearance:null,txPower:null,rssi:null,manufacturerData:new b,serviceData:new b},this.gatt=new q,this.gatt.device=this,this.uuids=[],e(this,a)};p.prototype.addEventListener=f(["gattserverdisconnected"]),p.prototype.removeEventListener=g,p.prototype.dispatchEvent=h;var q=function(){this._services=null,this.device=null,this.connected=!1};q.prototype.connect=function(){return new a(function(a,b){if(this.connected)return b("connect error: device already connected");m.connect(this.device._handle,function(){this.connected=!0,a(this)}.bind(this),function(){this._services=null,this.connected=!1,this.device.dispatchEvent({type:"gattserverdisconnected",bubbles:!0})}.bind(this),d(b,"connect error"))}.bind(this))},q.prototype.disconnect=function(){m.disconnect(this.device._handle),this.connected=!1},q.prototype.getPrimaryService=function(b){return new a(function(a,c){return this.connected?b?void this.getPrimaryServices(b).then(function(b){if(1!==b.length)return c("getPrimaryService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getPrimaryService error: no service specified"):c("getPrimaryService error: device not connected")}.bind(this))},q.prototype.getPrimaryServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getPrimaryServices error: service not found");a(d)}return this.connected?this._services?f.call(this):void m.discoverServices(this.device._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getPrimaryServices error")):e("getPrimaryServices error: device not connected")}.bind(this))};var r=function(a){this._handle=null,this._services=null,this._characteristics=null,this.device=null,this.uuid=null,this.isPrimary=!1,e(this,a),this.dispatchEvent({type:"serviceadded",bubbles:!0})};r.prototype.getCharacteristic=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getCharacteristics(b).then(function(b){if(1!==b.length)return c("getCharacteristic error: characteristic not found");a(b[0])}).catch(function(a){c(a)}):c("getCharacteristic error: no characteristic specified"):c("getCharacteristic error: device not connected")}.bind(this))},r.prototype.getCharacteristics=function(b){return new a(function(a,e){function f(){if(!b)return a(this._characteristics);var d=this._characteristics.filter(function(a){return a.uuid===c.getCharacteristicUUID(b)});if(1!==d.length)return e("getCharacteristics error: characteristic not found");a(d)}return this.device.gatt.connected?this._characteristics?f.call(this):void m.discoverCharacteristics(this._handle,[],function(a){this._characteristics=a.map(function(a){return a.service=this,new s(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getCharacteristics error")):e("getCharacteristics error: device not connected")}.bind(this))},r.prototype.getIncludedService=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getIncludedServices(b).then(function(b){if(1!==b.length)return c("getIncludedService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getIncludedService error: no service specified"):c("getIncludedService error: device not connected")}.bind(this))},r.prototype.getIncludedServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getIncludedServices error: service not found");a(d)}return this.device.gatt.connected?this._services?f.call(this):void m.discoverIncludedServices(this._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getIncludedServices error")):e("getIncludedServices error: device not connected")}.bind(this))},r.prototype.addEventListener=f(["serviceadded","servicechanged","serviceremoved"]),r.prototype.removeEventListener=g,r.prototype.dispatchEvent=h;var s=function(a){this._handle=null,this._descriptors=null,this.service=null,this.uuid=null,this.properties={broadcast:!1,read:!1,writeWithoutResponse:!1,write:!1,notify:!1,indicate:!1,authenticatedSignedWrites:!1,reliableWrite:!1,writableAuxiliaries:!1},this.value=null,e(this,a)};s.prototype.getDescriptor=function(b){return new a(function(a,c){return this.service.device.gatt.connected?b?void this.getDescriptors(b).then(function(b){if(1!==b.length)return c("getDescriptor error: descriptor not found");a(b[0])}).catch(function(a){c(a)}):c("getDescriptor error: no descriptor specified"):c("getDescriptor error: device not connected")}.bind(this))},s.prototype.getDescriptors=function(b){return new a(function(a,e){function f(){if(!b)return a(this._descriptors);var d=this._descriptors.filter(function(a){return a.uuid===c.getDescriptorUUID(b)});if(1!==d.length)return e("getDescriptors error: descriptor not found");a(d)}return this.service.device.gatt.connected?this._descriptors?f.call(this):void m.discoverDescriptors(this._handle,[],function(a){this._descriptors=a.map(function(a){return a.characteristic=this,new t(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getDescriptors error")):e("getDescriptors error: device not connected")}.bind(this))},s.prototype.readValue=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("readValue error: device not connected");m.readCharacteristic(this._handle,function(b){this.value=b,a(b),this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),d(b,"readValue error"))}.bind(this))},s.prototype.writeValue=function(b){return new a(function(a,c){if(!this.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeCharacteristic(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},s.prototype.startNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("startNotifications error: device not connected");m.enableNotify(this._handle,function(a){this.value=a,this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),function(){a(this)}.bind(this),d(b,"startNotifications error"))}.bind(this))},s.prototype.stopNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("stopNotifications error: device not connected");m.disableNotify(this._handle,function(){a(this)}.bind(this),d(b,"stopNotifications error"))}.bind(this))},s.prototype.addEventListener=f(["characteristicvaluechanged"]),s.prototype.removeEventListener=g,s.prototype.dispatchEvent=h;var t=function(a){this._handle=null,this.characteristic=null,this.uuid=null,this.value=null,e(this,a)};return t.prototype.readValue=function(){return new a(function(a,b){if(!this.characteristic.service.device.gatt.connected)return b("readValue error: device not connected");m.readDescriptor(this._handle,function(b){this.value=b,a(b)}.bind(this),d(b,"readValue error"))}.bind(this))},t.prototype.writeValue=function(b){return new a(function(a,c){if(!this.characteristic.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeDescriptor(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},{_addAdapter:function(a,b){n[a]=b,m=b},requestDevice:j,cancelRequest:k}}),function(a,b){"function"==typeof define&&define.amd?define(["bleat","bluetooth.helpers"],b.bind(this,a)):"object"==typeof exports?module.exports=function(c){return b(a,c,require("./bluetooth.helpers"))}:b(a,a.bleat,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(b){a.evothings&&evothings.ble?b():document.addEventListener("deviceready",b)}function e(a){var b=x[a];b&&(evothings.ble.close(b),delete x[a],f(b,y),f(b,z,!0),f(b,A))}function f(a,b,c){for(var d in b)a===b[d]&&(delete b[d],c&&B[d]&&delete B[d])}function g(a,b){var c=x[a];return c||(b&&b("Device does not exist for device id: "+a),null)}function h(a,b){var c=y[a];return c||(b&&b("Device does not exist for service handle: "+a),null)}function i(a,b){var c=z[a];return c||(b&&b("Device does not exist for characteristic handle: "+a),null)}function j(a,b,c,d){function e(b){evothings.ble.writeDescriptor(a,b,new Uint8Array([1,0]),function(){c()},function(a){d(a)})}function f(){w.discoverDescriptors(b,"00002902-0000-1000-8000-00805f9b34fb",function(a){var c=B[b];c?e(c):d("Could not find CCCD for characteristic: "+b)},function(a){d(a)})}var g=B[b];g?e(g):f()}function k(a){var b={};return b._handle=a.address,b.id=a.address,b.name=a.name,b.uuids=[],b.adData={},b.adData.rssi=a.rssi,b.adData.txPower=null,b.adData.serviceData={},b.adData.manufacturerData=null,a.advertisementData?l(a,b):a.scanRecord&&m(a,b),b}function l(a,b){if(a.advertisementData){if(a.advertisementData.kCBAdvDataLocalName&&(b.name=a.advertisementData.kCBAdvDataLocalName),a.advertisementData.kCBAdvDataTxPowerLevel&&(b.adData.txPower=a.advertisementData.kCBAdvDataTxPowerLevel),a.advertisementData.kCBAdvDataServiceUUIDs&&a.advertisementData.kCBAdvDataServiceUUIDs.forEach(function(a){b.uuids.push(c.getCanonicalUUID(a))}),a.advertisementData.kCBAdvDataServiceData)for(var d in a.advertisementData.kCBAdvDataServiceData){var e=a.advertisementData.kCBAdvDataServiceData[d];b.adData.serviceData[c.getCanonicalUUID(d)]=t(o(e))}a.advertisementData.kCBAdvDataManufacturerData&&(b.adData.manufacturerDataRaw=a.advertisementData.kCBAdvDataManufacturerData)}}function m(a,b){for(var d=o(a.scanRecord),e=0;e<d.length;){var f=d[e++];if(0===f)break;f-=1;var g,h=d[e++];if(8===h||9===h)b.name=evothings.ble.fromUtf8(new Uint8Array(d.buffer,e,f)).replace("\0","");else if(10===h)b.adData.txPower=p(d,e);else if(2===h||3===h)for(g=0;g<f;g+=2)b.uuids.push(c.getCanonicalUUID(q(d,e+g).toString(16)));else if(4===h||5===h)for(g=0;g<f;g+=4)b.uuids.push(c.getCanonicalUUID(r(d,e+g).toString(16)));else if(6===h||7===h)for(g=0;g<f;g+=16)b.uuids.push(c.getCanonicalUUID(s(d,e+g)));e+=f}}function n(a){return a>64&&a<91?a-65:a>96&&a<123?a-71:a>47&&a<58?a+4:43===a?62:47===a?63:0}function o(a,b){for(var c,d,e=a.replace(/[^A-Za-z0-9\+\/]/g,""),f=e.length,g=b?Math.ceil((3*f+1>>2)/b)*b:3*f+1>>2,h=new Uint8Array(g),i=0,j=0,k=0;k<f;k++)if(d=3&k,i|=n(e.charCodeAt(k))<<18-6*d,3===d||f-k==1){for(c=0;c<3&&j<g;c++,j++)h[j]=i>>>(16>>>c&24)&255;i=0}return h}function p(a,b){var c=a[b];return 128&c&&(c-=256),c}function q(a,b){return(a[b+1]<<8)+a[b]}function r(a,b){return(a[b+3]<<24)+(a[b+2]<<16)+(a[b+1]<<8)+a[b]}function s(a,b){for(var c="",d=0;d<16;d++)c+=("00"+a[b+d].toString(16)).slice(-2);return c}function t(a){var b=new Uint8Array(a).buffer;return new DataView(b)}function u(){return a.cordova?a.cordova.platformId:null}function v(){return"ios"===u()}if(b._addAdapter){var w={},x={},y={},z={},A={},B={};b._addAdapter("evothings",w),w.startScan=function(a,b,c,e){d(function(){evothings.ble.stopScan(),evothings.ble.startScan(a,function(a){b&&b(k(a))},function(a){e&&e(a)}),c&&c()})},w.stopScan=function(a){d(function(){evothings.ble.stopScan()})},w.connect=function(a,b,c,d){if(x[a])return void(d&&d("device already connected"));evothings.ble.connect(a,function(d){2===d.state&&b?(x[a]=d.deviceHandle,b()):0===d.state&&c&&(e(a),c())},function(a){d&&d(a)})},w.disconnect=function(a,b){e(a)},w.discoverServices=function(a,b,d,e){var f=g(a,e);f&&evothings.ble.services(f,function(a){var e=[];a.forEach(function(a){var d=c.getCanonicalUUID(a.uuid);(!b||0===b.length||b.indexOf(d)>=0)&&(y[a.handle]=f,e.push({_handle:a.handle,uuid:d,primary:!0}))}),d&&d(e)},function(a){e&&e(a)})},w.discoverIncludedServices=function(a,b,c,d){c([])},w.discoverCharacteristics=function(a,b,d,e){var f=h(a,e);f&&evothings.ble.characteristics(f,a,function(a){var e=[];a.forEach(function(a){var d=c.getCanonicalUUID(a.uuid);(!b||0===b.length||b.indexOf(d)>=0)&&(z[a.handle]=f,e.push({_handle:a.handle,uuid:d,properties:{broadcast:1&a.property,read:2&a.property,writeWithoutResponse:4&a.property&&1&a.writeType,write:8&a.property,notify:16&a.property,indicate:32&a.property,authenticatedSignedWrites:64&a.property&&4&a.writeType,reliableWrite:!1,writableAuxiliaries:!1}}))}),d&&d(e)},function(a){e&&e(a)})},w.discoverDescriptors=function(a,b,d,e){var f=i(a,e);f&&evothings.ble.descriptors(f,a,function(e){var g=[];e.forEach(function(d){var e=c.getCanonicalUUID(d.uuid);"00002902-0000-1000-8000-00805f9b34fb"===e&&(B[a]=d.handle),(!b||0===b.length||b.indexOf(e)>=0)&&(A[d.handle]=f,g.push({_handle:d.handle,uuid:e}))}),d&&d(g)},function(a){e&&e(a)})},w.readCharacteristic=function(a,b,c){var d=i(a,c);d&&evothings.ble.readCharacteristic(d,a,function(a){b&&b(t(a))},function(a){c&&c(a)})},w.writeCharacteristic=function(a,b,c,d){var e=i(a,d);e&&evothings.ble.writeCharacteristic(e,a,b,function(){c&&c()},function(a){d&&d(a)})},w.enableNotify=function(a,b,c,d){function e(){evothings.ble.enableNotification(f,a,function(a){b&&b(t(a))},function(a){d&&d(a)}),c&&c()}var f=i(a,d);f&&j(f,a,e,function(a){d&&d(a)})},w.disableNotify=function(a,b,c){var d=i(a,c);d&&(evothings.ble.disableNotification(d,a,function(){b&&b()},function(a){c&&c(a)}),v()&&setTimeout(b,0))},w.readDescriptor=function(a,b,c){var d=getDeviceHandleFromDescriptorHandle(a,c);d&&evothings.ble.readDescriptor(d,a,function(a){b&&b(t(a))},function(a){c&&c(a)})},w.writeDescriptor=function(a,b,c,d){var e=getDeviceHandleFromDescriptorHandle(a,d);e&&evothings.ble.writeDescriptor(e,a,b,function(){c&&c()},function(a){d&&d(a)})}}}); | ||
function(a,b){"function"==typeof define&&define.amd?define(["es6-promise","es6-map","bluetooth.helpers"],b):"object"==typeof exports?module.exports=b(Promise,Map,require("./bluetooth.helpers")):a.bleat=b(a.Promise,a.Map,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(a,b){return function(c){a(b+": "+c)}}function e(a,b){b&&Object.keys(b).forEach(function(c){b[c]&&a.hasOwnProperty(c)&&("[object Object]"===Object.prototype.toString.call(a[c])?e(a[c],b[c]):"[object Map]"===Object.prototype.toString.call(a[c])&&"[object Object]"===Object.prototype.toString.call(b[c])?Object.keys(b[c]).forEach(function(d){a[c].set(d,b[c][d])}):a[c]=b[c])})}function f(a){return function(b,c,d){a.indexOf(b)<0||(this.__events||(this.__events={}),this.__events[b]||(this.__events[b]=[]),this.__events[b].push(c))}}function g(a,b,c){if(this.__events&&this.__events[a]){var d=this.__events[a].indexOf(b);d>=0&&this.__events[a].splice(d,1),0===this.__events[a].length&&delete this.__events[a],0===Object.keys(this.__events).length&&delete this.__events}}function h(a){this.__events&&this.__events[a.type]&&(a.target=this,this.__events[a.type].forEach(function(b){"function"==typeof b&&b(a)}))}function i(a,b,d){var e=!1;return a.filters.forEach(function(a){if(!a.name||a.name===b.name){if(a.namePrefix){if(a.namePrefix.length>b.name.length)return;if(a.namePrefix!==b.name.substr(0,a.namePrefix.length))return}if(a.services){var f=a.services.map(c.getServiceUUID);if(!f.every(function(a){return b.uuids.indexOf(a)>-1}))return;d=d.concat(f)}e=!0}}),!!e&&b}function j(b){return new a(function(a,e){if(null!==o)return e("requestDevice error: request in progress");if(!b.acceptAllDevices&&!b.deviceFound){if(!b.filters||0===b.filters.length)return e(new TypeError("requestDevice error: no filters specified"));if(b.filters.some(function(a){return 0===Object.keys(a).length}))return e(new TypeError("requestDevice error: empty filter specified"));if(b.filters.some(function(a){return void 0!==a.namePrefix&&""===a.namePrefix}))return e(new TypeError("requestDevice error: empty namePrefix specified"))}var f=[];b.filters&&b.filters.forEach(function(a){a.services&&(f=f.concat(a.services.map(c.getServiceUUID)))}),f=f.filter(function(a,b,c){return c.indexOf(a)===b});var g=!1;m.startScan(f,function(d){function e(b){k().then(function(){a(b)})}function f(){e(j)}var h=[];if(b.filters&&(d=i(b,d,h)),d){g=!0,b.optionalServices&&(h=h.concat(b.optionalServices.map(c.getServiceUUID))),d._allowedServices=h.filter(function(a,b,c){return c.indexOf(a)===b});var j=new p(d);b.deviceFound&&!b.deviceFound(j,f)||e(j)}},function(){o=setTimeout(function(){k().then(function(){g||e("requestDevice error: no devices found")})},b.scanTime||l)},d(e,"requestDevice error"))})}function k(){return new a(function(a,b){o&&(clearTimeout(o),o=null,m.stopScan()),a()})}var l=10240,m=null,n={},o=null,p=function(a){this._handle=null,this._allowedServices=[],this.id="unknown",this.name=null,this.adData={appearance:null,txPower:null,rssi:null,manufacturerData:new b,serviceData:new b},this.gatt=new q,this.gatt.device=this,this.uuids=[],e(this,a)};p.prototype.addEventListener=f(["gattserverdisconnected"]),p.prototype.removeEventListener=g,p.prototype.dispatchEvent=h;var q=function(){this._services=null,this.device=null,this.connected=!1};q.prototype.connect=function(){return new a(function(a,b){if(this.connected)return b("connect error: device already connected");m.connect(this.device._handle,function(){this.connected=!0,a(this)}.bind(this),function(){this._services=null,this.connected=!1,this.device.dispatchEvent({type:"gattserverdisconnected",bubbles:!0})}.bind(this),d(b,"connect error"))}.bind(this))},q.prototype.disconnect=function(){m.disconnect(this.device._handle),this.connected=!1},q.prototype.getPrimaryService=function(b){return new a(function(a,c){return this.connected?b?void this.getPrimaryServices(b).then(function(b){if(1!==b.length)return c("getPrimaryService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getPrimaryService error: no service specified"):c("getPrimaryService error: device not connected")}.bind(this))},q.prototype.getPrimaryServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getPrimaryServices error: service not found");a(d)}return this.connected?this._services?f.call(this):void m.discoverServices(this.device._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getPrimaryServices error")):e("getPrimaryServices error: device not connected")}.bind(this))};var r=function(a){this._handle=null,this._services=null,this._characteristics=null,this.device=null,this.uuid=null,this.isPrimary=!1,e(this,a),this.dispatchEvent({type:"serviceadded",bubbles:!0})};r.prototype.getCharacteristic=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getCharacteristics(b).then(function(b){if(1!==b.length)return c("getCharacteristic error: characteristic not found");a(b[0])}).catch(function(a){c(a)}):c("getCharacteristic error: no characteristic specified"):c("getCharacteristic error: device not connected")}.bind(this))},r.prototype.getCharacteristics=function(b){return new a(function(a,e){function f(){if(!b)return a(this._characteristics);var d=this._characteristics.filter(function(a){return a.uuid===c.getCharacteristicUUID(b)});if(1!==d.length)return e("getCharacteristics error: characteristic not found");a(d)}return this.device.gatt.connected?this._characteristics?f.call(this):void m.discoverCharacteristics(this._handle,[],function(a){this._characteristics=a.map(function(a){return a.service=this,new s(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getCharacteristics error")):e("getCharacteristics error: device not connected")}.bind(this))},r.prototype.getIncludedService=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getIncludedServices(b).then(function(b){if(1!==b.length)return c("getIncludedService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getIncludedService error: no service specified"):c("getIncludedService error: device not connected")}.bind(this))},r.prototype.getIncludedServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getIncludedServices error: service not found");a(d)}return this.device.gatt.connected?this._services?f.call(this):void m.discoverIncludedServices(this._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getIncludedServices error")):e("getIncludedServices error: device not connected")}.bind(this))},r.prototype.addEventListener=f(["serviceadded","servicechanged","serviceremoved"]),r.prototype.removeEventListener=g,r.prototype.dispatchEvent=h;var s=function(a){this._handle=null,this._descriptors=null,this.service=null,this.uuid=null,this.properties={broadcast:!1,read:!1,writeWithoutResponse:!1,write:!1,notify:!1,indicate:!1,authenticatedSignedWrites:!1,reliableWrite:!1,writableAuxiliaries:!1},this.value=null,e(this,a)};s.prototype.getDescriptor=function(b){return new a(function(a,c){return this.service.device.gatt.connected?b?void this.getDescriptors(b).then(function(b){if(1!==b.length)return c("getDescriptor error: descriptor not found");a(b[0])}).catch(function(a){c(a)}):c("getDescriptor error: no descriptor specified"):c("getDescriptor error: device not connected")}.bind(this))},s.prototype.getDescriptors=function(b){return new a(function(a,e){function f(){if(!b)return a(this._descriptors);var d=this._descriptors.filter(function(a){return a.uuid===c.getDescriptorUUID(b)});if(1!==d.length)return e("getDescriptors error: descriptor not found");a(d)}return this.service.device.gatt.connected?this._descriptors?f.call(this):void m.discoverDescriptors(this._handle,[],function(a){this._descriptors=a.map(function(a){return a.characteristic=this,new t(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getDescriptors error")):e("getDescriptors error: device not connected")}.bind(this))},s.prototype.readValue=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("readValue error: device not connected");m.readCharacteristic(this._handle,function(b){this.value=b,a(b),this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),d(b,"readValue error"))}.bind(this))},s.prototype.writeValue=function(b){return new a(function(a,c){if(!this.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeCharacteristic(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},s.prototype.startNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("startNotifications error: device not connected");m.enableNotify(this._handle,function(a){this.value=a,this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),function(){a(this)}.bind(this),d(b,"startNotifications error"))}.bind(this))},s.prototype.stopNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("stopNotifications error: device not connected");m.disableNotify(this._handle,function(){a(this)}.bind(this),d(b,"stopNotifications error"))}.bind(this))},s.prototype.addEventListener=f(["characteristicvaluechanged"]),s.prototype.removeEventListener=g,s.prototype.dispatchEvent=h;var t=function(a){this._handle=null,this.characteristic=null,this.uuid=null,this.value=null,e(this,a)};return t.prototype.readValue=function(){return new a(function(a,b){if(!this.characteristic.service.device.gatt.connected)return b("readValue error: device not connected");m.readDescriptor(this._handle,function(b){this.value=b,a(b)}.bind(this),d(b,"readValue error"))}.bind(this))},t.prototype.writeValue=function(b){return new a(function(a,c){if(!this.characteristic.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeDescriptor(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},{_addAdapter:function(a,b){n[a]=b,m=b},requestDevice:j,cancelRequest:k}}),function(a,b){"function"==typeof define&&define.amd?define(["bleat","bluetooth.helpers"],b.bind(this,a)):"object"==typeof exports?module.exports=function(c){return b(a,c,require("./bluetooth.helpers"))}:b(a,a.bleat,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(b){a.evothings&&evothings.ble?b():document.addEventListener("deviceready",b)}function e(a){var b=x[a];b&&(evothings.ble.close(b),delete x[a],f(b,y),f(b,z,!0),f(b,A))}function f(a,b,c){for(var d in b)a===b[d]&&(delete b[d],c&&B[d]&&delete B[d])}function g(a,b){var c=x[a];return c||(b&&b("Device does not exist for device id: "+a),null)}function h(a,b){var c=y[a];return c||(b&&b("Device does not exist for service handle: "+a),null)}function i(a,b){var c=z[a];return c||(b&&b("Device does not exist for characteristic handle: "+a),null)}function j(a,b,c,d){function e(b){evothings.ble.writeDescriptor(a,b,new Uint8Array([1,0]),function(){c()},function(a){d(a)})}function f(){w.discoverDescriptors(b,"00002902-0000-1000-8000-00805f9b34fb",function(a){var c=B[b];c?e(c):d("Could not find CCCD for characteristic: "+b)},function(a){d(a)})}var g=B[b];g?e(g):f()}function k(a){var b={};return b._handle=a.address,b.id=a.address,b.name=a.name,b.uuids=[],b.adData={},b.adData.rssi=a.rssi,b.adData.txPower=null,b.adData.serviceData={},b.adData.manufacturerData=null,a.advertisementData?l(a,b):a.scanRecord&&m(a,b),b}function l(a,b){if(a.advertisementData){if(a.advertisementData.kCBAdvDataLocalName&&(b.name=a.advertisementData.kCBAdvDataLocalName),a.advertisementData.kCBAdvDataTxPowerLevel&&(b.adData.txPower=a.advertisementData.kCBAdvDataTxPowerLevel),a.advertisementData.kCBAdvDataServiceUUIDs&&a.advertisementData.kCBAdvDataServiceUUIDs.forEach(function(a){b.uuids.push(c.getCanonicalUUID(a))}),a.advertisementData.kCBAdvDataServiceData)for(var d in a.advertisementData.kCBAdvDataServiceData){var e=a.advertisementData.kCBAdvDataServiceData[d];b.adData.serviceData[c.getCanonicalUUID(d)]=t(o(e))}a.advertisementData.kCBAdvDataManufacturerData&&(b.adData.manufacturerDataRaw=a.advertisementData.kCBAdvDataManufacturerData)}}function m(a,b){for(var d=o(a.scanRecord),e=0;e<d.length;){var f=d[e++];if(0===f)break;f-=1;var g,h=d[e++];if(8===h||9===h)b.name=evothings.ble.fromUtf8(new Uint8Array(d.buffer,e,f)).replace("\0","");else if(10===h)b.adData.txPower=p(d,e);else if(2===h||3===h)for(g=0;g<f;g+=2)b.uuids.push(c.getCanonicalUUID(q(d,e+g).toString(16)));else if(4===h||5===h)for(g=0;g<f;g+=4)b.uuids.push(c.getCanonicalUUID(r(d,e+g).toString(16)));else if(6===h||7===h)for(g=0;g<f;g+=16)b.uuids.push(c.getCanonicalUUID(s(d,e+g)));e+=f}}function n(a){return a>64&&a<91?a-65:a>96&&a<123?a-71:a>47&&a<58?a+4:43===a?62:47===a?63:0}function o(a,b){for(var c,d,e=a.replace(/[^A-Za-z0-9\+\/]/g,""),f=e.length,g=b?Math.ceil((3*f+1>>2)/b)*b:3*f+1>>2,h=new Uint8Array(g),i=0,j=0,k=0;k<f;k++)if(d=3&k,i|=n(e.charCodeAt(k))<<18-6*d,3===d||f-k==1){for(c=0;c<3&&j<g;c++,j++)h[j]=i>>>(16>>>c&24)&255;i=0}return h}function p(a,b){var c=a[b];return 128&c&&(c-=256),c}function q(a,b){return(a[b+1]<<8)+a[b]}function r(a,b){return(a[b+3]<<24)+(a[b+2]<<16)+(a[b+1]<<8)+a[b]}function s(a,b){for(var c="",d=0;d<16;d++)c+=("00"+a[b+d].toString(16)).slice(-2);return c}function t(a){var b=new Uint8Array(a).buffer;return new DataView(b)}function u(){return a.cordova?a.cordova.platformId:null}function v(){return"ios"===u()}if(b._addAdapter){var w={},x={},y={},z={},A={},B={};b._addAdapter("evothings",w),w.startScan=function(a,b,c,e){d(function(){evothings.ble.stopScan(),evothings.ble.startScan(a,function(a){b&&b(k(a))},function(a){e&&e(a)}),c&&c()})},w.stopScan=function(a){d(function(){evothings.ble.stopScan()})},w.connect=function(a,b,c,d){if(x[a])return void(d&&d("device already connected"));evothings.ble.connect(a,function(d){2===d.state&&b?(x[a]=d.deviceHandle,b()):0===d.state&&c&&(e(a),c())},function(a){d&&d(a)})},w.disconnect=function(a,b){e(a)},w.discoverServices=function(a,b,d,e){var f=g(a,e);f&&evothings.ble.services(f,function(a){var e=[];a.forEach(function(a){var d=c.getCanonicalUUID(a.uuid);(!b||0===b.length||b.indexOf(d)>=0)&&(y[a.handle]=f,e.push({_handle:a.handle,uuid:d,primary:!0}))}),d&&d(e)},function(a){e&&e(a)})},w.discoverIncludedServices=function(a,b,c,d){c([])},w.discoverCharacteristics=function(a,b,d,e){var f=h(a,e);f&&evothings.ble.characteristics(f,a,function(a){var e=[];a.forEach(function(a){var d=c.getCanonicalUUID(a.uuid);(!b||0===b.length||b.indexOf(d)>=0)&&(z[a.handle]=f,e.push({_handle:a.handle,uuid:d,properties:{broadcast:1&a.property,read:2&a.property,writeWithoutResponse:4&a.property&&1&a.writeType,write:8&a.property,notify:16&a.property,indicate:32&a.property,authenticatedSignedWrites:64&a.property&&4&a.writeType,reliableWrite:!1,writableAuxiliaries:!1}}))}),d&&d(e)},function(a){e&&e(a)})},w.discoverDescriptors=function(a,b,d,e){var f=i(a,e);f&&evothings.ble.descriptors(f,a,function(e){var g=[];e.forEach(function(d){var e=c.getCanonicalUUID(d.uuid);"00002902-0000-1000-8000-00805f9b34fb"===e&&(B[a]=d.handle),(!b||0===b.length||b.indexOf(e)>=0)&&(A[d.handle]=f,g.push({_handle:d.handle,uuid:e}))}),d&&d(g)},function(a){e&&e(a)})},w.readCharacteristic=function(a,b,c){var d=i(a,c);d&&evothings.ble.readCharacteristic(d,a,function(a){b&&b(t(a))},function(a){c&&c(a)})},w.writeCharacteristic=function(a,b,c,d){var e=i(a,d);e&&evothings.ble.writeCharacteristic(e,a,b,function(){c&&c()},function(a){d&&d(a)})},w.enableNotify=function(a,b,c,d){function e(){evothings.ble.enableNotification(f,a,function(a){b&&b(t(a))},function(a){d&&d(a)}),c&&c()}var f=i(a,d);f&&j(f,a,e,function(a){d&&d(a)})},w.disableNotify=function(a,b,c){var d=i(a,c);d&&(evothings.ble.disableNotification(d,a,function(){b&&b()},function(a){c&&c(a)}),v()&&setTimeout(b,0))},w.readDescriptor=function(a,b,c){var d=getDeviceHandleFromDescriptorHandle(a,c);d&&evothings.ble.readDescriptor(d,a,function(a){b&&b(t(a))},function(a){c&&c(a)})},w.writeDescriptor=function(a,b,c,d){var e=getDeviceHandleFromDescriptorHandle(a,d);e&&evothings.ble.writeDescriptor(e,a,b,function(){c&&c()},function(a){d&&d(a)})}}}); |
@@ -53,3 +53,3 @@ /* @license | ||
*/ | ||
function(a,b){"function"==typeof define&&define.amd?a.navigator.bluetooth?define(a.navigator.bluetooth):define(["es6-promise","es6-map","bluetooth.helpers"],b):"object"==typeof exports?module.exports=b(Promise,Map,require("./bluetooth.helpers")):a.bleat=a.navigator.bluetooth||b(a.Promise,a.Map,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(a,b){return function(c){a(b+": "+c)}}function e(a,b){b&&Object.keys(b).forEach(function(c){b[c]&&a.hasOwnProperty(c)&&("[object Object]"===Object.prototype.toString.call(a[c])?e(a[c],b[c]):"[object Map]"===Object.prototype.toString.call(a[c])&&"[object Object]"===Object.prototype.toString.call(b[c])?Object.keys(b[c]).forEach(function(d){a[c].set(d,b[c][d])}):a[c]=b[c])})}function f(a){return function(b,c,d){a.indexOf(b)<0||(this.__events||(this.__events={}),this.__events[b]||(this.__events[b]=[]),this.__events[b].push(c))}}function g(a,b,c){if(this.__events&&this.__events[a]){var d=this.__events[a].indexOf(b);d>=0&&this.__events[a].splice(d,1),0===this.__events[a].length&&delete this.__events[a],0===Object.keys(this.__events).length&&delete this.__events}}function h(a){this.__events&&this.__events[a.type]&&(a.target=this,this.__events[a.type].forEach(function(b){"function"==typeof b&&b(a)}))}function i(a,b,d){var e=!1;return a.filters.forEach(function(a){if(!a.name||a.name===b.name){if(a.namePrefix){if(a.namePrefix.length>b.name.length)return;if(a.namePrefix!==b.name.substr(0,a.namePrefix.length))return}if(a.services){var f=a.services.map(c.getServiceUUID);if(!f.every(function(a){return b.uuids.indexOf(a)>-1}))return;d=d.concat(f)}e=!0}}),!!e&&b}function j(b){return new a(function(a,e){if(null!==o)return e("requestDevice error: request in progress");if(!b.acceptAllDevices&&!b.deviceFound){if(!b.filters||0===b.filters.length)return e(new TypeError("requestDevice error: no filters specified"));if(b.filters.some(function(a){return 0===Object.keys(a).length}))return e(new TypeError("requestDevice error: empty filter specified"));if(b.filters.some(function(a){return void 0!==a.namePrefix&&""===a.namePrefix}))return e(new TypeError("requestDevice error: empty namePrefix specified"))}var f=[];b.filters&&b.filters.forEach(function(a){a.services&&(f=f.concat(a.services.map(c.getServiceUUID)))}),f=f.filter(function(a,b,c){return c.indexOf(a)===b});var g=!1;m.startScan(f,function(d){function e(b){k().then(function(){a(b)})}function f(){e(j)}var h=[];if(b.filters&&(d=i(b,d,h)),d){g=!0,b.optionalServices&&(h=h.concat(b.optionalServices.map(c.getServiceUUID))),d._allowedServices=h.filter(function(a,b,c){return c.indexOf(a)===b});var j=new p(d);b.deviceFound&&!b.deviceFound(j,f)||e(j)}},function(){o=setTimeout(function(){k().then(function(){g||e("requestDevice error: no devices found")})},b.scanTime||l)},d(e,"requestDevice error"))})}function k(){return new a(function(a,b){o&&(clearTimeout(o),o=null,m.stopScan()),a()})}var l=10240,m=null,n={},o=null,p=function(a){this._handle=null,this._allowedServices=[],this.id="unknown",this.name=null,this.adData={appearance:null,txPower:null,rssi:null,manufacturerData:new b,serviceData:new b},this.gatt=new q,this.gatt.device=this,this.uuids=[],e(this,a)};p.prototype.addEventListener=f(["gattserverdisconnected"]),p.prototype.removeEventListener=g,p.prototype.dispatchEvent=h;var q=function(){this._services=null,this.device=null,this.connected=!1};q.prototype.connect=function(){return new a(function(a,b){if(this.connected)return b("connect error: device already connected");m.connect(this.device._handle,function(){this.connected=!0,a(this)}.bind(this),function(){this._services=null,this.connected=!1,this.device.dispatchEvent({type:"gattserverdisconnected",bubbles:!0})}.bind(this),d(b,"connect error"))}.bind(this))},q.prototype.disconnect=function(){m.disconnect(this.device._handle),this.connected=!1},q.prototype.getPrimaryService=function(b){return new a(function(a,c){return this.connected?b?void this.getPrimaryServices(b).then(function(b){if(1!==b.length)return c("getPrimaryService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getPrimaryService error: no service specified"):c("getPrimaryService error: device not connected")}.bind(this))},q.prototype.getPrimaryServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getPrimaryServices error: service not found");a(d)}return this.connected?this._services?f.call(this):void m.discoverServices(this.device._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getPrimaryServices error")):e("getPrimaryServices error: device not connected")}.bind(this))};var r=function(a){this._handle=null,this._services=null,this._characteristics=null,this.device=null,this.uuid=null,this.isPrimary=!1,e(this,a),this.dispatchEvent({type:"serviceadded",bubbles:!0})};r.prototype.getCharacteristic=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getCharacteristics(b).then(function(b){if(1!==b.length)return c("getCharacteristic error: characteristic not found");a(b[0])}).catch(function(a){c(a)}):c("getCharacteristic error: no characteristic specified"):c("getCharacteristic error: device not connected")}.bind(this))},r.prototype.getCharacteristics=function(b){return new a(function(a,e){function f(){if(!b)return a(this._characteristics);var d=this._characteristics.filter(function(a){return a.uuid===c.getCharacteristicUUID(b)});if(1!==d.length)return e("getCharacteristics error: characteristic not found");a(d)}return this.device.gatt.connected?this._characteristics?f.call(this):void m.discoverCharacteristics(this._handle,[],function(a){this._characteristics=a.map(function(a){return a.service=this,new s(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getCharacteristics error")):e("getCharacteristics error: device not connected")}.bind(this))},r.prototype.getIncludedService=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getIncludedServices(b).then(function(b){if(1!==b.length)return c("getIncludedService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getIncludedService error: no service specified"):c("getIncludedService error: device not connected")}.bind(this))},r.prototype.getIncludedServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getIncludedServices error: service not found");a(d)}return this.device.gatt.connected?this._services?f.call(this):void m.discoverIncludedServices(this._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getIncludedServices error")):e("getIncludedServices error: device not connected")}.bind(this))},r.prototype.addEventListener=f(["serviceadded","servicechanged","serviceremoved"]),r.prototype.removeEventListener=g,r.prototype.dispatchEvent=h;var s=function(a){this._handle=null,this._descriptors=null,this.service=null,this.uuid=null,this.properties={broadcast:!1,read:!1,writeWithoutResponse:!1,write:!1,notify:!1,indicate:!1,authenticatedSignedWrites:!1,reliableWrite:!1,writableAuxiliaries:!1},this.value=null,e(this,a)};s.prototype.getDescriptor=function(b){return new a(function(a,c){return this.service.device.gatt.connected?b?void this.getDescriptors(b).then(function(b){if(1!==b.length)return c("getDescriptor error: descriptor not found");a(b[0])}).catch(function(a){c(a)}):c("getDescriptor error: no descriptor specified"):c("getDescriptor error: device not connected")}.bind(this))},s.prototype.getDescriptors=function(b){return new a(function(a,e){function f(){if(!b)return a(this._descriptors);var d=this._descriptors.filter(function(a){return a.uuid===c.getDescriptorUUID(b)});if(1!==d.length)return e("getDescriptors error: descriptor not found");a(d)}return this.service.device.gatt.connected?this._descriptors?f.call(this):void m.discoverDescriptors(this._handle,[],function(a){this._descriptors=a.map(function(a){return a.characteristic=this,new t(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getDescriptors error")):e("getDescriptors error: device not connected")}.bind(this))},s.prototype.readValue=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("readValue error: device not connected");m.readCharacteristic(this._handle,function(b){this.value=b,a(b),this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),d(b,"readValue error"))}.bind(this))},s.prototype.writeValue=function(b){return new a(function(a,c){if(!this.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeCharacteristic(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},s.prototype.startNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("startNotifications error: device not connected");m.enableNotify(this._handle,function(a){this.value=a,this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),function(){a(this)}.bind(this),d(b,"startNotifications error"))}.bind(this))},s.prototype.stopNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("stopNotifications error: device not connected");m.disableNotify(this._handle,function(){a(this)}.bind(this),d(b,"stopNotifications error"))}.bind(this))},s.prototype.addEventListener=f(["characteristicvaluechanged"]),s.prototype.removeEventListener=g,s.prototype.dispatchEvent=h;var t=function(a){this._handle=null,this.characteristic=null,this.uuid=null,this.value=null,e(this,a)};return t.prototype.readValue=function(){return new a(function(a,b){if(!this.characteristic.service.device.gatt.connected)return b("readValue error: device not connected");m.readDescriptor(this._handle,function(b){this.value=b,a(b)}.bind(this),d(b,"readValue error"))}.bind(this))},t.prototype.writeValue=function(b){return new a(function(a,c){if(!this.characteristic.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeDescriptor(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},{_addAdapter:function(a,b){n[a]=b,m=b},requestDevice:j,cancelRequest:k}}),/* @license | ||
function(a,b){"function"==typeof define&&define.amd?define(["es6-promise","es6-map","bluetooth.helpers"],b):"object"==typeof exports?module.exports=b(Promise,Map,require("./bluetooth.helpers")):a.bleat=b(a.Promise,a.Map,a.bleatHelpers)}(this,function(a,b,c){"use strict";function d(a,b){return function(c){a(b+": "+c)}}function e(a,b){b&&Object.keys(b).forEach(function(c){b[c]&&a.hasOwnProperty(c)&&("[object Object]"===Object.prototype.toString.call(a[c])?e(a[c],b[c]):"[object Map]"===Object.prototype.toString.call(a[c])&&"[object Object]"===Object.prototype.toString.call(b[c])?Object.keys(b[c]).forEach(function(d){a[c].set(d,b[c][d])}):a[c]=b[c])})}function f(a){return function(b,c,d){a.indexOf(b)<0||(this.__events||(this.__events={}),this.__events[b]||(this.__events[b]=[]),this.__events[b].push(c))}}function g(a,b,c){if(this.__events&&this.__events[a]){var d=this.__events[a].indexOf(b);d>=0&&this.__events[a].splice(d,1),0===this.__events[a].length&&delete this.__events[a],0===Object.keys(this.__events).length&&delete this.__events}}function h(a){this.__events&&this.__events[a.type]&&(a.target=this,this.__events[a.type].forEach(function(b){"function"==typeof b&&b(a)}))}function i(a,b,d){var e=!1;return a.filters.forEach(function(a){if(!a.name||a.name===b.name){if(a.namePrefix){if(a.namePrefix.length>b.name.length)return;if(a.namePrefix!==b.name.substr(0,a.namePrefix.length))return}if(a.services){var f=a.services.map(c.getServiceUUID);if(!f.every(function(a){return b.uuids.indexOf(a)>-1}))return;d=d.concat(f)}e=!0}}),!!e&&b}function j(b){return new a(function(a,e){if(null!==o)return e("requestDevice error: request in progress");if(!b.acceptAllDevices&&!b.deviceFound){if(!b.filters||0===b.filters.length)return e(new TypeError("requestDevice error: no filters specified"));if(b.filters.some(function(a){return 0===Object.keys(a).length}))return e(new TypeError("requestDevice error: empty filter specified"));if(b.filters.some(function(a){return void 0!==a.namePrefix&&""===a.namePrefix}))return e(new TypeError("requestDevice error: empty namePrefix specified"))}var f=[];b.filters&&b.filters.forEach(function(a){a.services&&(f=f.concat(a.services.map(c.getServiceUUID)))}),f=f.filter(function(a,b,c){return c.indexOf(a)===b});var g=!1;m.startScan(f,function(d){function e(b){k().then(function(){a(b)})}function f(){e(j)}var h=[];if(b.filters&&(d=i(b,d,h)),d){g=!0,b.optionalServices&&(h=h.concat(b.optionalServices.map(c.getServiceUUID))),d._allowedServices=h.filter(function(a,b,c){return c.indexOf(a)===b});var j=new p(d);b.deviceFound&&!b.deviceFound(j,f)||e(j)}},function(){o=setTimeout(function(){k().then(function(){g||e("requestDevice error: no devices found")})},b.scanTime||l)},d(e,"requestDevice error"))})}function k(){return new a(function(a,b){o&&(clearTimeout(o),o=null,m.stopScan()),a()})}var l=10240,m=null,n={},o=null,p=function(a){this._handle=null,this._allowedServices=[],this.id="unknown",this.name=null,this.adData={appearance:null,txPower:null,rssi:null,manufacturerData:new b,serviceData:new b},this.gatt=new q,this.gatt.device=this,this.uuids=[],e(this,a)};p.prototype.addEventListener=f(["gattserverdisconnected"]),p.prototype.removeEventListener=g,p.prototype.dispatchEvent=h;var q=function(){this._services=null,this.device=null,this.connected=!1};q.prototype.connect=function(){return new a(function(a,b){if(this.connected)return b("connect error: device already connected");m.connect(this.device._handle,function(){this.connected=!0,a(this)}.bind(this),function(){this._services=null,this.connected=!1,this.device.dispatchEvent({type:"gattserverdisconnected",bubbles:!0})}.bind(this),d(b,"connect error"))}.bind(this))},q.prototype.disconnect=function(){m.disconnect(this.device._handle),this.connected=!1},q.prototype.getPrimaryService=function(b){return new a(function(a,c){return this.connected?b?void this.getPrimaryServices(b).then(function(b){if(1!==b.length)return c("getPrimaryService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getPrimaryService error: no service specified"):c("getPrimaryService error: device not connected")}.bind(this))},q.prototype.getPrimaryServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getPrimaryServices error: service not found");a(d)}return this.connected?this._services?f.call(this):void m.discoverServices(this.device._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getPrimaryServices error")):e("getPrimaryServices error: device not connected")}.bind(this))};var r=function(a){this._handle=null,this._services=null,this._characteristics=null,this.device=null,this.uuid=null,this.isPrimary=!1,e(this,a),this.dispatchEvent({type:"serviceadded",bubbles:!0})};r.prototype.getCharacteristic=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getCharacteristics(b).then(function(b){if(1!==b.length)return c("getCharacteristic error: characteristic not found");a(b[0])}).catch(function(a){c(a)}):c("getCharacteristic error: no characteristic specified"):c("getCharacteristic error: device not connected")}.bind(this))},r.prototype.getCharacteristics=function(b){return new a(function(a,e){function f(){if(!b)return a(this._characteristics);var d=this._characteristics.filter(function(a){return a.uuid===c.getCharacteristicUUID(b)});if(1!==d.length)return e("getCharacteristics error: characteristic not found");a(d)}return this.device.gatt.connected?this._characteristics?f.call(this):void m.discoverCharacteristics(this._handle,[],function(a){this._characteristics=a.map(function(a){return a.service=this,new s(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getCharacteristics error")):e("getCharacteristics error: device not connected")}.bind(this))},r.prototype.getIncludedService=function(b){return new a(function(a,c){return this.device.gatt.connected?b?void this.getIncludedServices(b).then(function(b){if(1!==b.length)return c("getIncludedService error: service not found");a(b[0])}).catch(function(a){c(a)}):c("getIncludedService error: no service specified"):c("getIncludedService error: device not connected")}.bind(this))},r.prototype.getIncludedServices=function(b){return new a(function(a,e){function f(){if(!b)return a(this._services);var d=this._services.filter(function(a){return a.uuid===c.getServiceUUID(b)});if(1!==d.length)return e("getIncludedServices error: service not found");a(d)}return this.device.gatt.connected?this._services?f.call(this):void m.discoverIncludedServices(this._handle,this.device._allowedServices,function(a){this._services=a.map(function(a){return a.device=this.device,new r(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getIncludedServices error")):e("getIncludedServices error: device not connected")}.bind(this))},r.prototype.addEventListener=f(["serviceadded","servicechanged","serviceremoved"]),r.prototype.removeEventListener=g,r.prototype.dispatchEvent=h;var s=function(a){this._handle=null,this._descriptors=null,this.service=null,this.uuid=null,this.properties={broadcast:!1,read:!1,writeWithoutResponse:!1,write:!1,notify:!1,indicate:!1,authenticatedSignedWrites:!1,reliableWrite:!1,writableAuxiliaries:!1},this.value=null,e(this,a)};s.prototype.getDescriptor=function(b){return new a(function(a,c){return this.service.device.gatt.connected?b?void this.getDescriptors(b).then(function(b){if(1!==b.length)return c("getDescriptor error: descriptor not found");a(b[0])}).catch(function(a){c(a)}):c("getDescriptor error: no descriptor specified"):c("getDescriptor error: device not connected")}.bind(this))},s.prototype.getDescriptors=function(b){return new a(function(a,e){function f(){if(!b)return a(this._descriptors);var d=this._descriptors.filter(function(a){return a.uuid===c.getDescriptorUUID(b)});if(1!==d.length)return e("getDescriptors error: descriptor not found");a(d)}return this.service.device.gatt.connected?this._descriptors?f.call(this):void m.discoverDescriptors(this._handle,[],function(a){this._descriptors=a.map(function(a){return a.characteristic=this,new t(a)}.bind(this)),f.call(this)}.bind(this),d(e,"getDescriptors error")):e("getDescriptors error: device not connected")}.bind(this))},s.prototype.readValue=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("readValue error: device not connected");m.readCharacteristic(this._handle,function(b){this.value=b,a(b),this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),d(b,"readValue error"))}.bind(this))},s.prototype.writeValue=function(b){return new a(function(a,c){if(!this.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeCharacteristic(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},s.prototype.startNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("startNotifications error: device not connected");m.enableNotify(this._handle,function(a){this.value=a,this.dispatchEvent({type:"characteristicvaluechanged",bubbles:!0})}.bind(this),function(){a(this)}.bind(this),d(b,"startNotifications error"))}.bind(this))},s.prototype.stopNotifications=function(){return new a(function(a,b){if(!this.service.device.gatt.connected)return b("stopNotifications error: device not connected");m.disableNotify(this._handle,function(){a(this)}.bind(this),d(b,"stopNotifications error"))}.bind(this))},s.prototype.addEventListener=f(["characteristicvaluechanged"]),s.prototype.removeEventListener=g,s.prototype.dispatchEvent=h;var t=function(a){this._handle=null,this.characteristic=null,this.uuid=null,this.value=null,e(this,a)};return t.prototype.readValue=function(){return new a(function(a,b){if(!this.characteristic.service.device.gatt.connected)return b("readValue error: device not connected");m.readDescriptor(this._handle,function(b){this.value=b,a(b)}.bind(this),d(b,"readValue error"))}.bind(this))},t.prototype.writeValue=function(b){return new a(function(a,c){if(!this.characteristic.service.device.gatt.connected)return c("writeValue error: device not connected");var e=b.buffer||b,f=new DataView(e);m.writeDescriptor(this._handle,f,function(){this.value=f,a()}.bind(this),d(c,"writeValue error"))}.bind(this))},{_addAdapter:function(a,b){n[a]=b,m=b},requestDevice:j,cancelRequest:k}}),/* @license | ||
* | ||
@@ -56,0 +56,0 @@ * BLE Abstraction Tool: noble adapter |
{ | ||
"name": "bleat", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Abstraction library following Web Bluetooth specification for hiding differences in JavaScript BLE APIs", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/thegecko/bleat", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
225302
3119