123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- var deviceId, serviceId, characticId, dev, TAG = "JLBleManagerTool.js",
- bleErrorMsg = require("./JLBleErrorMsg.js"),
- bleCode = require("./errorCode.js"),
- onfire = require("./onfire.js"),
- dataTool = require("./dataTool.js"),
- successCode = bleCode.bleSuccessCode(),
- errorCode = bleCode.bleErrorCode(),
- charact_serviceId = serviceId = "FEE7";
- export function scan_StartScanBleDevice(e, o) {
- console.log(serviceId), initBluetoothAdapte(function (c) {
- c.code == bleCode.bleSuccessCode() ? wx.startBluetoothDevicesDiscovery({
- services: ["FEE7"],
- success: function (c) {
- wx.onBluetoothDeviceFound(function (c) {
- for (var r = [], s = c.devices, t = 0; t < s.length; t++) {
- var l = s[t];
- if (null != e) {
- if (-1 != l.name.indexOf(e)) {
- var i = {
- device_no: l.deviceId,
- device_name: l.name
- };
- r.push(i)
- }
- } else if (l.name.length > 0) {
- i = {
- device_no: l.deviceId,
- device_name: l.name
- };
- r.push(i)
- }
- }
- r.length > 0 && o.call(this, {
- code: successCode,
- err_msg: "搜索设备成功",
- data: r,
- msg: ""
- })
- })
- },
- fail: function (e) {
- var c = "";
- e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
- code: bleCode.bleErrorCode(),
- msg: c,
- data: null
- })
- }
- }) : o.call(this, c)
- })
- }
- export function scan_stopScanBleDevice(e) {
- wx.stopBluetoothDevicesDiscovery({
- success: function (o) {
- e.call(this, {
- code: successCode,
- msg: "停止扫描成功",
- data: null
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: errorCode,
- msg: c,
- data: null
- })
- }
- })
- }
-
- function initBluetoothAdapte(e) {
- wx.openBluetoothAdapter({
- success: function (o) {
- wx.getBluetoothAdapterState({
- success: function (o) {
- 1 == o.available && 0 == o.discovering && e.call(this, {
- code: successCode,
- msg: "初始化蓝牙模块成功",
- data: null
- }), 0 == o.available && e.call(this, {
- code: errorCode,
- msg: "蓝牙适配器不可用",
- data: null
- }), 1 == o.discovering && e.call(this, {
- code: errorCode,
- msg: "正在搜索设备",
- data: null
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: errorCode,
- msg: c,
- data: null
- })
- }
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: errorCode,
- msg: c,
- data: null
- })
- }
- })
- }
-
- function closeBluetoothAdapte(e) {
- wx.getBluetoothAdapterState({
- success: function (o) {
- wx.closeBluetoothAdapter({
- success: function (o) {
- e.call(this, {
- code: successCode,
- msg: "关闭蓝牙模块成功",
- data: null
- })
- },
- fail: function (o) {
- e.call(this, {
- code: errorCode,
- msg: o.errmsg,
- data: null
- })
- }
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: errorCode,
- msg: c,
- data: null
- })
- }
- })
- }
- export function connectDevice(e, o) {
- dev = e, getConnectedDevice(function (e) {
- e.code == bleCode.bleSuccessCode() ? wxConnectDevice(function (e) {
- o.call(this, e)
- }) : o.call(this, e)
- })
- }
- export function disConnectDevice(e) {
- wxDisConnectDevice(function (o) {
- e.call(this, o)
- })
- }
- export function onDeviceConnectStateChange(e) {
- wx.onBLEConnectionStateChange(function (o) {
- e.call(this, o)
- })
- }
-
- function wxConnectDevice(e) {
- console.log("dev:" + JSON.stringify(dev)), wx.createBLEConnection({
- deviceId: dev.device_no,
- success: function (o) {
- e.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "蓝牙物理连接成功",
- data: dev,
- msg: ""
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: c,
- data: null,
- msg: ""
- })
- }
- })
- }
-
- function getConnectedDevice(e) {
- wx.getConnectedBluetoothDevices({
- services: ["fee7"],
- success: function (o) {
- o.devices.length > 0 ? e.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: "蓝牙被占用",
- data: o.services[0],
- msg: ""
- }) : e.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "可连接设备",
- data: null,
- msg: ""
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: c,
- data: null,
- msg: ""
- })
- }
- })
- }
-
- function wxDisConnectDevice(e) {
- null != dev ? wx.closeBLEConnection({
- deviceId: dev.device_no,
- success: function (o) {
- wx.closeBluetoothAdapter({
- success: function (o) {
- e.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "设备断开连接成功",
- data: null,
- msg: ""
- })
- },
- fail: function (o) {
- e.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: "设备断开连接成功," + o.errmsg,
- data: null,
- msg: ""
- })
- }
- })
- },
- fail: function (o) {
- var c = "";
- o && (c = bleErrorMsg.bluetoothErrMsg(o.errCode)), e.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: c,
- data: null,
- msg: ""
- })
- }
- }) : e.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "暂无设备连接",
- data: null,
- msg: ""
- })
- }
- export function findDeviceServices(e, o, c) {
- getDeviceService(e, function (r) {
- getDeviceCharacteristics(e, r.data, function (e) {
- o.call(this, e)
- }, function (e) {
- e.code == bleCode.bleSuccessCode() ? wx.onBLECharacteristicValueChange(function (e) {
- c.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "特征值发生变化",
- data: {
- value: dataTool.bufferTohex(e.value).toLocaleUpperCase()
- },
- msg: ""
- })
- }) : c.call(this, e)
- })
- })
- }
-
- function getDeviceService(e, o) {
- wx.getBLEDeviceServices({
- deviceId: e.device_no,
- success: function (e) {
- for (var c = e.services, r = 0; r < c.length; r++) "0000FEE7" == e.services[r].uuid.substr(0, 8) && o.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "寻找服务成功",
- data: e.services[r].uuid,
- msg: ""
- })
- }
- })
- }
-
- function getDeviceCharacteristics(e, o, c, r) {
- wx.getBLEDeviceCharacteristics({
- deviceId: e.device_no,
- serviceId: o,
- success: function (s) {
- for (var t = s.characteristics, l = 0; l < t.length; l++) {
- var i = t[l];
- 1 == i.properties.read && wx.readBLECharacteristicValue({
- deviceId: e.device_no,
- serviceId: charact_serviceId,
- characteristicId: i.uuid,
- success: function (e) {}
- }), 1 == i.properties.write && c.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: null,
- data: {
- id: {
- devId: e.device_no,
- serId: o,
- charactId: i.uuid
- }
- },
- msg: ""
- }), 1 != i.properties.notify && 1 != i.properties.indicate || wx.notifyBLECharacteristicValueChange({
- deviceId: e.device_no,
- serviceId: o,
- characteristicId: i.uuid,
- state: !0,
- success: function (e) {
- r.call(this, {
- code: bleCode.bleSuccessCode(),
- err_msg: "开启设备监听成功",
- data: e,
- msg: ""
- })
- },
- fail: function (e) {
- var o = "";
- e && (o = bleErrorMsg.bluetoothErrMsg(e.errCode)), r.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: o,
- data: null,
- msg: ""
- })
- }
- })
- }
- }
- })
- }
- export function setParams(e) {
- deviceId = e.devId, serviceId = e.serId, characticId = e.charactId, console.log("params id :" + JSON.stringify(e))
- }
- export function sendMessageToDevice(e, o) {
- console.log(TAG + " sendMessageToDevice() cmd:" + e[0]);
- var c = dataTool.strToBuffer(e[0]),
- r = 0;
- console.log(TAG + " 发送当前时间 :" + Date.parse(new Date)), wx.writeBLECharacteristicValue({
- deviceId: deviceId,
- serviceId: serviceId,
- characteristicId: characticId,
- value: c,
- success: function (c) {
- console.log(TAG + "wx. 发送成功 :" + Date.parse(new Date)), r < e.length - 1 ? (console.info(TAG + " wx. 已完成发送第====" + r + "===包====【" + e[r] + "】"), r++, onfire.fire("send", r)) : (onfire.clear(), console.info("wx. 所有数据发送完成 共:" + e.length + "包"), o.call(this, {
- code: successCode,
- err_msg: "数据发送成功",
- data: e,
- msg: ""
- }))
- },
- fail: function (e) {
- console.log(TAG + "wx. 发送失败 :" + Date.parse(new Date)), console.log("writeBLECharacteristicValue fail:" + JSON.stringify(e));
- var c = "";
- e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: c,
- data: null,
- msg: ""
- })
- }
- }), onfire.on("send", r => {
- console.log(TAG + " onfire.on sendMessageToDevice() cmd:" + e[r]);
- var s = dataTool.strToBuffer(e[r]);
- wx.writeBLECharacteristicValue({
- deviceId: deviceId,
- serviceId: serviceId,
- characteristicId: characticId,
- value: s,
- success: function (s) {
- console.log(TAG + "onfire.发送成功 :" + Date.parse(new Date)), r < e.length - 1 ? (console.info(TAG + " onfire. 已完成发送第====" + r + "===包====【" + c + "】"), r++, onfire.fire("send", r)) : (onfire.clear(), console.info("onfire.所有数据发送完成 共:" + e.length + "包"), o.call(this, {
- code: successCode,
- err_msg: "数据发送成功",
- data: e,
- msg: ""
- }))
- },
- fail: function (e) {
- console.log(TAG + "wx. 发送失败 :" + Date.parse(new Date)), console.log("writeBLECharacteristicValue fail:" + JSON.stringify(e));
- var c = "";
- e && (c = bleErrorMsg.bluetoothErrMsg(e.errCode)), o.call(this, {
- code: bleCode.bleErrorCode(),
- err_msg: c,
- data: null,
- msg: ""
- })
- }
- })
- })
- }
|