1.获取蓝牙列表 bleList.vue
<template><view><button @touchstart="startSearch">获取蓝牙列表</button><scroll-view :scroll-top="scrollTop" scroll-y class="content-pop"><viewclass="bluetoothItem"v-for="(item, index) in bluetoohList":key="index"@click="openControl(item)"><view class="textItem">蓝牙:{{ item.name }}</view><view>{{ item.deviceId }}</view></view></scroll-view></view>
</template><script>
export default {data() {return {bluetoohList: [],};},onLoad: function (options) {this.startSearch();},methods: {// 连接蓝牙startSearch() {var that = this;uni.openBluetoothAdapter({success: (res) => {uni.getBluetoothAdapterState({success: (res) => {if (res.available) {if (res.discovering) {uni.stopBluetoothDevicesDiscovery({success: (res) => {},});} else {that.getBluetoothDevices();}that.checkPemission();} else {uni.showToast({title: "本机蓝牙不可用",mask: true,});}},});},fail: (err) => {that.openSetting();},});},openSetting() {uni.showModal({title: "检测到您没打开蓝牙权限,是否去设置打开?",confirmText: "确认",cancelText: "取消",cancelColor: "#999999", //取消按钮颜色confirmColor: "#00A0E9", //确定按钮颜色success: (res) => {if (res.confirm) {uni.openSetting({//opensetting是调起设置页面的success: (res) => {if (res.authSetting == true) {//判断res.authsetting的值是true还是falseuni.openBluetoothAdapter({success: (res) => {this.startSearch();},});}},});} else {// 取消return false;}},});},checkPemission() {//android 6.0以上需授权地理位置权限var that = this;const sys = uni.getSystemInfoSync();if (sys.platform == "ios") {that.getBluetoothDevices();} else if (sys.platform == "android") {console.log(app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8),app.getSystem().length - (app.getSystem().length - 8) + 1));if (app.globalData.getSystem().substring(app.globalData.getSystem().length -(app.globalData.getSystem().length - 8),app.globalData.getSystem().length -(app.globalData.getSystem().length - 8) +1) > 5) {uni.getSetting({success: (res) => {console.log(res);if (!res.authSettin