-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtpair.py
48 lines (37 loc) · 1.09 KB
/
btpair.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
BTPair - Bluetooth pairing
You may use any Piment project under the terms
of the GNU General Public License (GPL) Version 3.
(c) 2016 Emilio Mariscal (emi420 [at] gmail.com)
'''
import pexpect
class BTPair(object):
waiting = False
_DEVICE_NAME = "HAS"
def __init__(self):
waiting = False
pexpect.spawn('hciconfig hci0 name ' + self._DEVICE_NAME)
def wait(self):
self.waiting = True
self.p = pexpect.spawn('bluetoothctl')
self.p.sendline("power on")
self.p.sendline("agent on")
self.p.sendline("default-agent")
self.p.sendline("discoverable on")
self.p.sendline("scan on")
print "Waiting request..."
self.p.expect('Confirm passkey', timeout=120)
print "Request confirmation"
self.p.sendline("yes")
try:
self.p.expect('Paired: yes')
self.stop()
except:
self.stop()
return "Paired"
def stop(self):
self.p.kill(0)
self.waiting = False
return "Stopped."