1#!/usr/bin/env python3

2# Copyright 2016 Canonical Ltd.

3# All rights reserved.

4#

5# Written by:

6# Maciej Kisielewski <maciej.kisielewski@canonical.com>

7

8 import checkbox_support.bt_helper as bt_helper

9

10

11 def main():

12 mgr = bt_helper.BtManager()

13 mgr.ensure_adapters_powered()

14 print("Make sure that the keyboard is turned on and is in pairable state.")

15 print("Then press ENTER")

16 input()

17 print("Scanning for devices...")

18 mgr.scan()

19

20 keyboards = sorted(

21 mgr.get_bt_devices(

22 category=bt_helper.BT_KEYBOARD, filters={"Paired": False}

23 ),

24 key=lambda x: int(x.rssi or -255),

25 reverse=True,

26 )

27 if not keyboards:

28 print("No keyboards detected")

29 return

30 print("Detected keyboards (sorted by RSSI; highest first).")

31 # let's assing numbers to keyboards

32 keyboards = dict(enumerate(keyboards, 1))

33 for num, kb in keyboards.items():

34 print("{}. {} (RSSI: {})".format(num, kb, kb.rssi))

35 chosen = False

36 while not chosen:

37 print(

38 "Which one would you like to connect to? (0 to exit) ", flush=True

39 )

40 num = input()

41 if num == "0":

42 return

43 chosen = num.isnumeric() and int(num) in keyboards.keys()

44 kb = keyboards[int(num)]

45 print("{} chosen. Pairing...".format(kb))

46 kb.pair()

47 print(

48 (

49 "Try typing on a keyboard. "

50 'Type "quit" and press ENTER to end the test.'

51 )

52 )

53 while input().lower() != "quit":

54 pass

55 print("Unpairing the keyboard...")

56 kb.unpair()

57

58

59 if __name__ == "__main__":

60 main()