You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JLAlipayBleManager.js 11KB

1
  1. var bleCode=require('./JLConfig.js');var onfire=require('./onfire.js');var dataTool=require('./dataTool.js');var successCode=bleCode.bleSuccessCode();var errorCode=bleCode.bleErrorCode();var serviceId='FEE7';var charact_serviceId=serviceId;var deviceId;var characticId;var TAG=" JLAlipayBleManager.js ";var dev;export function scan_StartScanBleDevice(nameFlag,callBack){var devs=[];initBluetoothAdapte((res)=>{my.startBluetoothDevicesDiscovery({services:['FEE7'],success:(res)=>{my.onBluetoothDeviceFound({success:(res)=>{console.log(TAG+" onBluetoothDeviceFound "+JSON.stringify(res));var scandevices=res.devices;for(var i=0;i<scandevices.length;i++){var scan_dev=scandevices[i];if(nameFlag!=null){if(scan_dev.name.startsWith(nameFlag)){devs.push(scan_dev)}}else{if(scan_dev.name&&scan_dev.deviceId){devs.push(scan_dev)}}}if(devs.length>0){callBack.call(this,{code:successCode,err_msg:'搜索设备成功',data:devs,msg:''})}}})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,err_msg:msg,data:null})}})})}export function scan_stopScanBleDevice(callBack){my.stopBluetoothDevicesDiscovery({success:(res)=>{callBack.call(this,{code:successCode,err_msg:'停止扫描成功',data:null,msg:''})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,err_msg:msg,data:null,msg:''})}})}export function connectDevice(device,callBack){dev=device;getConnectedDevice((res)=>{console.log(TAG+"getConnectedDevice "+JSON.stringify(res));if(res.code==successCode){wxConnectDevice(function(res){if(res.code==errorCode){if(res.code==errorCode){wxConnectDevice(function(res){if(res.code==errorCode){wxConnectDevice(function(res){callBack.call(this,res)})}else{callBack.call(this,res)}})}else{callBack.call(this,res)}}else{callBack.call(this,res)}})}else{callBack.call(this,res)}})}export function disConnectDevice(callBack){wxDisConnectDevice(function(res){callBack.call(this,res)})}export function findDeviceServices(device,callBack1,callBack2,callBack3){var soft_ver='';var hard_ver='';var serial_ver='';var protocol_ver='';getDeviceService(device,function(res){getDeviceCharacteristics(device,{ser_fee7:res.data.fee7,ser_180a:res.data.s180A},(res1)=>{callBack1.call(this,res1)},(res2)=>{if(res2.code==successCode){my.onBLECharacteristicValueChange({success:(res)=>{console.log(JSON.stringify(res));if(res.serviceId.substr(0,4).startsWith('fee7')||res.serviceId.substr(0,8).startsWith('0000fee7')||res.serviceId.substr(0,4).startsWith('FEE7')||res.serviceId.substr(0,8).startsWith('0000FEE7')){console.log('RECV <<================:'+res.value);callBack2.call(this,{code:successCode,msg:'收到设备数据',data:{value:res.value}})}}})}else{callBack2.call(this,res2)}},(res3)=>{my.onBLECharacteristicValueChange({success:(res)=>{if(res.characteristicId.substr(0,4).startsWith('2a26')||res.characteristicId.substr(0,8).startsWith('00002a26')||res.characteristicId.substr(0,4).startsWith('2A26')||res.characteristicId.substr(0,8).startsWith('00002A26')){protocol_ver=dataTool.hexToString(res.value);console.log('协议版本:'+protocol_ver);callBack3.call(this,{code:0,data:{protocol:protocol_ver},err_msg:'获取版本成功'})}}})})})}export function setParams(id){deviceId=id.devId;serviceId=id.serId;characticId=id.charactId;console.log('写数据各项ID:'+JSON.stringify(id))}export function onDeviceConnectStateChange(callBack){my.offBLECharacteristicValueChange();my.onBLEConnectionStateChanged((res)=>{if(res.connected==false){wxDisConnectDevice((res)=>{callBack.call(this,{code:errorCode,err_msg:'设备断开连接',data:''})})}if(res.connected==true){callBack.call(this,{code:successCode,err_msg:'设备已连接',data:''})}})}export function sendMessageToDevice(cmdArray,callBack){var i=0;var cmd=cmdArray[i];my.writeBLECharacteristicValue({deviceId:deviceId,serviceId:serviceId,characteristicId:characticId,value:cmdArray[0],success:(res)=>{if(i<cmdArray.length-1){console.log('SEND ================>>:'+cmdArray[0]);i++;onfire.fire('send',i)}else{onfire.clear();console.log('SEND ================>>:'+cmdArray[0]);callBack.call(this,{code:successCode,msg:'数据发送成功',data:cmdArray,})}},fail:(err)=>{console.log('writeBLECharacteristicValue fail:'+JSON.stringify(err));var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,msg:msg,data:null})}});onfire.on('send',(i)=>{var cmd1=cmdArray[i];my.writeBLECharacteristicValue({deviceId:deviceId,serviceId:serviceId,characteristicId:characticId,value:cmd1,success:(res)=>{if(i<cmdArray.length-1){console.log('SEND ================>>:'+cmd1);i++;onfire.fire('send',i)}else{onfire.clear();console.log('SEND ================>>:'+cmd1);callBack.call(this,{code:successCode,msg:'数据发送成功',data:cmdArray})}},fail:(err)=>{console.log('writeBLECharacteristicValue fail:'+JSON.stringify(err));var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,msg:msg,data:null})}})})}function getDeviceService(device,callBack){console.log('getDeviceService:'+device);my.getBLEDeviceServices({deviceId:device.deviceId,success:(res)=>{var services=res.services;var ser_180A='';var ser_FEE7='';for(var i=0;i<services.length;i++){if(services[i].serviceId.substr(0,4).startsWith('fee7')||services[i].serviceId.substr(0,8).startsWith('0000fee7')||services[i].serviceId.substr(0,4).startsWith('FEE7')||services[i].serviceId.substr(0,8).startsWith('0000FEE7')){ser_FEE7=res.services[i].serviceId}if(services[i].serviceId.substr(0,4).startsWith('180a')||services[i].serviceId.substr(0,8).startsWith('0000180a')||services[i].serviceId.substr(0,4).startsWith('180A')||services[i].serviceId.substr(0,8).startsWith('0000180A')){ser_180A=res.services[i].serviceId}}callBack.call(this,{code:bleCode.bleSuccessCode(),err_msg:'寻找服务成功',data:{fee7:ser_FEE7,s180A:ser_180A},msg:''})},})}function getDeviceCharacteristics(device,serviceId,callBack1,callBack2,callBack3){my.getBLEDeviceCharacteristics({deviceId:device.deviceId,serviceId:serviceId.ser_180a,success:(res)=>{var charactertics=res.characteristics;for(var i=0;i<charactertics.length;i++){var ch=charactertics[i];if(ch.properties.read==true){my.readBLECharacteristicValue({deviceId:device.deviceId,serviceId:serviceId.ser_180a,characteristicId:ch.characteristicId,success:(res)=>{},})}}},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack3.call(this,{code:errorCode,msg:msg,data:null})},complete:function(com){callBack3.call(this,{code:successCode,err_msg:"",data:null,msg:''})}});my.getBLEDeviceCharacteristics({deviceId:device.deviceId,serviceId:serviceId.ser_fee7,success:(res)=>{var charactertics=res.characteristics;for(var i=0;i<charactertics.length;i++){var charact=charactertics[i];if(charact.properties.read==true){my.readBLECharacteristicValue({deviceId:device.deviceId,serviceId:serviceId.ser_fee7,characteristicId:charact.characteristicId,success:function(res){},})}if(charact.properties.write==true){callBack1.call(this,{code:successCode,err_msg:null,data:{id:{devId:device.deviceId,serId:serviceId.ser_fee7,charactId:charact.characteristicId}},msg:''})}if(charact.properties.notify==true||charact.properties.indicate==true){my.notifyBLECharacteristicValueChange({deviceId:device.deviceId,serviceId:serviceId.ser_fee7,characteristicId:charact.characteristicId,success:(res)=>{callBack2.call(this,{code:successCode,err_msg:'开启设备监听成功',data:res,msg:''})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack2.call(this,{code:errorCode,msg:msg,data:null})},})}}},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack3.call(this,{code:errorCode,msg:msg,data:null})},})}function wxDisConnectDevice(callBack){if(dev!=null){my.offBluetoothAdapterStateChange();my.offBLECharacteristicValueChange();my.offBluetoothDeviceFound();my.offBLEConnectionStateChanged();my.disconnectBLEDevice({deviceId:dev.deviceId,success:(res)=>{my.closeBluetoothAdapter({success:(res)=>{callBack.call(this,{code:successCode,err_msg:'设备断开连接成功',data:null,msg:''})},fail:(err)=>{callBack.call(this,{code:successCode,err_msg:'设备断开连接成功',data:null,msg:''})},})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,msg:msg,data:null})},})}else{callBack.call(this,{code:errorCode,err_msg:'暂无设备连接',data:null,msg:''})}}function getConnectedDevice(callBack){my.getConnectedBluetoothDevices({success:(res)=>{if(res.devices.length>0){callBack.call(this,{code:errorCode,err_msg:'蓝牙被占用',data:res.services[0],msg:''})}else{callBack.call(this,{code:successCode,err_msg:'可连接设备',data:null,msg:''})}},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,msg:msg,data:null})},})}function wxConnectDevice(callBack){if(dev.deviceId&&dev.name){my.connectBLEDevice({deviceId:dev.deviceId,success:(res)=>{callBack.call(this,{code:successCode,err_msg:'蓝牙物理连接成功',data:dev,msg:''})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,err_msg:msg,data:null,msg:''})},})}else{callBack.call(this,{code:errorCode,err_msg:msg,data:null,msg:''})}}function initBluetoothAdapte(callBack){my.openBluetoothAdapter({success:(res)=>{my.getBluetoothAdapterState({success:(res)=>{if(!res.available){callBack.call(this,{code:errorCode,msg:'抱歉,您的手机蓝牙暂不可用',data:null});return}if(res.discovering){callBack.call(this,{code:errorCode,msg:'抱歉,蓝牙正在搜索设备',data:null});return}callBack.call(this,{code:successCode,msg:'本机蓝牙可用',data:null})},})},fail:(err)=>{var msg='';if(err){msg=bluetoothErrMsg(err.error)}callBack.call(this,{code:errorCode,msg:msg,data:null})},})}function bluetoothErrMsg(errorCode){switch(errorCode){case 12:return'手机蓝牙未打开';break;case 10000:return'未初始化蓝牙适配器';break;case 10001:return'当前蓝牙适配器不可用';break;case 10002:return'没有找到指定设备';break;case 10003:return'连接失败:操作超时';break;case 10004:return'蓝牙设备没有找到指定服务';break;case 10005:return'蓝牙设备没有找到指定特征值';break;case 10006:return'当前设备连接已断开';break;case 10007:return'当前特征值不支持此操作';break;case 10008:return'其余所有系统上报的异常';break;case 10009:return'安卓系统版本低于4.3不支持BLE';break;case 10010:return'没有找到指定描述符';break;case 10011:return'设备 id 不可用/为空';break;case 10012:return'服务 id 不可用/为空';break;case 10013:return'特征 id 不可用/为空';break;case 10014:return'发送的数据为空或格式错误';break;case 10015:return'操作超时';break;case 10016:return'缺少参数';break;case 10017:return'写入特征值失败';break;case 10018:return'读取特征值失败';break;default:return'操作失败';break}}