1import * as BT_HOST from 'BT_HOST' 2import * as events from 'events' 3 4class bt_host extends events.EventEmitter{ 5 constructor(options) { 6 super(); 7 if (!options) { 8 throw new Error('options is invalid'); 9 } 10 11 if (this.inited == true) { 12 throw new Error('bt_host already inited'); 13 } 14 this.options = { 15 deviceName: options.deviceName, 16 conn_num_max: options.conn_num_max, 17 }; 18 19 this.success = options.success|| function(){}; 20 this.failed = options.failed|| function(){}; 21 22 this._init(); 23 } 24 25 _init() { 26 console.log('bt_host native init') 27 let result = BT_HOST.init(this.options); 28 if (result == 0) { 29 this.inited = true; 30 console.log('bt host inited'); 31 this.success(); 32 } else { 33 console.log('bt host init failed'); 34 this.failed(); 35 } 36 } 37 38 start_adv(params) { 39 console.log('bt host start adv') 40 if (this.inited == false) { 41 throw new Error('bt host not initialed'); 42 } 43 44 if (this.adv_flag == true) { 45 this.stop_adv() 46 } 47 48 let result = BT_HOST.start_adv(params, function(conn_handle, connect){ 49 console.log('connect callback'/*+conn_handle+connect*/); 50 if (connect) { 51 this.adv_flag = false 52 this.emit('connect', conn_handle); 53 } else { 54 this.emit('disconnect', conn_handle); 55 } 56 }.bind(this)); 57 if (result == 0) { 58 this.adv_flag = true; 59 if (params.success) { 60 params.success(); 61 } 62 } else { 63 if (params.failed) { 64 params.failed(); 65 } 66 } 67 } 68 69 stop_adv(params) { 70 if (this.inited == false) { 71 throw new Error('bt host not initialed'); 72 } 73 74 if (this.adv_flag == false) { 75 return; 76 } 77 78 let result = BT_HOST.stop_adv(); 79 if (result == 0) { 80 if (params.success) { 81 params.success(); 82 } 83 this.adv_flag = false; 84 } else { 85 if (params.failed) { 86 params.failed(); 87 } 88 } 89 } 90 91 start_scan(params) { 92 console.log('bt host start scan') 93 if (this.inited == false) { 94 throw new Error('bt host not initialed'); 95 } 96 97 if (this.scan_flag == true) { 98 stop_scan() 99 } 100 101 let result = BT_HOST.start_scan(params, function(addr, addr_type, adv_type, adv_data, rssi){ 102 console.log('scan result callback'+' addr:'+addr+' data:'+adv_data); 103 }.bind(this)); 104 if (result == 0) { 105 this.scan_flag = true; 106 if (params.success) { 107 params.success(); 108 } 109 } else { 110 if (params.failed) { 111 params.failed(); 112 } 113 } 114 } 115 116 stop_scan(params) { 117 if (this.inited == false) { 118 throw new Error('bt host not initialed'); 119 } 120 121 if (this.scan_flag == false) { 122 return; 123 } 124 125 let result = BT_HOST.stop_scan(); 126 if (result == 0) { 127 this.scan_flag = false; 128 if (params.success) { 129 params.success(); 130 } 131 } else { 132 if (params.failed) { 133 params.failed(); 134 } 135 } 136 } 137 138 add_service(params) { 139 if (this.inited == false) { 140 throw new Error('bt host not initialed'); 141 } 142 console.log('srvc_cfg: ' + params.service); 143 let result = BT_HOST.add_service(params.service, function(len, data){ 144 console.log('add_service: len: ' + len + ', data: ' + data); 145 this.emit('onCharWrite', data); 146 }.bind(this)); 147 console.log('add_service result: ' + result); 148 if (result == 0) { 149 if (params.success) { 150 params.success(); 151 } 152 } else { 153 if (params.failed) { 154 params.failed(); 155 } 156 } 157 } 158 159 update_char(params) { 160 if (this.inited == false) { 161 throw new Error('bt host not initialed'); 162 } 163 let result = BT_HOST.update_chars(params.arg); 164 if (result == 0) { 165 if (params.success) { 166 params.success(); 167 } 168 } else { 169 if (params.failed) { 170 params.failed(); 171 } 172 } 173 } 174} 175 176export function open(options) { 177 return new bt_host(options); 178} 179