-
Notifications
You must be signed in to change notification settings - Fork 28
/
init.js
60 lines (48 loc) · 1.68 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Recording from '../utils/recording'
export default function init() {
wx.showToast({ title: '初始化蓝牙中', icon: 'loading' })
const self = this
function openBluetoothStateListener() {
self.onBluetoothAdapterStateChange(bluetoothStateHandler)
}
function bluetoothStateHandler(res) {
const { debug, timeout } = self.config;
const { available, discovering } = res
debug && console.log('W-BLE:into bluetoothStateHandler ', res)
if (!available) {
wx.hideToast()
wx.showModal({ title: '设备蓝牙不可用', content: '请检查设备蓝牙是否打开', showCancel: false })
} else {
if (discovering) {
self._discovering = true
timeout && self.openTimeout()
} else {
self._discovering = false
// wx.showModal({ title: '蓝牙搜索状态被关闭', content: '请检查设备蓝牙是否正常', showCancel: false })
if (self.currentState !== 'init') {
self.currentState = 'init'
}
self.trigger('success')
}
}
}
function getBluetoothAdapterState() {
return self.getBluetoothAdapterState()
.then(bluetoothStateHandler)
.catch(error => self.trigger('failure', error))
}
if (Recording.isOpenedAdapter) {
openBluetoothStateListener()
getBluetoothAdapterState()
} else {
this.openBluetoothAdapter()
.then(res => {
wx.showToast({ title: '蓝牙初始化成功', duration: 1000 })
!!!Recording.isOpenedAdapter && (Recording.isOpenedAdapter = true)
openBluetoothStateListener()
getBluetoothAdapterState()
})
.catch(error => this.trigger('failure', error))
}
}
init.state = 'init'