-
Notifications
You must be signed in to change notification settings - Fork 28
/
search.js
52 lines (42 loc) · 1.54 KB
/
search.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
import { isFunction } from '../utils/index'
export default function search() {
const self = this
function getConnectedBluetoothDevices() {
self.getConnectedBluetoothDevices({ services: self.config.connectOptions.services || [] })
.then(res => {
const { devices } = res
if (devices && devices.length) {
self.config.debug && console.log(devices)
if (self.checkDevicesWhetherMatch(devices, true)) {
self.trigger('success')
} else {
startBluetoothDevicesDiscovery()
}
} else {
startBluetoothDevicesDiscovery()
}
})
.catch(error => self.trigger('failure', error, getConnectedBluetoothDevices))
}
function startBluetoothDevicesDiscovery() {
self.startBluetoothDevicesDiscovery(self.config.connectOptions)
.then(res => res.isDiscovering
? onBluetoothDeviceFound()
: self.trigger('failure', error, startBluetoothDevicesDiscovery))
.catch(error => self.trigger('failure', error, startBluetoothDevicesDiscovery))
}
function onBluetoothDeviceFound() {
self.onBluetoothDeviceFound(res => {
self.config.debug && console.log('WX-BLE:into self.onBluetoothDeviceFound ', res)
const { devices } = res
isFunction(self.config.onFound) && self.config.onFound.call(this, devices)
if (devices && devices.length) {
if (self.checkDevicesWhetherMatch(devices)) {
self.trigger('success')
}
}
})
}
getConnectedBluetoothDevices()
}
search.state = 'search'