uni-app微信小程序获取蓝牙列表

本文讲述了在微信小程序中点击“申请蓝牙”按钮后,如何使用API获取蓝牙设备列表,以及开发者在真机调试中的遇到的问题,包括遮罩效果的实现和微信开发者工具预览的限制。
摘要由CSDN通过智能技术生成

小程序点击“申请蓝牙”按钮,调用api获取蓝牙列表。

微信开发者工具需要真机调试才能用,开发者工具预览的没用。

本来想在点击按钮后增加一个遮罩,蓝牙列表查完后关掉。但是没能有效实现,大神可以补充下。

<template>
  <view style="height: 100vh; padding-top: 80rpx;">
    <button @click="applyBluetooth" style="margin-top: 80px;">申请蓝牙</button>
    <view class="table-body">
      <view class="table-item" v-for="(item,index) in bluetoothList" :key="index">
        {{item.name}}
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      bluetoothList: []
    }
  },
  methods: {
    /** 申请蓝牙 */
    async applyBluetooth() {
      await this.$modal.loading("正在搜索蓝牙设备");
      await this.openBluetoothAdapter(this).then(() => {
        this.$modal.closeLoading()
      });
    },
    /** 初始化蓝牙模块 */
    async openBluetoothAdapter(_this) {
      await uni.openBluetoothAdapter({
        // 蓝牙已打开
        success: (res) => {
          // 获取本机蓝牙适配器状态
          _this.getBluetoothAdapterState(_this);
        },
        // 蓝牙未打开
        fail: err => {
          this.$modal.msg("请检查手机蓝牙是否打开");
        }
      })
    },
    /** 获取本机蓝牙适配器状态 */
    async getBluetoothAdapterState(_this) {
      await uni.getBluetoothAdapterState({
        success: (res) => {
          // 开始搜索蓝牙设备
          _this.startBluetoothDevicesDiscovery(_this);
        },
        fail: err => {
          this.$modal.msg("请检查手机蓝牙是否打开");
        }
      });
    },
    /** 开始搜寻附近的蓝牙外围设备 */
    async startBluetoothDevicesDiscovery(_this) {
      await uni.startBluetoothDevicesDiscovery({
        success: (res) => {
          // 延迟一秒获取蓝牙列表
          setTimeout(() => {
            // 获取设备列表
            _this.getBluetoothDevices(_this);
          }, 1000);
        },
        fail: (err) => {
          console.log(err);
        }
      })
    },
    /** 获取在蓝牙模块生效期间所有已发现的蓝牙设备 */
    async getBluetoothDevices(_this) {
      await uni.getBluetoothDevices({
        success: function (res) {
          _this.bluetoothList = res.devices;
        },
        fail: function () {
          this.$modal.msg("搜索蓝牙设备失败或附件暂无开启的蓝牙设备");
        },
        complete: function () {
          // 停止搜寻附近的蓝牙外围设备
          _this.stopBluetoothDevicesDiscovery();
        }
      })
    },
    /** 停止搜寻附近的蓝牙外围设备 */
    async stopBluetoothDevicesDiscovery() {
      await uni.stopBluetoothDevicesDiscovery({
        success(res) {
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.table-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 30rpx;
  height: calc(100vh - 350rpx);
  overflow: scroll;

  .table-item {
    width: 650rpx;
    height: 90rpx;
    line-height: 90rpx;
    background: #FFFFFF;
    box-shadow: 0rpx 0rpx 30rpx 14rpx rgba(236, 235, 236, 0.25);
    border-radius: 45rpx;
    padding-left: 50rpx;
    margin-bottom: 20rpx;
  }
}
</style>
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值