微信小程序是可以跑通、就支付宝小程序报错
<style>
page{
background: white;
}
</style>
<template>
<view>
<view class="bluetoothDetails_content">
<cu-custom bgColor="bg-blue" :isBack="true">
<block slot="backText">返回</block>
<block slot="content">蓝牙详情</block>
</cu-custom>
</view>
<view>
<button type="primary" @click="openBluetoothAdapter">初始化蓝牙</button>
</view>
</view>
</template>
<script>
export default {
data() {
return {
deviceCode: 'B380001B59', // 设备编号
deviceId: '', // 设备max
}
},
// 建议使用 uni-app 的 onLoad 代替 vue 的 created
onLoad(option) {
console.log('onLoad')
console.log(option)
},
onShow() {
console.log('onShow')
},
// 建议使用 uni-app 的 onReady代替 vue 的 mounted
onReady() {
console.log('onReady')
},
// 监听变量
watch: {
},
// 页面离开
destroyed() {
console.log('destroyed')
},
// 页面隐藏
onHide() {
console.log('onHide')
},
// 页面卸载
onUnload() {
console.log('onUnload')
},
methods: {
// 初始化蓝牙
openBluetoothAdapter() {
uni.openBluetoothAdapter({
success:(res) => {
console.log(res)
this.startBluetoothDevicesDiscovery()
},
fail:(res) => {
console.log(res)
}
})
},
// 搜索周边设备
startBluetoothDevicesDiscovery() {
uni.startBluetoothDevicesDiscovery({
services: [],
success:(res) => {
console.log(res)
this.onBluetoothDeviceFound()
},
fail:(res) => {
console.log(res)
}
})
},
// 停止搜索
stopBluetoothDevicesDiscovery() {
uni.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log(res)
}
})
},
// 监听到搜索的新设备
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound((res) => {
console.log('new device list has founded')
console.log(res.devices[0])
let name = ''
if (res.devices[0].localName) {
name = res.devices[0].localName
} else if (res.devices[0].name) {
name = res.devices[0].name
}
if (name === 'K' + this.deviceCode) {
this.deviceId = res.devices[0].deviceId
this.createBLEConnection()
}
})
},
// 连接设备
createBLEConnection() {
console.log('连接设备---' + this.deviceId)
uni.createBLEConnection({
deviceId: this.deviceId,
success: (res) => {
console.log(res)
}
})
}
},
computed: {},
// 监听用户下拉动作,一般用于下拉刷新
onPullDownRefresh() {
},
// 页面上拉触底事件的处理函数
onReachBottom() {
},
// 监听页面滚动
onPageScroll(res) {
this.scrollTop = res.scrollTop
}
}
</script>