uu
This commit is contained in:
parent
8ebe91b021
commit
64670054fd
@ -199,6 +199,39 @@ const canvasGetImageData = (params = {})=> {
|
||||
})
|
||||
}
|
||||
|
||||
const uniPromiseApi = (name, options = {}, extObj = { })=> {
|
||||
return new Promise((resolve, reject)=> {
|
||||
if(!(uni?.[name])) {
|
||||
console.error(`uni.${name} api不存在 `)
|
||||
return reject(`uni.${name} api不存在`)
|
||||
}
|
||||
uni?.[name]?.({
|
||||
...options,
|
||||
success(res){
|
||||
console.error(`uni.${name} -execResult:success====`, res)
|
||||
resolve({ res })
|
||||
if(extObj.successText) {
|
||||
showToast(successText)
|
||||
}
|
||||
},
|
||||
fail(err){
|
||||
console.error(`uni.${name} -execResult:fail====`, err)
|
||||
resolve({ err })
|
||||
if(extObj.failText) {
|
||||
showToast(failText)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const promiseTimeout = (time) => {
|
||||
return new Promise((resolve, reject)=> {
|
||||
setTimeout(()=> {
|
||||
resolve()
|
||||
}, time)
|
||||
})
|
||||
}
|
||||
|
||||
export {
|
||||
reLaunch,
|
||||
@ -216,5 +249,7 @@ export {
|
||||
saveFile,
|
||||
getImageTempFilePath,
|
||||
canvasGetImageData,
|
||||
getImageInfo
|
||||
getImageInfo,
|
||||
uniPromiseApi,
|
||||
promiseTimeout,
|
||||
}
|
||||
|
||||
@ -179,6 +179,7 @@ const textDecoder = (input)=> {
|
||||
return decodeURIComponent(escape(String.fromCharCode(...[input])));
|
||||
}
|
||||
|
||||
|
||||
export {
|
||||
getAddressDetail,
|
||||
setAddresModalParam,
|
||||
@ -190,5 +191,5 @@ export {
|
||||
debounce,
|
||||
throttle,
|
||||
textEncoder,
|
||||
textDecoder
|
||||
textDecoder,
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { showLoading, showToast, hideLoading } from "@/common/untool"
|
||||
import { showLoading, showToast, hideLoading, uniPromiseApi, promiseTimeout } from "@/common/untool"
|
||||
import { getSessionBluetoothInfo, setSessionBluetoothInfo } from "@/session"
|
||||
|
||||
export default {
|
||||
@ -163,76 +163,59 @@ export default {
|
||||
let conSta = false
|
||||
// 判断是否有历史连接
|
||||
if(characteristicId) {
|
||||
conSta = await this.connect()
|
||||
let { res, err } = await this.connect()
|
||||
conSta = !!res
|
||||
}
|
||||
// 连接不成功,打开蓝牙配置页面
|
||||
if(!conSta) {
|
||||
this.showConnectPage = true
|
||||
this.discoveryPrinter()
|
||||
}
|
||||
return bl
|
||||
return conSta
|
||||
},
|
||||
changeTab(type) {
|
||||
this.curTab = type
|
||||
},
|
||||
openBluetoothAdapter () {
|
||||
async openBluetoothAdapter () {
|
||||
var _this = this
|
||||
return new Promise((resolve, reject)=> {
|
||||
uni?.openBluetoothAdapter?.({
|
||||
complete (e) {
|
||||
console.log(e);
|
||||
if (!e.errCode) {
|
||||
resolve()
|
||||
console.log('初始化完成')
|
||||
} else if (e.errCode == 10001) {
|
||||
reject(e)
|
||||
showToast('请打开手机蓝牙')
|
||||
} else {
|
||||
reject(e)
|
||||
console.log('openBluetoothAdapter:error=========', e.errMsg)
|
||||
showToast(e.errMsg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
const { res, err } = await uniPromiseApi('openBluetoothAdapter', { })
|
||||
if(err) {
|
||||
if (err.errCode == 10001) {
|
||||
showToast('请打开手机蓝牙')
|
||||
} else {
|
||||
showToast(err.errMsg)
|
||||
}
|
||||
}
|
||||
},
|
||||
// 开始搜寻附近的蓝牙外围设备
|
||||
discoveryPrinter () {
|
||||
async discoveryPrinter () {
|
||||
var _this = this
|
||||
_this.devices = []
|
||||
_this.searchLoading = true
|
||||
_this.curTab = 0
|
||||
return new Promise((resolve, reject)=> {
|
||||
uni?.startBluetoothDevicesDiscovery?.({
|
||||
complete (e) {
|
||||
console.log(e)
|
||||
if (e.errMsg == "startBluetoothDevicesDiscovery:ok") {
|
||||
resolve()
|
||||
// ArrayBuffer转16进度字符串示例
|
||||
uni.onBluetoothDeviceFound(devices => {
|
||||
console.log("========devices=====", devices)
|
||||
let curDvs = devices?.devices?.[0] || {}
|
||||
// 过滤重复的
|
||||
if(curDvs.deviceId && _this.devices.map(t=> t.deviceId).indexOf(curDvs.deviceId) == -1) {
|
||||
_this.devices.push(curDvs)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
const { res, err } = await uniPromiseApi('startBluetoothDevicesDiscovery', { })
|
||||
if(res) {
|
||||
// 打开搜索后,监听设备回调信息
|
||||
uni.onBluetoothDeviceFound(devices => {
|
||||
console.log("========devices=====", devices)
|
||||
let curDvs = devices?.devices?.[0] || {}
|
||||
// 过滤已经搜到的设备
|
||||
if(curDvs.deviceId && _this.devices.map(t=> t.deviceId).indexOf(curDvs.deviceId) == -1) {
|
||||
_this.devices.push(curDvs)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
return { res, err }
|
||||
},
|
||||
// 停止搜寻附近的蓝牙外围设备
|
||||
async stopDiscoveryPrinter () {
|
||||
this.searchLoading = false
|
||||
uni?.stopBluetoothDevicesDiscovery?.()
|
||||
return uniPromiseApi('stopBluetoothDevicesDiscovery')
|
||||
},
|
||||
// 断开蓝牙
|
||||
async closeBluetoothAdapter () {
|
||||
this.searchLoading = false
|
||||
uni?.closeBluetoothAdapter?.()
|
||||
return uniPromiseApi('closeBluetoothAdapter')
|
||||
},
|
||||
async handleSelectDeviceId (item) {
|
||||
this.deviceId = item.deviceId
|
||||
@ -266,113 +249,76 @@ export default {
|
||||
handleSelectChara (item) {
|
||||
this.characteristicId = item.uuid
|
||||
},
|
||||
connect () {
|
||||
async connect () {
|
||||
var _this = this
|
||||
console.log("连接蓝牙=====deviceId", _this.deviceId)
|
||||
showLoading('正在连接蓝牙')
|
||||
return new Promise((resolve)=> {
|
||||
uni.createBLEConnection({
|
||||
deviceId: _this.deviceId,
|
||||
complete (e) {
|
||||
hideLoading()
|
||||
console.log("蓝牙连接回调=====", e)
|
||||
if (e.errMsg == "createBLEConnection:ok") {
|
||||
showToast(`蓝牙连接成功`)
|
||||
//获取蓝牙设备所有服务(service)。
|
||||
setTimeout(async()=> {
|
||||
await _this.getBLEDeviceServices()
|
||||
resolve(true)
|
||||
}, 2000)
|
||||
} else {
|
||||
resolve(false)
|
||||
showToast(`连接设备失败:${e.errMsg}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
let { res, err } = await uniPromiseApi('createBLEConnection', { deviceId: _this.deviceId })
|
||||
hideLoading()
|
||||
if( res) {
|
||||
showToast(`蓝牙连接成功`)
|
||||
await promiseTimeout(1000)
|
||||
// 获取蓝牙设备所有服务(service)。
|
||||
await _this.getBLEDeviceServices()
|
||||
}
|
||||
if(err) {
|
||||
showToast(`连接设备失败:${err.errMsg}`)
|
||||
}
|
||||
return { res, err }
|
||||
},
|
||||
getBLEDeviceServices(){
|
||||
async getBLEDeviceServices() {
|
||||
var _this = this
|
||||
return new Promise((resolve)=> {
|
||||
showLoading('正在搜索服务')
|
||||
uni.getBLEDeviceServices({
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: _this.deviceId,
|
||||
success:(res)=>{
|
||||
hideLoading()
|
||||
resolve()
|
||||
console.log('device services:', res)
|
||||
_this.serverList = res.services
|
||||
// _this.serviceId = res.services[0].uuid;
|
||||
console.log('serverId:', _this.serviceId)
|
||||
},
|
||||
fail:(e) =>{
|
||||
hideLoading()
|
||||
resolve()
|
||||
this.serverList = []
|
||||
showToast(`设备服务获取失败:${e.errMsg}`)
|
||||
console.log("getBLEDeviceServices - fail==============", e)
|
||||
}
|
||||
})
|
||||
let { res, err } = await uniPromiseApi('getBLEDeviceServices', {
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: _this.deviceId,
|
||||
})
|
||||
if(res) {
|
||||
_this.serverList = res.services
|
||||
// _this.serviceId = res.services[0].uuid;
|
||||
console.log('serverId:', _this.serviceId)
|
||||
}
|
||||
if(err) {
|
||||
this.serverList = []
|
||||
showToast(`设备服务获取失败:${err.errMsg}`)
|
||||
}
|
||||
return { res, err }
|
||||
},
|
||||
getBLEDeviceCharacteristics () {
|
||||
async getBLEDeviceCharacteristics () {
|
||||
var _this = this
|
||||
return new Promise((resolve, reject)=> {
|
||||
showLoading('正在搜索特征值')
|
||||
uni.getBLEDeviceCharacteristics({
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: _this.deviceId,
|
||||
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
||||
serviceId:_this.serviceId,
|
||||
success:(res)=>{
|
||||
hideLoading()
|
||||
console.log('getBLEDeviceCharacteristics', res)
|
||||
_this.characteristics = res.characteristics
|
||||
resolve()
|
||||
// _this.characteristicId = res.characteristics[0].uuid
|
||||
},
|
||||
fail:(res)=>{
|
||||
hideLoading()
|
||||
reject()
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
let { res, err } = await uniPromiseApi('getBLEDeviceCharacteristics', {
|
||||
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
||||
deviceId: _this.deviceId,
|
||||
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
|
||||
serviceId:_this.serviceId,
|
||||
})
|
||||
if(res) {
|
||||
_this.characteristics = res.characteristics
|
||||
}
|
||||
return { res, err }
|
||||
},
|
||||
// 根据选择的deviceId自动匹配 serviceId, characteristicId
|
||||
async handleAutoSelectAllData(item) {
|
||||
console.log("============测试==1111111")
|
||||
const _this = this
|
||||
this.deviceId = item.deviceId
|
||||
this.serviceId = ''
|
||||
this.serverList = []
|
||||
this.characteristics = []
|
||||
this.characteristicId = ''
|
||||
await this.connect()
|
||||
await this.handleSelectDeviceId(item)
|
||||
|
||||
if(this.serverList?.length > 0) {
|
||||
for(let i=0; i<_this.serverList?.length; i++) {
|
||||
let _serviceId = _this.serverList[i].uuid
|
||||
let charRes = await new Promise((resolve, reject) => {
|
||||
uni.getBLEDeviceCharacteristics({
|
||||
deviceId: _this.deviceId,
|
||||
serviceId: _serviceId,
|
||||
success:(res)=>{
|
||||
let charItem = res.characteristics.filter(t=> {
|
||||
let { notify, indicate, write } = t.properties || {}
|
||||
return (notify || indicate) && write
|
||||
})[0]
|
||||
resolve(charItem)
|
||||
},
|
||||
fail:(res)=>{
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
let charApi = await uniPromiseApi('getBLEDeviceCharacteristics', {
|
||||
deviceId: _this.deviceId,
|
||||
serviceId: _serviceId,
|
||||
})
|
||||
if(charRes) {
|
||||
_this.serviceId = _serviceId
|
||||
_this.characteristicId = charRes.uuid
|
||||
break
|
||||
let { res, err } = charApi
|
||||
if(res) {
|
||||
let charItem = res.characteristics.filter(t=> {
|
||||
let { notify, indicate, write } = t.properties || {}
|
||||
return (notify || indicate) && write
|
||||
})[0]
|
||||
if(charItem) {
|
||||
_this.serviceId = _serviceId
|
||||
_this.characteristicId = charRes.uuid
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user