286 lines
7.1 KiB
JavaScript
286 lines
7.1 KiB
JavaScript
Page({
|
|
data: {
|
|
devices: [],
|
|
searching: false,
|
|
loading: false,
|
|
searchProgress: 0
|
|
},
|
|
|
|
/**
|
|
* 页面加载
|
|
*/
|
|
onLoad() {
|
|
console.log('蓝牙搜索页面加载')
|
|
this.setData({ loading: true })
|
|
this.checkBluetoothState()
|
|
},
|
|
|
|
/**
|
|
* 页面卸载
|
|
*/
|
|
onUnload() {
|
|
console.log('蓝牙搜索页面卸载')
|
|
this.stopBluetoothDiscovery()
|
|
},
|
|
|
|
/**
|
|
* 返回上一页
|
|
*/
|
|
goBack() {
|
|
wx.navigateBack({ delta: 1 })
|
|
},
|
|
|
|
/**
|
|
* 检查蓝牙状态
|
|
*/
|
|
checkBluetoothState() {
|
|
console.log('检查蓝牙适配器状态')
|
|
|
|
wx.getBluetoothAdapterState({
|
|
success: (res) => {
|
|
console.log('获取蓝牙状态成功:', res)
|
|
|
|
if (res.available) {
|
|
console.log('蓝牙可用,开始搜索设备')
|
|
this.startBluetoothDiscovery()
|
|
} else {
|
|
console.log('蓝牙不可用,尝试打开蓝牙适配器')
|
|
this.openBluetoothAdapter()
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('获取蓝牙状态失败:', err)
|
|
console.log('尝试打开蓝牙适配器')
|
|
this.openBluetoothAdapter()
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 打开蓝牙适配器
|
|
*/
|
|
openBluetoothAdapter() {
|
|
console.log('尝试打开蓝牙适配器')
|
|
|
|
wx.openBluetoothAdapter({
|
|
success: (res) => {
|
|
console.log('打开蓝牙适配器成功:', res)
|
|
this.setData({ loading: false })
|
|
wx.showToast({ title: '蓝牙已打开', icon: 'success' })
|
|
this.startBluetoothDiscovery()
|
|
},
|
|
fail: (err) => {
|
|
console.error('打开蓝牙适配器失败:', err)
|
|
this.setData({ loading: false })
|
|
|
|
let errorMessage = '请打开手机蓝牙'
|
|
if (err.errCode === 10001) {
|
|
errorMessage = '蓝牙未初始化'
|
|
} else if (err.errCode === 10002) {
|
|
errorMessage = '蓝牙未打开'
|
|
} else if (err.errCode === 10003) {
|
|
errorMessage = '蓝牙设备不可用'
|
|
}
|
|
|
|
wx.showToast({
|
|
title: errorMessage,
|
|
icon: 'none',
|
|
duration: 3000
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 开始搜索蓝牙设备
|
|
*/
|
|
startBluetoothDiscovery() {
|
|
console.log('开始搜索蓝牙设备')
|
|
this.setData({
|
|
searching: true,
|
|
loading: false,
|
|
searchProgress: 0,
|
|
devices: []
|
|
})
|
|
wx.showLoading({ title: '搜索蓝牙设备...' })
|
|
|
|
wx.startBluetoothDevicesDiscovery({
|
|
services: [],
|
|
allowDuplicatesKey: false,
|
|
success: (res) => {
|
|
console.log('开始搜索设备成功:', res)
|
|
this.scanDevices()
|
|
},
|
|
fail: (err) => {
|
|
console.error('开始搜索失败:', err)
|
|
wx.hideLoading()
|
|
this.setData({ searching: false })
|
|
|
|
let errorMessage = '搜索失败'
|
|
if (err.errCode === 10001) {
|
|
errorMessage = '蓝牙未初始化'
|
|
} else if (err.errCode === 10002) {
|
|
errorMessage = '蓝牙未打开'
|
|
} else if (err.errCode === 10003) {
|
|
errorMessage = '蓝牙设备不可用'
|
|
} else if (err.errCode === 10004) {
|
|
errorMessage = '搜索操作已存在'
|
|
}
|
|
|
|
wx.showToast({
|
|
title: errorMessage,
|
|
icon: 'none',
|
|
duration: 2000
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 扫描设备
|
|
*/
|
|
scanDevices() {
|
|
console.log('开始扫描设备')
|
|
let scanCount = 0
|
|
const maxScanTime = 10 // 最大扫描时间(秒)
|
|
|
|
const scanInterval = setInterval(() => {
|
|
scanCount++
|
|
|
|
// 更新搜索进度
|
|
const progress = Math.min(100, Math.round((scanCount / maxScanTime) * 100))
|
|
this.setData({ searchProgress: progress })
|
|
|
|
wx.getBluetoothDevices({
|
|
success: (res) => {
|
|
// 过滤并排序设备(按信号强度)
|
|
const devices = res.devices
|
|
.filter(device => device.name || device.localName)
|
|
.map(device => ({
|
|
...device,
|
|
name: device.name || device.localName || '未知设备'
|
|
}))
|
|
.sort((a, b) => (b.RSSI || 0) - (a.RSSI || 0))
|
|
|
|
this.setData({ devices })
|
|
console.log('搜索到设备:', devices.length)
|
|
},
|
|
fail: (err) => {
|
|
console.error('获取设备列表失败:', err)
|
|
}
|
|
})
|
|
|
|
if (scanCount >= maxScanTime) {
|
|
clearInterval(scanInterval)
|
|
this.stopBluetoothDiscovery()
|
|
}
|
|
}, 1000)
|
|
},
|
|
|
|
/**
|
|
* 停止搜索蓝牙设备
|
|
*/
|
|
stopBluetoothDiscovery() {
|
|
console.log('停止搜索蓝牙设备')
|
|
|
|
wx.stopBluetoothDevicesDiscovery({
|
|
success: () => {
|
|
wx.hideLoading()
|
|
this.setData({
|
|
searching: false,
|
|
searchProgress: 0
|
|
})
|
|
console.log('停止搜索设备成功')
|
|
},
|
|
fail: (err) => {
|
|
console.error('停止搜索设备失败:', err)
|
|
wx.hideLoading()
|
|
this.setData({
|
|
searching: false,
|
|
searchProgress: 0
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 连接设备
|
|
* @param {Event} e 点击事件
|
|
*/
|
|
connectDevice(e) {
|
|
const device = e.currentTarget.dataset.device
|
|
console.log('选择连接设备:', device)
|
|
|
|
if (!device || !device.deviceId) {
|
|
wx.showToast({ title: '设备信息错误', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
wx.showLoading({ title: '连接设备...' })
|
|
|
|
// 先停止搜索
|
|
this.stopBluetoothDiscovery()
|
|
|
|
wx.createBLEConnection({
|
|
deviceId: device.deviceId,
|
|
success: (res) => {
|
|
console.log('连接成功:', device, res)
|
|
|
|
// 保存设备信息到全局变量
|
|
const app = getApp()
|
|
app.globalData = app.globalData || {}
|
|
app.globalData.connectedDevice = device
|
|
|
|
// 跳转到主页面
|
|
wx.hideLoading()
|
|
wx.showToast({ title: '连接成功', icon: 'success' })
|
|
|
|
// 延迟跳转,确保用户看到成功提示
|
|
setTimeout(() => {
|
|
wx.navigateBack({ delta: 1 })
|
|
}, 1000)
|
|
},
|
|
fail: (err) => {
|
|
console.error('连接失败:', err)
|
|
wx.hideLoading()
|
|
|
|
let errorMessage = '连接失败'
|
|
if (err.errCode === 10001) {
|
|
errorMessage = '蓝牙未初始化'
|
|
} else if (err.errCode === 10002) {
|
|
errorMessage = '蓝牙未打开'
|
|
} else if (err.errCode === 10003) {
|
|
errorMessage = '蓝牙设备不可用'
|
|
} else if (err.errCode === 10004) {
|
|
errorMessage = '连接操作已存在'
|
|
} else if (err.errCode === 10005) {
|
|
errorMessage = '设备未找到'
|
|
} else if (err.errCode === 10006) {
|
|
errorMessage = '连接超时'
|
|
} else if (err.errCode === 10007) {
|
|
errorMessage = '连接断开'
|
|
}
|
|
|
|
wx.showToast({
|
|
title: errorMessage,
|
|
icon: 'none',
|
|
duration: 3000
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* 刷新设备列表
|
|
*/
|
|
refreshDevices() {
|
|
console.log('刷新设备列表')
|
|
this.setData({
|
|
loading: true,
|
|
searchProgress: 0,
|
|
devices: []
|
|
})
|
|
this.checkBluetoothState()
|
|
}
|
|
})
|