3
# This file is part of Checkbox.
5
# Copyright 2016 Canonical Ltd.
8
# Po-Hsu Lin <po-hsu.lin@canonical.com>
9
# Yung Shen <yung.shen@canonical.com>
11
# Checkbox is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License version 3,
13
# as published by the Free Software Foundation.
15
# Checkbox is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
# GNU General Public License for more details.
20
# You should have received a copy of the GNU General Public License
21
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
28
from argparse import ArgumentParser
31
def unpair_all(devices, manager):
32
""" Unpairing paired devices and scanning again for rerun jobs."""
35
print("INFO: Unpairing", dev)
37
except bt_helper.BtException as exc:
38
print("Warning: Unpairing failed", exc)
40
# print(flush=True) to bypass plainbox output buffer,
41
# see LP: #1569808 for more details.
42
print("Please reset the device to pairing mode in 13 seconds",
45
print("INFO: Re-scaning for devices in pairing mode", flush=True)
50
"""Add argument parser here and do most of the job."""
51
parser = ArgumentParser(description=("Bluetooth auto paring and connect. "
52
"Please select one option."))
53
group = parser.add_mutually_exclusive_group(required=True)
54
group.add_argument("--mac", type=str,
55
help="Pair with a given MAC, not using scan result,")
56
group.add_argument("--mouse", action="store_const",
57
const="input-mouse", dest="target",
58
help="List and pair with mouse devices")
59
group.add_argument("--keyboard", action="store_const",
60
const="input-keyboard", dest="target",
61
help="List and pair with keyboard devices")
62
args = parser.parse_args()
64
manager = bt_helper.BtManager()
65
# Power on bluetooth adapter and scanning devices in advance.
66
manager.ensure_adapters_powered()
70
# TODO check MAC format
71
print("INFO: Trying to pair with {}".format(args.mac))
72
device = list(manager.get_bt_devices(filters={'Address': args.mac}))
73
paired_device = list(manager.get_bt_devices(
74
filters={'Address': args.mac, 'Paired': True}))
76
print("ERROR: No pairable device found, terminating")
79
unpair_all(paired_device, manager)
84
except bt_helper.BtException as exc:
85
print("ERROR: Unable to pair: ", exc)
88
print("INFO: Device paired")
91
print("INFO: Listing targeting devices")
92
# Listing device based on RSSI
93
paired_targets = list(manager.get_bt_devices(category=bt_helper.BT_ANY,
94
filters={'Paired': True, 'Icon': args.target}))
95
if not paired_targets:
96
print("INFO: No paired targeting devices found")
99
unpair_all(paired_targets, manager)
101
target_devices = sorted(manager.get_bt_devices(
102
category=bt_helper.BT_ANY, filters={
103
'Paired': False, 'Icon': args.target}),
104
key=lambda x: int(x.rssi or -255), reverse=True)
105
if not target_devices:
106
print("ERROR: No target devices found, terminating")
108
print("INFO: Detected devices (sorted by RSSI; highest first).")
109
# let's assing numbers to devices
110
devices = dict(enumerate(target_devices, 1))
111
for num, dev in devices.items():
112
print("{}. {} (RSSI: {})".format(num, dev, dev.rssi))
115
print("Which one would you like to connect to? (0 to exit)")
117
# TODO: enter as default to 1st device
120
chosen = num.isnumeric() and int(num) in devices.keys()
121
print("INFO: {} chosen.".format(devices[int(num)]))
122
print("INFO: Pairing selected device..")
124
devices[int(num)].pair()
125
except bt_helper.BtException as exc:
126
print("ERROR: something wrong: ", exc)
129
print("Paired successfully.")
131
# capture all other silence failures
134
if __name__ == "__main__":