123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // index.js
- // 获取应用实例
- const getBle = require("../../utils/ble");
- const app = getApp();
- const ble = getBle();
- const connectStateMap = {
- 0: {text:'未连接',color:"red"},
- 1: {text:'扫描中',color:"greenYellow"},
- 2: {text:'连接中',color:"#d8e84d"},
- 3: {text:'已连接',color:"#00ff00"}
- }
- const bleStateEnum = {
- notConnect: 0,
- searching: 1,
- connecting: 2,
- connected: 3,
- }
- Page({
- data: {
- connectStateMap,
- ble:{
- state: 0,
- init: false,
- },
- remoteDev:{
- connect: false,
- devName: "",
- },
- bleDevs:[],// 搜索到的蓝牙设备列表
- motto: 'Hello World',
- userInfo: {},
- hasUserInfo: false,
- canIUse: wx.canIUse('button.open-type.getUserInfo'),
- canIUseGetUserProfile: false,
- canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
- },
- async onShow(){
- // 初始化蓝牙
- try {
- console.log("init ble");
- await ble.initBle();
- this.setData({
- ble: {...this.data.ble,init: true},
- })
- } catch (error) {
- console.log(error);
- this.setData({
- ble: {...this.data.ble,init: false},
- })
- wx.showModal({
- title: '初始化蓝牙失败',
- content: error.errMsg,
- success (res) {
- if (res.confirm) {
- console.log('用户点击确定')
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- }
- },
- // 事件处理函数
- bindViewTap() {
- wx.navigateTo({
- url: '../logs/logs'
- })
- },
- async searchDevice(){
- console.log("搜索 蓝牙设备");
- if(!this.data.ble.init){
- return wx.showModal({
- title: '蓝牙错误',
- content: "无法启用蓝牙!!!",
- success (res) { }
- })
- }
-
- this.setData({
- ble: {...this.data.ble,state: 1},
- })
- },
- tapState(){
- switch(this.data.ble.state){
- case bleStateEnum.notConnect:
- this.searchDevice();
- break;
- case bleStateEnum.searching:
- break;
- case bleStateEnum.connecting:
- break;
- case bleStateEnum.connected:
- break;
- }
- },
- onLoad() {
- if (wx.getUserProfile) {
- this.setData({
- canIUseGetUserProfile: true
- })
- }
- },
- getUserProfile(e) {
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
- wx.getUserProfile({
- desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: (res) => {
- console.log(res)
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- }
- })
- },
- getUserInfo(e) {
- // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
- console.log(e)
- this.setData({
- userInfo: e.detail.userInfo,
- hasUserInfo: true
- })
- }
- })
|