Commit fc4540a6 by lirandong

解决 iOS 获取蓝牙特征信息失败问题

parent 24fe9cbc
......@@ -63,11 +63,36 @@ class Bluetooth {
/** 连接蓝牙 */
async createBLEConnection(deviceId: string) {
await bluetooth.createBLEConnection({ deviceId })
bluetooth.stopBluetoothDevicesDiscovery()
bluetooth.onBLEConnectionStateChange(({ connected }) => {
showMyToast({ title: connected ? '蓝牙已连接' : '蓝牙已断开' })
})
try {
await bluetooth.createBLEConnection({ deviceId })
bluetooth.stopBluetoothDevicesDiscovery()
await this.getBLEDeviceServices(deviceId)
} catch (error) {
console.error(error)
}
}
/** 获取蓝牙服务列表, 初始化蓝牙服务列表 */
async getBLEDeviceServices(deviceId: string) {
Taro.showLoading({ title: '获取蓝牙参数...' })
const { services } = await bluetooth.getBLEDeviceServices({ deviceId }) // 获取服务列表
Taro.showLoading({ title: '获取特征值...' })
const serveList: Array<Promise<Taro.getBLEDeviceCharacteristics.Promised>> = []
for (const iterator of services) {
const serve = bluetooth.getBLEDeviceCharacteristics({
deviceId,
serviceId: iterator.uuid
}) // 获取特征服务
serveList.push(serve)
}
const serveListRes = await Promise.all(serveList)
serveListRes.forEach(({ characteristics }) => {
console.log({ characteristics })
})
}
/** 打开蓝牙的监听功能 */
......@@ -111,17 +136,21 @@ class Bluetooth {
/** 整理WiFi列表的信息包 */
async formatWifiList(deviceId: string, value: ArrayBuffer) {
const { page, advertisData } = await analysisPage(value)
if (page === 0) {
// 数据传输完毕
this.getWifiListCallBack && this.getWifiListCallBack(JSON.parse(advertisData))
try {
const { page, advertisData } = await analysisPage(value)
if (page === 0) {
// 数据传输完毕
this.getWifiListCallBack && this.getWifiListCallBack(JSON.parse(advertisData))
}
bluetooth.writeBLECharacteristicValue({
deviceId,
serviceId: BLE_SERVICE_ID,
value: new Uint8Array([page]).buffer,
characteristicId: WIFI_CHARACTERISTIC_ID
})
} catch (error) {
console.error('获取WiFi列表', error)
}
bluetooth.writeBLECharacteristicValue({
deviceId,
serviceId: BLE_SERVICE_ID,
value: new Uint8Array([page]).buffer,
characteristicId: WIFI_CHARACTERISTIC_ID
})
}
/** 整理WiFi连接状态信息 */
......
......@@ -11,7 +11,11 @@ class Token {
init() {
if (!this.initDone) {
this.initDone = true
return this.getStorageToken()
const token = this.getStorageToken()
if (!token) {
Taro.navigateTo({ url: '/pages/login/index' })
}
return token
}
}
......
......@@ -58,7 +58,7 @@ class User extends Component {
logOut() {
token.reset()
Taro.navigateTo({ url: '/pages/login/index.tsx' })
Taro.navigateTo({ url: '/pages/login/index' })
}
shouldComponentUpdate(_nextPorps, _nextState) {
......
......@@ -21,12 +21,13 @@ interface Index {
}
class Index extends Component {
init = false
config: Config = {
navigationBarTitleText: '',
navigationStyle: 'custom'
}
constructor(props) {
super(props)
componentWillMount() {
this.initToekn()
}
......
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