123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- "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
- },
- _wjUtils = require("./wjUtils.js"),
- _wjUtils2 = _interopRequireDefault(_wjUtils);
-
-
- const common_vendor = require("../../../common/vendor.js");
- var uni = common_vendor.index
-
- function _interopRequireDefault(e) {
- return e && e.__esModule ? e : {
- default: e
- }
- }
- var connectedDeviceId, timeID, onDisconnect, ListenFlag = void 0,
- DataListenerCallBack = void 0,
- foundDevices = [],
- TAG_FUNCTION = "function",
- services = "0000FEE7-0000-1000-8000-00805F9B34FB",
- notifyServiceId = "0000FEC8-0000-1000-8000-00805F9B34FB",
- writeServiceId = "0000FEC7-0000-1000-8000-00805F9B34FB";
-
- function reallyscanConnect(s, r) {
- var d = {};
- _wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"), foundDevices = [], wx
- .closeBluetoothAdapter(), uni.openBluetoothAdapter({
- success: function(e) {
- uni.startBluetoothDevicesDiscovery({
- services: [],
- success: function(e) {
- uni.onBluetoothDeviceFound(function(e) {
- for (var i = 0; i < e.devices.length; i++) {
- for (var n = !1, c = 0; c < foundDevices.length; c++)
- if (e.devices[i].deviceId == foundDevices[c].deviceId) {
- n = !0;
- break
- } var t = e.devices[i].name;
- if (_wjUtils2.default.showLog("connectedDeviceName", e
- .devices[i].name), 0 == n && "" != t && null != t &&
- (foundDevices.push(e.devices[i]), -1 != t.indexOf(
- "WJ") || -1 != t.indexOf("WanJi"))) {
- connectedDeviceId = e.devices[i].deviceId;
- var o = {};
- o.name = t, o.deviceId = connectedDeviceId, _wjUtils2
- .default.showLog("connectedDeviceId", e.devices), wx
- .stopBluetoothDevicesDiscovery({
- success: function(e) {
- _wjUtils2.default.showLog(
- "停止扫描,开始连接"), null !=
- timeID && (clearTimeout(timeID),
- timeID = null),
- reallyConnect(o, function(e) {
- (void 0 === s ?
- "undefined" :
- _typeof(s)) ==
- TAG_FUNCTION && s(e)
- }, function(e) {
- (void 0 === r ?
- "undefined" :
- _typeof(r)) ==
- TAG_FUNCTION && r(e)
- })
- },
- fail: function(e) {
- _wjUtils2.default.showError(
- "stopBluetoothDevicesDiscovery:" +
- e), d.serviceCode = 1003, d
- .serviceInfo =
- "stopBluetoothDevicesDiscovery fail!",
- (void 0 === s ? "undefined" :
- _typeof(s)) ==
- TAG_FUNCTION && s(d)
- }
- });
- break
- }
- }
- })
- },
- fail: function(e) {
- _wjUtils2.default.showError("scanerror:" + e), d.serviceCode = 1002, d
- .serviceInfo = "scanerror fail!", (void 0 === s ? "undefined" : _typeof(
- s)) == TAG_FUNCTION && s(d)
- }
- }), timeID = setTimeout(function() {
- uni.closeBluetoothAdapter(), _wjUtils2.default.showError("scan timeout"), d
- .serviceCode = 100100, d.serviceInfo = "scan timeout fail!", (void 0 === s ?
- "undefined" : _typeof(s)) == TAG_FUNCTION && s(d)
- }, 5e3)
- },
- fail: function(e) {
- _wjUtils2.default.showError("openadapter:" + e), d.serviceCode = 1001, d.serviceInfo =
- "openadapter fail!", (void 0 === s ? "undefined" : _typeof(s)) == TAG_FUNCTION && s(d)
- }
- })
- }
-
- function reallyConnect(e, i, n) {
- _wjUtils2.default.showLog("start reallyConnect,20190830001,V0.0.4"), _onBLEConnectionStateChange(), onDisconnect =
- n;
- var c = {},
- t = e.name;
- _wjUtils2.default.showLog("name:", t), -1 != t.indexOf("WJ") || -1 != t.indexOf("WanJi") || -1 != t.indexOf("ZZ") ?
- (connectedDeviceId = e.deviceId, uni.createBLEConnection({
- deviceId: connectedDeviceId,
- success: function(e) {
- _wjUtils2.default.showLog("已连接,开始使能服务", connectedDeviceId), _enableService(function(e) {
- 0 == e.serviceCode ? (_wjUtils2.default.showLog("已连接,并使能成功"), c.serviceCode = 0,
- c.serviceInfo = "connected", (void 0 === i ? "undefined" : _typeof(
- i)) == TAG_FUNCTION && i(c), (void 0 === onDisconnect ?
- "undefined" :
- _typeof(onDisconnect)) == TAG_FUNCTION && onDisconnect(c)) : ((c =
- e).serviceInfo = "连接成功,但服务使能失败!", (void 0 === i ? "undefined" :
- _typeof(i)) == TAG_FUNCTION && i(c))
- }), timeID = setTimeout(function() {
- uni.closeBLEConnection({
- deviceId: connectedDeviceId,
- success: function(e) {}
- }), uni.closeBluetoothAdapter(), _wjUtils2.default.showError(
- "scan timeout"),
- c.serviceCode = 100100, c.serviceInfo = "scan timeout fail!", (void 0 ===
- i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(c)
- }, 5e3)
- },
- fail: function(e) {
- _wjUtils2.default.showError("creatcon error:" + e), c.serviceCode = 1005, c.serviceInfo =
- "creatcon error fail!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(
- c)
- }
- })) : (c.serviceCode = 1004, c.serviceInfo = "device's name error", (void 0 === i ? "undefined" : _typeof(i)) ==
- TAG_FUNCTION && i(c))
- }
-
- function _enableService(t) {
- var o = {};
- uni.getBLEDeviceServices({
- deviceId: connectedDeviceId,
- success: function(e) {
- for (var i = 0; i < e.services.length; i++) {
- var n = e.services[i].uuid;
- n == services && uni.getBLEDeviceCharacteristics({
- deviceId: connectedDeviceId,
- serviceId: n,
- success: function(e) {
- for (var i = 0, n = 0; n < e.characteristics.length; n++) {
- var c = e.characteristics[n].uuid;
- c == notifyServiceId ? i++ : c == writeServiceId && i++
- }
- i < 2 ? (o.serviceCode = -1, o.serviceInfo =
- "getBLEDeviceCharacteristics temp<2!", (void 0 === t ?
- "undefined" : _typeof(t)) == TAG_FUNCTION && t(o)) : wx
- .notifyBLECharacteristicValueChange({
- deviceId: connectedDeviceId,
- serviceId: services,
- characteristicId: notifyServiceId,
- state: !0,
- success: function(e) {
- _onBLECharacteristicValueChange(), null != timeID &&
- (clearTimeout(timeID), timeID = null), o
- .serviceCode = 0, o.serviceInfo =
- "enable success!", (void 0 === t ? "undefined" :
- _typeof(t)) == TAG_FUNCTION && t(o)
- },
- fail: function() {
- o.serviceCode = 1008, o.serviceInfo =
- "notifyBLECharacteristicValueChange fail!", (
- void 0 === t ? "undefined" : _typeof(t)) ==
- TAG_FUNCTION && t(o)
- }
- })
- },
- fail: function() {
- o.serviceCode = 1007, o.serviceInfo =
- "getBLEDeviceCharacteristics fail!", (void 0 === t ? "undefined" :
- _typeof(t)) == TAG_FUNCTION && t(o)
- }
- })
- }
- },
- fail: function() {
- o.serviceCode = 1006, o.serviceInfo = "getBLEDeviceServices fail!", (void 0 === t ?
- "undefined" : _typeof(t)) == TAG_FUNCTION && t(o)
- }
- })
- }
-
- function _onBLEConnectionStateChange() {
- var i = {};
- uni.onBLEConnectionStateChange(function(e) {
- console.error("device " + e.deviceId + " state has changed, connected: " + e.connected), 0 == e
- .connected && (uni.closeBLEConnection({
- deviceId: connectedDeviceId,
- success: function(e) {}
- }), uni.closeBluetoothAdapter(), i.serviceCode = 1, i.serviceInfo = "disconnected", (void 0 ===
- onDisconnect ? "undefined" : _typeof(onDisconnect)) == TAG_FUNCTION && onDisconnect(i))
- })
- }
-
- function reallyDisConnect(i) {
- var n = {};
- uni.closeBLEConnection({
- deviceId: connectedDeviceId,
- success: function(e) {
- _wjUtils2.default.showLog("closeBLEConnection:", e)
- }
- }), uni.closeBluetoothAdapter({
- success: function(e) {
- _wjUtils2.default.showLog("closeBluetoothAdapter", e), n.serviceCode = 0, n.serviceInfo =
- "断开蓝牙成功!", (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(n)
- }
- })
- }
-
- function _writeBLECharacteristicValue(e, i) {
- var n = {};
- uni.writeBLECharacteristicValue({
- deviceId: connectedDeviceId,
- serviceId: services,
- characteristicId: writeServiceId,
- value: e,
- success: function(e) {
- _wjUtils2.default.showLog("writeBLECharacteristicValue success", e.errMsg), n.serviceCode = 0, n
- .serviceInfo = e.errMsg, (void 0 === i ? "undefined" : _typeof(i)) == TAG_FUNCTION && i(n)
- }
- })
- }
-
- function _onBLECharacteristicValueChange() {
- uni.onBLECharacteristicValueChange(function(e) {
- _wjUtils2.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)
- }
- module.exports = {
- reallyscanConnect: reallyscanConnect,
- reallyDisConnect: reallyDisConnect,
- reallyConnect: reallyConnect,
- _writeBLECharacteristicValue: _writeBLECharacteristicValue,
- _SetDataListenerCallBack: _SetDataListenerCallBack
- };
|