lanya.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. var BLData = {
  2. openOK: false,//蓝牙是否打开
  3. BLZT: false,//蓝牙状态是否可用
  4. BLSO: false,//蓝牙是否正在搜索
  5. TS: "开始初始化蓝牙模块...",
  6. }
  7. var dayinok=false;//连接成功时,记录为 true,检测到断开后记录为false;
  8. var lianjieing=false;//记录是否正在尝试连接
  9. var dayinji = {
  10. uuid: "",
  11. suuid: "",
  12. nuuid: ""
  13. }//已连接的蓝牙设备 uuid
  14. var BLSB = new Array();//记录已搜索到的蓝牙设备
  15. function GETData() {
  16. return BLData;
  17. }
  18. function chonglian(back){
  19. console.log(dayinok)
  20. if(dayinok==true){
  21. back('ok');
  22. return;
  23. }else{
  24. back('设备未打开蓝牙');
  25. return;
  26. }
  27. if(lianjieing==true){
  28. back("正在尝试连接请稍后");
  29. return;
  30. }
  31. try {
  32. var value = wx.getStorageSync('dayinji')
  33. if (value) {
  34. //重新初始化蓝牙
  35. wx.openBluetoothAdapter({
  36. success: function (res) {
  37. lianjie(value.uuid,back);//成功则开始连接
  38. },
  39. fail: function (res) {
  40. lianjieing=false;
  41. back('请先打开蓝牙');
  42. BLData.TS = "请先打开蓝牙开关!"
  43. }
  44. })
  45. }
  46. } catch (e) {
  47. console.log("读取缓存数据错误:", e)
  48. }
  49. }
  50. function OpenP() {
  51. BLSB = new Array();
  52. //先关闭再打开
  53. wx.closeBluetoothAdapter({
  54. success: function (res) {
  55. //初始化蓝牙模块
  56. wx.openBluetoothAdapter({
  57. success: function (res) {
  58. console.log(res.errMsg);
  59. if (res.errMsg == "openBluetoothAdapter:ok") {
  60. BLData.openOK = true
  61. BLData.TS = "蓝牙模块初始化成功!"
  62. GetBLZT();
  63. }
  64. else {
  65. BLData.TS = "请先打开蓝牙开关!"
  66. }
  67. },
  68. fail: function (res) {
  69. BLData.TS = "请先打开蓝牙开关!"
  70. }
  71. })
  72. }
  73. })
  74. //wx.closeBluetoothAdapter 关闭蓝牙模块,使其进入未初始化状态
  75. }
  76. function GetBLZT() {
  77. //获取本机蓝牙适配器状态
  78. wx.getBluetoothAdapterState({
  79. success: function (res) {
  80. if (res.available) {
  81. BLData.BLZT = true;
  82. BLData.TS = "蓝牙适配器状态可用";
  83. SOBL();
  84. }
  85. }
  86. })
  87. //监听本机蓝牙适配器变化(蓝牙状态变化时触发)
  88. wx.onBluetoothAdapterStateChange(function (res) {
  89. if (res.available) {
  90. BLData.BLZT = true;
  91. BLData.TS = "蓝牙适配器状态可用";
  92. SOBL();
  93. } else {
  94. BLData.BLZT = false;
  95. BLData.TS = "蓝牙适配器当前不可用";
  96. }
  97. })
  98. }
  99. //开始搜索蓝牙设备
  100. function SOBL() {
  101. BLData.TS = "开始搜索蓝牙设备";
  102. wx.startBluetoothDevicesDiscovery({
  103. success: function (res) {
  104. if (res.errMsg == "startBluetoothDevicesDiscovery:ok") {
  105. BLData.BLZT = true;
  106. BLData.TS = "开始搜索蓝牙设备";
  107. newSB();
  108. }
  109. }
  110. })
  111. }
  112. function newSB(){
  113. //监听搜索到新设备事件
  114. wx.onBluetoothDeviceFound(function (res) {
  115. //console.log(res)
  116. var SB = {
  117. name: "",
  118. id: "",
  119. tp: "",
  120. dis: false,
  121. LJ: "1",
  122. LJOK: "0"
  123. };
  124. BLData.TS = "搜索到蓝牙设备";
  125. console.log("localName:", res.devices[0].localName)
  126. if (res.devices[0].localName == "" || !res.devices[0].localName) {
  127. return;
  128. }
  129. if (chongfu(res.devices[0].deviceId)) {
  130. return;
  131. }
  132. SB.name = res.devices[0].localName;
  133. SB.id = res.devices[0].deviceId;
  134. var xh = res.devices[0].RSSI
  135. if (xh > -70) {
  136. SB.tp = "xinhao5"
  137. } else if (xh > -85) {
  138. SB.tp = "xinhao4"
  139. }
  140. else if (xh > -90) {
  141. SB.tp = "xinhao3"
  142. }
  143. else if (xh > -100) {
  144. SB.tp = "xinhao2"
  145. }
  146. else {
  147. SB.tp = "xinhao1"
  148. }
  149. SB.dis = false;
  150. SB.LJ = "0";
  151. BLSB.push(SB)
  152. //console.log(SB)
  153. })
  154. }
  155. function chongfu(uuid) {
  156. var iscf=false;
  157. for (var x in BLSB) {
  158. if (BLSB[x].id == uuid) {
  159. iscf=true
  160. }
  161. }
  162. return iscf;
  163. }
  164. function GETSB() {
  165. return BLSB;
  166. }
  167. function stopSO(){
  168. console.log("停止搜索:")
  169. wx.stopBluetoothDevicesDiscovery({
  170. success: function (res) {
  171. console.log("停止搜索成功:",res)
  172. wx.onBluetoothAdapterStateChange(function (res) {
  173. console.log(`adapterState changed, now is`, res)
  174. })
  175. }, fail: function (res) {
  176. console.log("停止搜索失败:", res)
  177. }
  178. })
  179. }
  180. function stopBL() {
  181. wx.stopBluetoothDevicesDiscovery({
  182. success: function (res) {
  183. wx.closeBluetoothAdapter({
  184. success: function (resx) {
  185. console.log(resx)
  186. }
  187. })
  188. }
  189. })
  190. }
  191. var lcs = 0;//尝试连接的次数
  192. var cgcs = 0;//成功连接的次数
  193. var jianting = false;
  194. function lianjie(uuid,back) {
  195. if(lianjieing==true){
  196. back("正在尝试连接,请稍后再试");
  197. return;
  198. }
  199. if (dayinji.uuid != "" && dayinji.uuid !=uuid)
  200. {
  201. duankai(dayinji.uuid,uuid,back);
  202. console.log("换一个")
  203. }//如果有已连接的打印,并且uuid 不等于现在要连接的uuid 则 先断开之前的打印机
  204. else
  205. {
  206. lcs = 0;
  207. cgcs = 0;
  208. lianjiego(uuid, back);//开始连接
  209. hidesb(uuid, "1")
  210. }
  211. }
  212. function hidesb(uuid, h) {
  213. for (var x in BLSB) {
  214. if (BLSB[x].id == uuid) {
  215. BLSB[x].LJ = h
  216. }
  217. }
  218. }
  219. function lianjiego(deviceId,back) {
  220. lianjieing = true;//记录开始尝试连接
  221. lcs += 1;
  222. console.log("第" + lcs + "次尝试连接")
  223. wx.createBLEConnection({
  224. deviceId: deviceId,
  225. success: function (res) {
  226. //stopSO();
  227. stopSO();
  228. cgcs += 1;
  229. console.log("连接成功.第" + lcs + "次:", res)
  230. if (cgcs == 1) { jtlj() }
  231. jianting = false;
  232. hidesb(deviceId, '2')
  233. getFW(deviceId,back);//开始获取服务
  234. }, fail: function (res) {
  235. console.log("连接失败:", res)
  236. dayinok=false;
  237. if (lcs > 5) {
  238. back('连接失败,请重新配置打印机');
  239. lianjieing=false;//已经不在尝试连接了
  240. hidesb(deviceId, "3");
  241. } else {
  242. lianjiego(deviceId,back);//再次尝试连接
  243. }
  244. }
  245. })
  246. }
  247. function duankai(deviceId,uuid,back) {
  248. hidesb(deviceId, "0")
  249. jianting = true;
  250. wx.closeBLEConnection({
  251. deviceId: deviceId,
  252. success: function (res) {
  253. dayinok = false;
  254. console.log("断开成功:",res)
  255. if(uuid!="no"){
  256. console.log("断开再连接")
  257. lcs = 0;
  258. cgcs = 0;
  259. lianjiego(uuid, back);//开始连接
  260. hidesb(uuid, "1")
  261. }
  262. }
  263. })
  264. }
  265. function jtlj() {
  266. wx.onBLEConnectionStateChange(function (res) {
  267. if (res.connected == false && jianting == false) {
  268. dayinok=false;//记录已断开
  269. console.log("监测到断开,自动重连");
  270. hidesb(res.deviceId, "1");
  271. lcs = 0;
  272. cgcs = 0;
  273. lianjiego(res.deviceId,function(res){
  274. //console.log("打印机断开了连接");
  275. });
  276. jianting = true;
  277. }
  278. })
  279. }
  280. var tzz = 0;
  281. //获取蓝牙设备所有 service(服务)
  282. function getFW(deviceId,back) {
  283. tzz=0;
  284. wx.getBLEDeviceServices({
  285. deviceId: deviceId,
  286. success: function (res) {
  287. console.log("所有服务:",res)
  288. for(var p in res.services) {
  289. if(tzz==1)break;
  290. getTZ(deviceId, res.services[p].uuid,back);//开始获取特征吗
  291. }
  292. }
  293. })
  294. }
  295. //获取蓝牙设备某个服务中的所有 characteristic(特征值)
  296. function getTZ(xuuid, xsuuid,back) {
  297. if(tzz==1)return;
  298. wx.getBLEDeviceCharacteristics({
  299. deviceId: xuuid,
  300. serviceId: xsuuid,
  301. success: function (res) {
  302. // console.log("所有特征值:",res)
  303. for (var p in res.characteristics) {
  304. var rr = res.characteristics[p].properties
  305. console.log(rr)
  306. //if (rr.notify && rr.read && rr.write) {
  307. // if (rr.notify && rr.write) {
  308. if (rr.write) {
  309. dayinji.uuid = xuuid;
  310. dayinji.suuid = xsuui3d;
  311. dayinji.nuuid = res.characteristics[p].uuid;
  312. console.log("AA当前打印机:uuid:"+dayinji.uuid+"suuid:"+dayinji.suuid+"nuuid:"+dayinji.nuuid);
  313. wx.setStorage({
  314. key: "dayinji",
  315. data: dayinji
  316. })
  317. dayinok = true;
  318. lianjieing=false;
  319. tzz = 1;
  320. back('ok');
  321. break;
  322. }
  323. }
  324. }
  325. })
  326. }
  327. function zdlianjie(){
  328. try {
  329. var value = wx.getStorageSync('dayinji')
  330. if (value) {
  331. wx.openBluetoothAdapter({
  332. success: function (res) {
  333. lianjie(value.uuid,function(re){});
  334. },
  335. fail: function (res) {
  336. BLData.TS = "请先打开蓝牙开关!"
  337. }
  338. })
  339. }
  340. } catch (e) {
  341. console.log("读取缓存数据错误:",e)
  342. }
  343. }
  344. module.exports = {
  345. dayinji: dayinji,//连接的打印机信息
  346. OpenPrint: OpenP,
  347. GETData: GETData,
  348. GETSB: GETSB,
  349. stopBL: stopBL,
  350. lianjie: lianjie,
  351. duankai: duankai,
  352. zdlj:zdlianjie,
  353. chonglian:chonglian,
  354. };