Commit 0f9ec355 by lirandong

接通 设备端连接WiFi功能

parent 332c60fe
......@@ -89,7 +89,7 @@ class Bluetooth {
// 获取 WiFi 列表
this.formatWifiList(deviceId, value)
break
case WIFI_CHARACTERISTIC_ID:
case POST_WIFI_PASS_ID:
// WiFi 连接状态回调
this.formatWifiLine(value)
default:
......@@ -134,12 +134,13 @@ class Bluetooth {
}
}
/** 整理WiFi连接信息 */
/** 整理WiFi连接状态信息 */
formatWifiLine(value: arrayBuffer) {
console.log('ArrayBufferToString', ArrayBufferToString(value))
const str = String.fromCharCode.apply(null, new Uint8Array(value))
console.log('ArrayBufferToString', str)
}
/** 向蓝牙发送WiFi密码 */
/** 向设备端发送WiFi密码 */
async postWifiPassword(id: number, pass: string, deviceId: string) {
await bluetooth.notifyBLECharacteristicValueChange({
deviceId,
......@@ -150,7 +151,7 @@ class Bluetooth {
return bluetooth.writeBLECharacteristicValue({
deviceId,
serviceId: BLE_SERVICE_ID,
value: strToAb(id + pass),
value: strToAb(id, pass),
characteristicId: POST_WIFI_PASS_ID
})
}
......
......@@ -48,14 +48,28 @@ export function getWindiwHeight() {
return windowHeight - HeaderHeight
}
/** ArrayBuffer转16进字符串 */
export function ArrayBufferToString(buffer: ArrayBuffer) {
/** ArrayBuffer转16进字符串 */
export function ArrayBufferToString(buffer: ArrayBuffer, toText: boolean = false) {
const hexArr: any[] = Array.prototype.map.call(new Uint8Array(buffer), bit => {
return ('00' + bit.toString(16)).slice(-2)
})
if (toText) {
hexArr.forEach(element => {
element = element.charAt()
console.log('element.charAt()', element)
})
}
return hexArr.join('')
}
// /** ArrayBuffer转文字 */
// export function ArrayBufferToText(buffer: ArrayBuffer) {
// const hexArr: any[] = Array.prototype.map.call(new Uint8Array(buffer), bit => {
// return ('00' + bit.toString(16)).slice(-2)
// })
// return hexArr.join('')
// }
/** 解析蓝牙接收到的 十六进制 状态 */
export function getBLEState(buffer: ArrayBuffer) {
return new Uint8Array(buffer)[0]
......@@ -73,12 +87,13 @@ export function getAdverts(BleInfo: Taro.onBluetoothDeviceFound.ParamParamPropDe
}
/** 字符串转 ArrayBuffer */
export function strToAb(str: string) {
export function strToAb(id: number, str: string) {
const codeArr: number[] = []
console.log({ str })
for (let i = 0; i < str.length; i++) {
codeArr.push(str.charCodeAt(i))
}
console.log({ codeArr })
codeArr.unshift(id)
return new Uint8Array(codeArr).buffer
}
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