class MyApp(object):
    def __init__(self, stdscreen):
        self.scan_timeout = 90
        self.bt = Bluetooth()
        self.bt.start_scanning(self.scan_timeout)

        self.screen = stdscreen
        curses.curs_set(0)
        mainMenu = [
            ('Rescan devices\t\t(scans for {} seconds in background, system bus will be processed every 10 seconds)'
             .format(self.scan_timeout), self.rescan_devices),
            ('Trust controller\t\t(shows only untrusted pairable controllers)',
             self.trust_controller_menu),
            ('Pair controller\t\t(shows only unpaired pairable controllers)',
             self.pair_controller_menu),
            ('Connect controller\t\t(shows only paired and trusted connectable controllers)',
             self.connect_device_menu),
            ('Disconnect controller\t(shows only connected controllers)',
             self.disconnect_device_menu),
            ('Remove controller\t\t(shows only trusted, paired OR connected controllers)',
             self.remove_device_menu),
        ]
        self.make_menu(mainMenu)
        self.menu.display()

    def make_menu(self, menulist):
        self.menu = Menu(menulist, self.screen)

    def trust_controller_menu(self):
        properties = [
            'Icon',
            'RSSI',
            'Trusted',
        ]
        menu = []
        for device in self.bt.get_available_devices():
            mac_address = device['mac_address']
            for property in properties:
                device[property] = self.bt.get_device_property(
                    mac_address, property)
            if ((device['Icon'] == 'input-gaming')
                    and (device['Trusted'] == 0)):
                menu.append(
                    ('{}\t{}\tRSSI: {}'.format(device['mac_address'],
                                               device['name'], device['RSSI']),
                     self.trust_controller))
        self.make_menu(menu)
        self.menu.display()

    def trust_controller(self):
        mac = self.get_selected_device()[0]
        self.bt.trust(mac)
        if self.bt.get_device_property(mac, 'Trusted') == 1:
            self.menu.items[self.menu.position] = (
                'MAC {} ({}) trusted!\n'.format(mac,
                                                self.get_selected_device()[1]),
                self.navigate_to_back)
        else:
            self.menu.items[self.menu.position] = (
                'Error trusting MAC {} ({})!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)

    def pair_controller_menu(self):
        properties = [
            'Icon',
            'Paired',
            'RSSI',
            'Trusted',
        ]
        menu = []
        for device in self.bt.get_devices_to_pair():
            mac_address = device['mac_address']
            for property in properties:
                device[property] = self.bt.get_device_property(
                    mac_address, property)
            if ((device['Icon'] == 'input-gaming') and (device['Trusted'] == 1)
                    and device['Paired'] == 0):
                menu.append(
                    ('{}\t{}\tRSSI: {}'.format(device['mac_address'],
                                               device['name'], device['RSSI']),
                     self.pair_controller))
        self.make_menu(menu)
        self.menu.display()

    def pair_controller(self):
        mac = self.get_selected_device()[0]
        self.bt.pair(mac)
        if self.bt.get_device_property(mac, 'Paired') == 1:
            self.menu.items[self.menu.position] = (
                'MAC {} ({}) paired!\n'.format(mac,
                                               self.get_selected_device()[1]),
                self.navigate_to_back)
        else:
            self.menu.items[self.menu.position] = (
                'Error paring MAC {} ({})!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)

    def connect_device_menu(self):
        properties = [
            'Icon',
            'RSSI',
            'Connected',
            'Paired',
            'Trusted',
        ]
        menu = []
        for device in self.bt.get_available_devices():
            mac_address = device['mac_address']
            for property in properties:
                device[property] = self.bt.get_device_property(
                    mac_address, property)
            if ((device['Icon'] == 'input-gaming') and (device['Paired'] == 1)
                    and (device['Trusted'] == 1)
                    and (device['Connected'] == 0)):
                menu.append(
                    ('{}\t{}\tRSSI: {}'.format(device['mac_address'],
                                               device['name'], device['RSSI']),
                     self.connect_device))
        self.make_menu(menu)
        self.menu.display()

    def connect_device(self):
        mac = self.get_selected_device()[0]
        self.bt.connect(mac)
        if self.bt.get_device_property(mac, 'Connected') == 1:
            self.menu.items[self.menu.position] = (
                'MAC {} ({}) connected!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)
        else:
            self.menu.items[self.menu.position] = (
                'Error connecting MAC {} ({})!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)

    def disconnect_device_menu(self):
        properties = [
            'Icon',
            'Connected',
            'RSSI',
        ]
        menu = []
        for device in self.bt.get_connected_devices():
            mac_address = device['mac_address']
            for property in properties:
                device[property] = self.bt.get_device_property(
                    mac_address, property)
            if ((device['Icon'] == 'input-gaming')
                    and (device['Connected'] == 1)):
                menu.append(
                    ('{}\t{}\tRSSI: {}'.format(device['mac_address'],
                                               device['name'], device['RSSI']),
                     self.disconnect_device))
        self.make_menu(menu)
        self.menu.display()

    def disconnect_device(self):
        mac = self.get_selected_device()[0]
        self.bt.disconnect(mac)
        if self.bt.get_device_property(mac, 'Connected') == 0:
            self.menu.items[self.menu.position] = (
                'MAC {} ({}) disconnected!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)
        else:
            self.menu.items[self.menu.position] = (
                'Error disconnecting MAC {} ({})!\n'.format(
                    mac,
                    self.get_selected_device()[1]), self.navigate_to_back)

    def remove_device_menu(self):
        properties = [
            'Icon',
            'Paired',
            'Trusted',
            'RSSI',
            'Blocked',
            'Connected',
        ]
        menu = []
        for device in self.bt.get_available_devices():
            mac_address = device['mac_address']
            for property in properties:
                device[property] = self.bt.get_device_property(
                    mac_address, property)
            if ((device['Icon'] == 'input-gaming')
                    and ((device['Paired'] == 1) or (device['Trusted'] == 1) or
                         (device['Blocked'] == 1))):
                menu.append((
                    '{}\t{}\tRSSI: {}\tTrusted: {}\tPaired: {}\tConnected: {}\tBlocked: {}'
                    .format(device['mac_address'], device['name'],
                            device['RSSI'], device['Trusted'],
                            device['Paired'], device['Connected'],
                            device['Blocked']), self.remove_device))
        self.make_menu(menu)
        self.menu.display()

    def remove_device(self):
        mac = self.get_selected_device()[0]
        self.bt.remove(mac)
        self.menu.items[self.menu.position] = ('MAC {} ({}) removed!\n'.format(
            mac,
            self.get_selected_device()[1]), self.navigate_to_back)

    def rescan_devices(self):
        self.menu.window.addstr(
            9, 1,
            'Scanning for device for {} seconds in background now, please refresh views...'
            .format(self.scan_timeout), curses.A_NORMAL)
        self.bt.start_scanning(self.scan_timeout)

    def get_selected_device(self):
        return (self.menu.items[self.menu.position][0].split('\t'))

    def navigate_to_back(self):
        self.menu.navigate(len(self.menu.items) - 1)
Ejemplo n.º 2
0
from bluetool import Bluetooth

scanner = Bluetooth()
scanner.scan()
found = scanner.get_available_devices()
print(found)
Ejemplo n.º 3
0
###@package scan_devices scan devices this file scans all the bluetooth devices in the area and writes them to a file for the function to read

from bluetool import Bluetooth

import json

d = []

bt = Bluetooth()
bt.scan()
devices = bt.get_available_devices()

f = open('/home/pi/hub-repository/shutter/shutter/controller/scripts/scan_results.json', 'w+').close()

f = open('/home/pi/hub-repository/shutter/shutter/controller/scripts/scan_results.json', 'w+')
f.write(json.dumps(devices))
f.close()
print(devices)
Ejemplo n.º 4
0
from bluetool import Bluetooth
from time import sleep
bluetooth = Bluetooth()
print("Scanning started...")
bluetooth.scan()
print("Scanning completed...")
devices = bluetooth.get_available_devices()
print("Available devices")
print(devices)

print("pairing with D8:32:E3:38:02:9D")
if bluetooth.pair('D8:32:E3:38:02:9D'):
    print("Pairing completed")
    sleep(7)
else:
    print("Unable to Pair")
    exit(1)
print("Connecting with D8:32:E3:38:02:9D")
if bluetooth.connect('D8:32:E3:38:02:9D'):
    print("Connected successfully!")
else:
    print("Unable to connect")