bluetoothService.ts (1695B)
1 import { PrimaryService, Characteristic } from 'bleno'; 2 import CameraActions from './camera'; 3 class BLEService extends Characteristic { 4 private _value: Buffer; 5 private _updateValueCallback: any; 6 private camera: CameraActions; 7 8 constructor() { 9 super({ 10 uuid: '14444444444444444444444444444448', 11 properties: ['write', 'notify'], 12 value: null 13 }); 14 this.camera = new CameraActions(); 15 this._value = Buffer.alloc(0); 16 this._updateValueCallback = null; 17 } 18 19 onWriteRequest(data: Buffer, _offset: number, _withoutResponse: boolean, callback: (result: number) => void) { 20 this._value = data; 21 console.log(this._value.toString()); 22 // TODO use json instead 23 switch (this._value.toString()) { 24 case "capture": 25 this.camera.capture( 26 false, 27 false, 28 true 29 ); 30 break; 31 case "timelapse": 32 this.camera.run_timelapse( 33 20000, 34 1500, 35 false 36 ); 37 break; 38 } 39 40 41 if (this._updateValueCallback) { 42 this._updateValueCallback(this._value); 43 } 44 45 callback(Characteristic.RESULT_SUCCESS); 46 } 47 48 onSubscribe(_maxValueSize: number, updateValueCallback: any) { 49 this._updateValueCallback = updateValueCallback; 50 } 51 52 onUnsubscribefunction() { 53 this._updateValueCallback = null; 54 } 55 } 56 57 export const bleService = new PrimaryService({ uuid: '14444444444444444444444444444447', characteristics: [new BLEService()] })