Commit 4451ecdc by hank

修改蓝牙连接

parent 33eed3a1
......@@ -2,7 +2,46 @@ import Taro, { arrayBuffer } from '@tarojs/taro'
import { Header } from 'react-navigation'
import AsyncStorage from '@react-native-community/async-storage'
import { bluetooth } from './adapter'
function Utf8ArrayToStr(array) {
let out, i, len, c
let char2, char3
out = ''
len = array.length
i = 0
while (i < len) {
c = array[i++]
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c)
break
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++]
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f))
break
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++]
char3 = array[i++]
out += String.fromCharCode(
((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
)
break
}
}
return out
}
/** 连接参数 */
export function concatParam(data: any): string {
return Object.entries(data)
......@@ -78,7 +117,9 @@ export function getBLEState(buffer: ArrayBuffer) {
/** 解析蓝牙接收到的数据 */
export function getBLEData(buffer: ArrayBuffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer.slice(1, buffer.byteLength)))
console.log(Utf8ArrayToStr(new Uint8Array(buffer.slice(1, buffer.byteLength))))
// return String.fromCharCode.apply(null, new Uint8Array(buffer.slice(1, buffer.byteLength)))
return Utf8ArrayToStr(new Uint8Array(buffer.slice(1, buffer.byteLength)))
}
/** 获取蓝牙的 */
......
......@@ -146,21 +146,33 @@ class MyDevice extends Component {
})
const mode = JSON.parse(result).model || 'OWN'
const code = JSON.parse(result).qrcode
console.log(result, mode, code)
// console.log(result, mode, code)
if (!code) {
showMyToast({ title: '添加失败~' })
return false
}
if (mode === 'TV' && code) {
await api.common.addDeviceToken(code)
api.common
.addDeviceToken(code)
.then(() => {
showMyToast({ title: '添加成功~' })
})
.catch(() => {
showMyToast({ title: '添加失败~' })
})
this.page = 1
this.getDate()
}
if (mode === 'OWN' && code) {
if ((mode === 'OWN' || mode === 'OWNRESET') && code) {
const res = await Ble.openBle(code)
const { deviceId } = res
Taro.navigateTo({ url: `/pages/system/wifi_list/index?deviceId=${deviceId}` })
Taro.navigateTo({
url: `/pages/system/wifi_list/index?deviceId=${deviceId}&mode=${mode}`
})
}
} catch (error) {
console.error(error)
showMyToast({ result: error, title: '添加失败~' })
console.log(error)
}
}
}
......
......@@ -156,6 +156,7 @@ class WifiList extends Component {
async onConfirm() {
const { id, type } = this.activeWLAN
const { password, deviceId } = this
const { mode } = this.$router.params
if (id === undefined || !deviceId) return
if (type !== 'OPEN' && password.length < 8) {
showMyToast({ title: 'wifi 密码少于8位' })
......@@ -165,6 +166,11 @@ class WifiList extends Component {
try {
Taro.showLoading({ title: '连接中...' })
await Ble.postWifiPassword(id, password, deviceId)
if (mode && mode === 'OWNRESET') {
setTimeout(() => {
Taro.navigateBack()
}, 500)
}
// Taro.hideLoading()
} catch (error) {
console.error(error)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment