@youssefhenna/expo-enode-link-sdk
Version:
An Expo Module integration for Enode's Link SDK
6 lines (5 loc) • 1.71 kB
JavaScript
/*global
atob, Event, nslog, uk, window
*/
// https://webbluetoothcg.github.io/web-bluetooth/ interface
!function(){"use strict";const n=enode.wb,t=enode.wbutils;n.BluetoothRemoteGATTServer=function(e){if(void 0===e)throw new Error("Attempt to create BluetoothRemoteGATTServer with no device");t.defineROProperties(this,{device:e}),this.connected=!1,this.connectionTransactionIDs=[]},n.BluetoothRemoteGATTServer.prototype={connect:function(){let e=this,t=n.native.getTransactionID();return this.connectionTransactionIDs.push(t),this.sendMessage("connectGATT",{callbackID:t}).then(function(){return e.connected=!0,n.native.registerDeviceForNotifications(e.device),e.connectionTransactionIDs.splice(e.connectionTransactionIDs.indexOf(t),1),e})},disconnect:function(){this.connectionTransactionIDs.forEach(e=>n.native.cancelTransaction(e)),this.connectionTransactionIDs=[],this.connected&&(this.connected=!1,this.device.dispatchEvent(new n.BluetoothEvent("gattserverdisconnected",this.device)),n.native.unregisterDeviceForNotifications(this.device),this.sendMessage("disconnectGATT"))},getPrimaryService:async function(e){return e?(await this.getPrimaryServices(e))[0]:Promise.reject(new Error("getPrimaryService requires a UUID"))},getPrimaryServices:async function(e){e={data:e?{serviceUUID:window.BluetoothUUID.getService(e)}:{}};return(await this.sendMessage("getPrimaryServices",e)).map(e=>new n.BluetoothRemoteGATTService(this.device,window.BluetoothUUID.getService(e),!0))},sendMessage:async function(e,t){return(t=t||{}).data=t.data||{},t.data.deviceId=this.device.id,n.native.sendMessage("device:"+e,t)},toString:function(){return`BluetoothRemoteGATTServer(${this.device.toString()})`}}}();