index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // index.js
  2. // 获取应用实例
  3. const getBle = require("../../utils/ble");
  4. const app = getApp();
  5. const ble = getBle();
  6. const connectStateMap = {
  7. 0: {text:'未连接',color:"red"},
  8. 1: {text:'扫描中',color:"greenYellow"},
  9. 2: {text:'连接中',color:"#d8e84d"},
  10. 3: {text:'已连接',color:"#00ff00"}
  11. }
  12. const bleStateEnum = {
  13. notConnect: 0,
  14. searching: 1,
  15. connecting: 2,
  16. connected: 3,
  17. }
  18. Page({
  19. data: {
  20. connectStateMap,
  21. ble:{
  22. state: 0,
  23. init: false,
  24. },
  25. remoteDev:{
  26. connect: false,
  27. devName: "",
  28. },
  29. bleDevs:[],// 搜索到的蓝牙设备列表
  30. motto: 'Hello World',
  31. userInfo: {},
  32. hasUserInfo: false,
  33. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  34. canIUseGetUserProfile: false,
  35. canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  36. },
  37. async onShow(){
  38. // 初始化蓝牙
  39. try {
  40. console.log("init ble");
  41. await ble.initBle();
  42. this.setData({
  43. ble: {...this.data.ble,init: true},
  44. })
  45. } catch (error) {
  46. console.log(error);
  47. this.setData({
  48. ble: {...this.data.ble,init: false},
  49. })
  50. wx.showModal({
  51. title: '初始化蓝牙失败',
  52. content: error.errMsg,
  53. success (res) {
  54. if (res.confirm) {
  55. console.log('用户点击确定')
  56. } else if (res.cancel) {
  57. console.log('用户点击取消')
  58. }
  59. }
  60. })
  61. }
  62. },
  63. // 事件处理函数
  64. bindViewTap() {
  65. wx.navigateTo({
  66. url: '../logs/logs'
  67. })
  68. },
  69. async searchDevice(){
  70. console.log("搜索 蓝牙设备");
  71. if(!this.data.ble.init){
  72. return wx.showModal({
  73. title: '蓝牙错误',
  74. content: "无法启用蓝牙!!!",
  75. success (res) { }
  76. })
  77. }
  78. this.setData({
  79. ble: {...this.data.ble,state: 1},
  80. })
  81. },
  82. tapState(){
  83. switch(this.data.ble.state){
  84. case bleStateEnum.notConnect:
  85. this.searchDevice();
  86. break;
  87. case bleStateEnum.searching:
  88. break;
  89. case bleStateEnum.connecting:
  90. break;
  91. case bleStateEnum.connected:
  92. break;
  93. }
  94. },
  95. onLoad() {
  96. if (wx.getUserProfile) {
  97. this.setData({
  98. canIUseGetUserProfile: true
  99. })
  100. }
  101. },
  102. getUserProfile(e) {
  103. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  104. wx.getUserProfile({
  105. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  106. success: (res) => {
  107. console.log(res)
  108. this.setData({
  109. userInfo: res.userInfo,
  110. hasUserInfo: true
  111. })
  112. }
  113. })
  114. },
  115. getUserInfo(e) {
  116. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  117. console.log(e)
  118. this.setData({
  119. userInfo: e.detail.userInfo,
  120. hasUserInfo: true
  121. })
  122. }
  123. })