const resp = require('../util/resp') const noble = require('noble'); let inRange = {}; let blueDeviceList = [] let blueCharacteristics = [] let connectedCharacteristic = null; let on_data_callback = null; noble.on('discover', function (peripheral) { if(peripheral.addressType != 'public') { return; } inRange[peripheral.id] = { peripheral: peripheral }; var blueDevice = { 'id': peripheral.id, 'localName': peripheral.advertisement.localName, 'serviceUuids': JSON.stringify(peripheral.advertisement.serviceUuids), 'address': peripheral.address, 'addressType': peripheral.addressType, 'connectable': peripheral.connectable, 'rssi': peripheral.rssi, } blueDeviceList.push(blueDevice) }); function onServicesAndCharacteristicsDiscovered(error, services, characteristics) { if(connectedCharacteristic ==null) { console.log('Discovered services and characteristics'); const echoCharacteristic = characteristics[0]; // data callback receives notifications echoCharacteristic.on('data', (data, isNotification) => { if (on_data_callback) { on_data_callback(data) } }); // subscribe to be notified whenever the peripheral update the characteristic echoCharacteristic.subscribe(error => { if (error) { console.error('Error subscribing to echoCharacteristic'); } else { console.log('Subscribed for echoCharacteristic notifications'); } }); connectedCharacteristic = echoCharacteristic } } module.exports = { scan: function (resp_callback) { try { blueDeviceList = [] blueCharacteristics = [] setTimeout(() => { noble.stopScanning() resp_callback(resp.ok_resp(blueDeviceList)) }, 3000); noble.startScanning() } catch (e) { resp_callback(resp.fail_resp(e.message)) } }, // 连接蓝牙设备 connectDevice:function (args,callBack) { let peripheral = inRange[args.id].peripheral on_data_callback = args.on_data_cb; peripheral.connect(error => { if(error == null) { callBack(resp.ok_resp('success')) } else { callBack(resp.fail_resp('error')) } }) peripheral.on('disconnect', () => { console.log('disconnect') // callBack(resp.fail_resp('connectError')) }); }, // 断开蓝牙设备 disConnectDevice:function (id,callBack) { let peripheral = inRange[id].peripheral peripheral.disconnect(error=>{ if(error == null) { callBack(resp.ok_resp('success')) } else { callBack(resp.fail_resp('error')) } }) }, getCharList:function (id,callBack) { let peripheral = inRange[id].peripheral peripheral.discoverServices([], function (error, services) { for (let serviceIndex = 0; serviceIndex < services.length; serviceIndex++) { (function (service) { service.discoverCharacteristics([], function (error, characteristics) { for (let charIndex = 0; charIndex < characteristics.length; charIndex++) { let c = characteristics[charIndex]; let info = { id: peripheral.id, uuid: service.uuid, name: service.name, characteristic: { 'uuid': c.uuid, 'properties': c.properties } } callBack(info) } }) })(services[serviceIndex]) } }) }, setUp:function (row) { const serviceUUIDs = [row.uuid]; const characteristicUUIDs = [row.characteristic.uuid]; let peripheral = inRange[row.id].peripheral peripheral.discoverSomeServicesAndCharacteristics( serviceUUIDs, characteristicUUIDs, onServicesAndCharacteristicsDiscovered ); }, disSetUp:function () { if(connectedCharacteristic != null) { connectedCharacteristic.unsubscribe(error =>{ if(error ==null) { console.log('unsubscribe,success!') connectedCharacteristic = null } }) } }, write: function (msg, resp_callback) { try { connectedCharacteristic.write(msg); } catch (e) { resp_callback(resp.fail_resp(e.message)) } } }