var bleno = require('bleno'); var serviceUuids = ['fffffffffffffffffffffffffffffff0'] function Utf8ArrayToStr(array) { var out, i, len, c; var char2, char3; out = ""; len = array.length; i = 0; while(i < len) { c = array[i++]; switch(c >> 4) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 0xxxxxxx out += String.fromCharCode(c); break; case 12: case 13: // 110x xxxx 10xx xxxx char2 = array[i++]; out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); break; case 14: // 1110 xxxx 10xx xxxx 10xx xxxx char2 = array[i++]; char3 = array[i++]; out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); break; } } return out; } var ioNew = null try { bleno.on('stateChange', function(state) { console.log('on -> stateChange: ' + state); if (state === 'poweredOn') { bleno.startAdvertising('semsx', serviceUuids); } else { bleno.stopAdvertising(); } }); bleno.on('advertisingStart', function(error) { console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); if (!error) { io.sockets.emit('blueConnect', 'success'); console.log('start'); var Descriptor = bleno.Descriptor; var descriptor = new Descriptor({ uuid: '212112', value: '123123' // static value, must be of type Buffer or string if set }); var Characteristic = bleno.Characteristic; var characteristic = new Characteristic({ uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit properties: ['read', 'write','notify','writeWithoutResponse'], // can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate' value: null, // optional static value, must be of type Buffer - for read only characteristics descriptors: [ // see Descriptor for data type descriptor ], onWriteRequest: function (data, offset, withoutResponse,callBack) { var obj = JSON.parse( Utf8ArrayToStr(new Uint8Array(data))) //console.log(withoutResponse) callBack() }, onReadRequest: function (offset, callBack) { var result = characteristic.RESULT_SUCCESS; //var data = new Buffer('321'); sendMessageHandle = callBack } }); messageObj = characteristic var PrimaryService = bleno.PrimaryService; var primaryService = new PrimaryService({ uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit characteristics: [ // see Characteristic for data type characteristic ] }); bleno.setServices(primaryService, function (error) { if (error) { console.log(error); } console.log('success'); }); } else { } }); bleno.on('disconnect',function (clientAddress) { console.log('disconnectAddress') }) bleno.on('accept',function (clientAddress) { console.log('connectAddress') }) bleno.on('advertisingStartError', function(error) { console.log(error) }); }catch (err){ // 判断用户是否开启蓝牙 或者是堵塞 if(err.message == 'No compatible USB Bluetooth 4.0 device found!') { // 没有插设备 } console.log(err.message) } var sendMessageHandle = null var messageObj = null module.exports = { sendData:function (buffer) { if(sendMessageHandle) { var result =messageObj.RESULT_SUCCESS; var data = new Buffer('woshi zhangxiaoyu'); sendMessageHandle(result,data) } }, setIo:function (data) { ioNew = data } }