import { adapter } from "./adapter" ; import { W3CBluetoothRemoteGATTDescriptor } from "./interfaces" ; /**
 * Bluetooth Remote GATT Descriptor class
 */ export class BluetoothRemoteGATTDescriptor implements W3CBluetoothRemoteGATTDescriptor { /**
     * The characteristic the descriptor is related to
     */ public readonly characteristic : BluetoothRemoteGATTCharacteristic = null ; /**
     * The unique identifier of the descriptor
     */ public readonly uuid : string = null ; private _value : DataView = null ; /**
     * The value of the descriptor
     */ public get value ( ) : DataView { return this . _value ; } private handle : string = null ; param init constructor ( init ) { this . characteristic = init . characteristic ; this . uuid = init . uuid ; this . _value = init . value ; this . handle = `${ this . characteristic . uuid }-${ this . uuid }` ; } returns public readValue ( ) { return new Promise ( ( resolve , reject ) => { if ( ! this . characteristic . service . device . gatt . connected ) return reject ( "readValue error: device not connected" ) ; adapter . readDescriptor ( this . handle , dataView => { this . _value = dataView ; resolve ( dataView ) ; } , error => { reject ( `readValue error: ${ error }` ) ; } ) ; } ) ; } param value public writeValue ( value ) { return new Promise ( ( resolve , reject ) => { if ( ! this . characteristic . service . device . gatt . connected ) return reject ( "writeValue error: device not connected" ) ; function isView ( source ) { return ( source as ArrayBufferView ) . buffer !== undefined ; } const arrayBuffer = isView ( value ) ? value . buffer : value ; const dataView = new DataView ( arrayBuffer ) ; adapter . writeDescriptor ( this . handle , dataView , ( ) => { this . _value = dataView ; resolve ( ) ; } , error => { reject ( `writeValue error: ${ error }` ) ; } ) ; } ) ; } }