|
- // 示例demo
- const plugin = requirePlugin('ppScale-plugin')
- var app = getApp();
- Page({
- data: {
- deviceslist: [{
- name: "LFSmart Scale",
-
- deviceId: "CF:E6:16:22:82:28"
- }],
-
- },
- onHide() {
-
- },
- onLoad() {
- let that = this;
- //监听蓝牙打开状态
- wx.onBluetoothAdapterStateChange(function (res) {
- console.log('蓝牙适配器打开状态变化', res)
- // 如果蓝牙从关闭到打开并且 deviceslist 为空,则重新初始化蓝牙
- if (res.available && !that.data.deviceslist) {
- that.initBlue();
- }
- })
- //监听蓝牙连接状态
- wx.onBLEConnectionStateChange(function (res) {
- // 该方法回调中可以用于处理连接意外断开等异常情况
- console.log(`设备蓝牙连接状态`, res);
- // 如果蓝牙断开
- if (!res.connected) {
- // 关闭蓝牙连接事件,再重新初始化连接?
- // that.closeBlue();
- wx.showToast({
- title: '蓝牙已断开',
- icon: 'error'
- })
- /* setTimeout(() => {
- that.initBlue();
- }, 1500) */
-
- } else {
- wx.showToast({
- title: '蓝牙已连接',
- icon: 'success'
- })
- }
- })
- },
- onShow() {
- // 初始化蓝牙
- this.initBlue()
- },
- onUnload() {
- // 业务结束后需要停止蓝牙
- this.closeBlue();
-
- },
- closeBlue() {
- wx.closeBluetoothAdapter();
- wx.offBluetoothAdapterStateChange()
- },
- initBlue() {
- let that = this;
- /* that.setData({
- deviceslist: ''
- }) */
- console.log("初始化蓝牙");
- wx.openBluetoothAdapter({
- mode: 'peripheral',
- success: (res) => {
- console.log('openBluetoothAdapter', res)
- // 初始化成功后开始搜索周边蓝牙列表
- wx.showLoading({
- title: '搜索中',
- })
- wx.startBluetoothDevicesDiscovery({
- services: [], // 可指定服务UUID筛选要搜索的设备
- success: function (res) {
- if (res.errno === 0) {
- console.log('蓝牙设备搜索成功');
-
- setTimeout(() => {
-
- wx.getBluetoothDevices({
- success: (resu) => {
- console.log('getBluetoothDevices', resu)
- // resu.device 为蓝牙列表
- wx.hideLoading();
- if (resu.devices.length > 0) {
- wx.showToast({
- title: '搜索成功',
- icon: 'success'
- })
- that.setData({
- deviceslist: resu.devices
- });
- // 此时再监听搜索到新设备的事件
- wx.onBluetoothDeviceFound(function(res) {
- var devices = res.devices;
- console.log('找到一个新设备', devices)
- that.data.deviceslist.push(devices[0]);
- that.setData({
- deviceslist: that.data.deviceslist
- });
- })
-
-
- } else {
-
- wx.showModal({
- title: '温馨提示',
- content: `暂无搜索到可用设备`,
- showCancel: false,
- })
- }
-
- }
- })
- // // 搜索到设备后停止搜索
- // wx.stopBluetoothDevicesDiscovery({
- // success: () => {
- // console.log("搜索到设备,停止搜索");
- // }
- // });
-
- }, 1500)
- }
-
- },
- fail: function (err) {
- wx.hideLoading();
- wx.showModal({
- title: '温馨提示',
- content: `蓝牙设备搜索失败`,
- showCancel: false,
- })
- console.error('蓝牙设备搜索失败', err)
- }
- })
- },
- fail: (fail) => {
- wx.showModal({
- title: '温馨提示',
- content: `请打开蓝牙`,
- showCancel: false,
- })
- }
- });
-
- },
- // 选择设备
- selItem(res) {
- var that = this;
- var devItem = res.currentTarget.dataset.item;
- var device = this.data.deviceslist.find(item => item.deviceId === devItem.deviceId)
- app.globalData.selmac = device;
- that.data.selmac = device;
- that.setData({
- selmac: device
- })
- // 本地缓存连接的蓝牙设备信息
- console.log("this.globalData", app.globalData.selmac)
-
- },
- refresh: function (res) {
- this.initBlue();
- console.log("刷新", );
- },
- doback(res) {
- if (app.globalData.selmac) {
- // 进入详情前先停止搜索设备,
- wx.stopBluetoothDevicesDiscovery({
- success: () => {
- console.log("开始连接到设备,停止搜索");
- }
- });
- wx.navigateTo({
- url: '/pages/devicedetail/index'
- })
- } else if (this.data.deviceslist.length <= 0) {
- wx.showModal({
- title: '温馨提示',
- content: `搜索不到设备,请重试获取打开蓝牙`,
- showCancel: false,
- })
- } else {
- wx.showModal({
- title: '温馨提示',
- content: `请选择上面要连接的设备`,
- showCancel: false,
- })
- console.log('请选择设备')
- }
- },
-
-
-
-
-
-
-
- })
|