Commit 0f9ec355 by lirandong

接通 设备端连接WiFi功能

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