123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- "use strict";
- var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (e) {
- return typeof e
- }
- : function (e) {
- return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
- }
- , _kcUtils = require("./kcUtils.js")
- , _kcUtils2 = _interopRequireDefault(_kcUtils);
- function _interopRequireDefault(e) {
- return e && e.__esModule ? e : {
- default: e
- }
- }
- var timeID, ListenFlag = void 0, DataListenerCallBack = void 0, connectedDeviceId = "", foundDevices = [], TAG_FUNCTION = "function", services = "", androidServices = "0000FEE7-0000-1000-8000-00805F9B34FB", iosServices = "FEE7", notifyServiceId = "", androidNotifyServiceId = "0000FEC8-0000-1000-8000-00805F9B34FB", iosNotifyServiceId = "FEC8", writeServiceId = "", androidWriteServiceId = "0000FEC7-0000-1000-8000-00805F9B34FB", iosWriteServiceId = "FEC7", readServiceId = "", androidReadServiceId = "0000FEC9-0000-1000-8000-00805F9B34FB", iosReadServiceId = "FEC9", device = {}, SetoutTimer = 0,
- unavailableServices = true;
- function reallyScanConnect(s) {
- wx.closeBluetoothAdapter(),
- _kcUtils2.default.showLog("/***********Runing :: Do reallyScanConnect() begin *************/"),
- foundDevices = [],
- wx.openBluetoothAdapter({
- success: function (e) {
- wx.startBluetoothDevicesDiscovery({
- services: [],
- success: function (e) {
- wx.onBluetoothDeviceFound(function (e) {
- for (var i = 0; i < e.devices.length; i++) {
- for (var c = !1, t = 0; t < foundDevices.length; t++)
- if (e.devices[i].deviceId == foundDevices[t].deviceId) {
- c = !0;
- break
- }
- if (_kcUtils2.default.showLog("connectedDeviceName", e.devices[i].name),
- 0 == c) {
- foundDevices.push(e.devices[i]);
- var n = e.devices[i].name;
- if (-1 != n.indexOf("ETC-KC-")) {
- connectedDeviceId = e.devices[i].deviceId,
- _kcUtils2.default.showLog("connectedDeviceId", e.devices);
- var o = {};
- o.device_name = n,
- o.device_id = connectedDeviceId,
- wx.stopBluetoothDevicesDiscovery({
- success: function (e) {
- _kcUtils2.default.showLog("停止扫描,开始连接"),
- reallyConnect(o, function (e) {
- (void 0 === s ? "undefined" : _typeof(s)) == TAG_FUNCTION && s(e)
- })
- },
- fail: function (e) {
- _kcUtils2.default.showError("stopBluetoothDevicesDiscovery fail", e)
- }
- });
- break
- }
- }
- }
- })
- },
- fail: function (e) {
- _kcUtils2.default.showError("startBluetoothDevicesDiscovery fail", e)
- }
- })
- },
- fail: function (e) {
- _kcUtils2.default.showError("openBluetoothAdapter fail", e)
- }
- }),
- wx.onBLEConnectionStateChange(function (e) {
- 0 == e.connected && (wx.closeBLEConnection({
- deviceId: e.deviceId,
- success: function (e) {
- _kcUtils2.default.showLog("closeBLEConnection:", e)
- }
- }),
- wx.closeBluetoothAdapter({
- success: function (e) {
- _kcUtils2.default.showLog("closeBluetoothAdapter", e)
- }
- })),
- console.error("device " + e.deviceId + " state has changed, connected: " + e.connected)
- })
- }
- function reallyConnect(device, callback) {
- console.log("WeiXin ble SDK 20240606");
- var result = {
- code: -2,
- msg: '连接失败'
- };
- var deviceName = device.device_name;
- console.log("name", deviceName);
- if (deviceName.includes("ETC-KC-") || deviceName.includes("K")) {
- connectedDeviceId = device.device_id;
- wx.createBLEConnection({
- deviceId: connectedDeviceId,
- success: function (res) {
- console.log("已连接,开始使能服务", connectedDeviceId);
- _enableService(function (serviceResult) {
- console.log("serviceResult:", serviceResult);
- if (serviceResult.serviceCode === 0) {
- console.log("2.连接成功,并使能成功");
- result.code = 0;
- result.msg = "连接成功";
- } else {
- console.log("2.连接成功,但服务使能失败");
- }
- if (typeof callback === TAG_FUNCTION) callback(result);
- });
- },
- fail: function (err) {
- console.error("createBLEConnection fail", err);
- if (typeof callback === TAG_FUNCTION) callback(result);
- }
- });
- } else {
- result.msg = "设备验证失败";
- if (typeof callback === TAG_FUNCTION) callback(result);
- }
- }
-
- function _setPlatformServicesId() {
- const infoa = wx.getSystemInfoSync();
- console.log(`%cplatform ${JSON.stringify(infoa.platform, null, 2)}`, "color: green;");
- }
-
- function containsServiceInPrefix(serviceId) {
- var ret = false, serviceTemp;
- // 确保将字符串转换为大写,以避免大小写敏感问题
- serviceTemp = serviceId.toUpperCase();
-
- // 获取前4个字符和前8个字符
- const prefix4 = serviceTemp.substring(0, 4);
- const prefix8 = serviceTemp.substring(0, 8);
-
- // 判断是否包含 "FEE7"
- ret = prefix4.includes("FEE7") || prefix8.includes("FEE7");
- if (ret) {
- services = serviceId;
- }
- return ret;
- }
-
- function containsNotifyInPrefix(service) {
- var ret = false, serviceTemp;
- // 确保将字符串转换为大写,以避免大小写敏感问题
- serviceTemp = service.toUpperCase();
-
- // 获取前4个字符和前8个字符
- const prefix4 = serviceTemp.substring(0, 4);
- const prefix8 = serviceTemp.substring(0, 8);
-
- // 判断是否包含 "FEC8"
- ret = prefix4.includes("FEC8") || prefix8.includes("FEC8");
- if (ret) {
- notifyServiceId = service;
- }
- return ret;
- }
-
- function containsWriteInPrefix(service) {
- var ret = false, serviceTemp;
- // 确保将字符串转换为大写,以避免大小写敏感问题
- serviceTemp = service.toUpperCase();
- // 获取前4个字符和前8个字符
- const prefix4 = serviceTemp.substring(0, 4);
- const prefix8 = serviceTemp.substring(0, 8);
-
- // 判断是否包含 "FEC7"
- ret = prefix4.includes("FEC7") || prefix8.includes("FEC7");
- if (ret) {
- writeServiceId = service;
- }
- return ret;
- }
-
- function containsReadInPrefix(service) {
- var ret = false, serviceTemp;
- // 确保将字符串转换为大写,以避免大小写敏感问题
- serviceTemp = service.toUpperCase();
- // 获取前4个字符和前8个字符
- const prefix4 = serviceTemp.substring(0, 4);
- const prefix8 = serviceTemp.substring(0, 8);
- // 判断是否包含 "FEC9"
- ret = prefix4.includes("FEC9") || prefix8.includes("FEC9");
- if (ret) {
- readServiceId = service;
- }
- return ret;
- }
-
- function _enableService(callback) {
- var result = {};
- _setPlatformServicesId();
- wx.getBLEDeviceServices({
- deviceId: connectedDeviceId,
- success: function (res) {
- for (var i = 0; i < res.services.length; i++) {
- console.log(res.services[i]);
- var uuid = res.services[i].uuid;
- if (containsServiceInPrefix(uuid)) {
- wx.getBLEDeviceCharacteristics({
- deviceId: connectedDeviceId,
- serviceId: uuid,
- success: function (charRes) {
- var matchedChars = charRes.characteristics.filter(char => containsNotifyInPrefix(char.uuid) || containsWriteInPrefix(char.uuid)).length;
- console.log("matchedChars", matchedChars);
- if (matchedChars < 2) {
- result.serviceCode = -1;
- result.serviceInfo = "getBLEDeviceCharacteristics temp<2!";
- if (typeof callback === "function") callback(result);
- } else {
- wx.notifyBLECharacteristicValueChange({
- deviceId: connectedDeviceId,
- serviceId: services,
- characteristicId: notifyServiceId,
- state: true,
- success: function (res) {
- },
- fail: function (res) {
- },
- complete: function (res) {
- console.log("res:", res);
- if (res.errno === 0) {
- _onBLECharacteristicValueChange();
- result.serviceCode = 0;
- result.serviceInfo = "enable success!";
- } else {
- result.serviceCode = -2;
- result.serviceInfo = "notifyBLECharacteristicValueChange fail!";
- }
- if (typeof callback === "function") callback(result);
- }
- });
- }
- },
- fail: function () {
- console.error("getBLEDeviceCharacteristics fail");
- result.serviceCode = -3;
- result.serviceInfo = "getBLEDeviceCharacteristics fail!";
- if (typeof callback === "function") callback(result);
- }
- });
- unavailableServices = false;
- break;
- }
- }
- console.log("unavailableServices", unavailableServices);
- },
- fail: function () {
- result.serviceCode = -4;
- result.serviceInfo = "getBLEDeviceServices fail!";
- if (typeof callback === "function") callback(result);
- }
- });
- }
- function reallyDisConnect(callBack) {
- console.log("/***********Runing :: Do reallyDisConnect() begin *************/");
- var c = {
- code: 0,
- msg: '已断连'
- };
- wx.closeBLEConnection({
- deviceId: connectedDeviceId,
- success: function (e) {
- connectedDeviceId = "";
- console.log("closeBLEConnection success", e);
- },
- fail: function (e) {
- console.error("closeBLEConnection fail", e);
- },
- complete: function (e) {
- (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
- }
- });
- }
- function _writeBLECharacteristicValue(e, i) {
- var c = {};
- wx.writeBLECharacteristicValue({
- deviceId: connectedDeviceId,
- serviceId: services,
- characteristicId: writeServiceId,
- value: e,
- success: function (e) {
- _kcUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg),
- c.serviceCode = 0,
- c.serviceInfo = e.errMsg,
- (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(c)
- }
- })
- }
- function _onBLECharacteristicValueChange() {
- wx.onBLECharacteristicValueChange(function (e) {
- _kcUtils2.default.showLog("有回复"),
- 1 == ListenFlag && (void 0 === DataListenerCallBack ? "undefined" : _typeof(DataListenerCallBack)) == TAG_FUNCTION && DataListenerCallBack(e.value)
- })
- }
- function _SetDataListenerCallBack(e, i) {
- 1 == e ? (ListenFlag = !0,
- DataListenerCallBack = i) : 0 == e && (ListenFlag = !1)
- }
- function disconnectDeviceBluetooth(callBack) {
- console.log("/***********Runing :: Do disconnectDeviceBluetooth() begin *************/");
- console.log("connectedDeviceId是空", connectedDeviceId == "");
- var c = {
- code: 0,
- msg: '已断开'
- };
- if (connectedDeviceId != "") {
- wx.closeBLEConnection({
- deviceId: connectedDeviceId,
- success: function (e) {
- connectedDeviceId = "";
- console.log("closeBLEConnection success", e);
- wx.closeBluetoothAdapter({
- success: function (e) {
- console.log("closeBluetoothAdapter success", e);
- },
- fail: function (e) {
- console.error("closeBluetoothAdapter fail", e);
- },
- complete: function (e) {
- (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
- }
- });
- }
- });
- }else{
- wx.closeBluetoothAdapter({
- success: function (e) {
- console.log("closeBluetoothAdapter success", e);
- },
- fail: function (e) {
- console.error("closeBluetoothAdapter fail", e);
- },
- complete: function (e) {
- (void 0 === callBack ? "undefined" : _typeof(callBack)) == TAG_FUNCTION && callBack(c)
- }
- });
- }
- }
- module.exports = {
- reallyConnect: reallyConnect,
- reallyScanConnect: reallyScanConnect,
- reallyDisConnect: reallyDisConnect,
- _writeBLECharacteristicValue: _writeBLECharacteristicValue,
- _SetDataListenerCallBack: _SetDataListenerCallBack,
- disconnectDeviceBluetooth: disconnectDeviceBluetooth,
- };
|