示例#1
0
 def __init__(self, connect_callback, disconnect_callback):
     BTAgent.__init__(self, default_pin_code=config.get('bluez', 'pin_code') or '0000', cb_notify_on_authorize=self.auto_accept_one)
     self.adapter = BTAdapter(config.get('bluez', 'device_path'))
     self.adapter.set_property('Discoverable', config.getboolean('bluez', 'discoverable'))
     self.allowed_uuids = [ SERVICES["AdvancedAudioDistribution"].uuid, SERVICES["AVRemoteControl"].uuid ]
     self.connected = None
     self.tracked_devices =  []
     self.connect_callback = connect_callback
     self.disconnect_callback = disconnect_callback
     self.update_discoverable()
示例#2
0
class AutoAcceptSingleAudioAgent(BTAgent):
    """
    Accepts one client unconditionally and hides the device once connected.
    As long as the client is connected no other devices may connect.
    This 'first comes first served' is not necessarily the 'bluetooth way' of
    connecting devices but the easiest to implement.
    """
    def __init__(self):
        BTAgent.__init__(self, auto_authorize_connections=False, cb_notify_on_authorize=self.auto_accept_one, default_pass_key=7130, cb_notify_on_request_confirmation=self.confirm_request, cb_notify_on_request_pin_code=self.pincode_request, cb_notify_on_request_pass_key=self.passkey_request)
        self.adapter = BTAdapter(config.get('bluez', 'device_path'))
        self.allowed_uuids = [ SERVICES["AdvancedAudioDistribution"].uuid, SERVICES["AVRemoteControl"].uuid ]
        self.connected = None
        self.tracked_devices =  []
        self.update_discoverable()

    def update_discoverable(self):
        if bool(self.connected):
            print("Hiding adapter from all devices.")
            self.adapter.set_property('Discoverable', False)
        else:
            print("Showing adapter to all devices.")
            self.adapter.set_property('Discoverable', True)

    def confirm_request(self, *arg):
        print("confirm")
        print(arg)
        return True

    def pincode_request(self, *arg):
        print("pin")
        print(arg)
        return 7130

    def passkey_request(self, *arg):
        print("Passkey request")
        print(arg)
        print("EOR")
        return "7130"

    def auto_accept_one(self, method, device, uuid):
        #print("lol")
        #print(args)
        #return True
        if not BTUUID(uuid).uuid in self.allowed_uuids: return False
        if self.connected and self.connected != device:
            print("Rejecting device, because another one is already connected. connected_device=%s, device=%s" % (self.connected, device))
            return False

        # track connection state of the device (is there a better way?)
        if not device in self.tracked_devices:
            self.tracked_devices.append(device)
            self.adapter._bus.add_signal_receiver(self._track_connection_state,
                                                  path=device,
                                                  signal_name='PropertiesChanged',
                                                  dbus_interface='org.freedesktop.DBus.Properties',
                                                  path_keyword='device')

        return True

    def _track_connection_state(self, addr, properties, signature, device):
        if self.connected and self.connected != device: return
        if not 'Connected' in properties: return

        if not self.connected and bool(properties['Connected']):
            print("Device connected. device=%s" % device)
            self.connected = device
            self.update_discoverable()
            subprocess.Popen(config.get('btaudio', 'connect_command'), shell=True)
        elif self.connected and not bool(properties['Connected']):
            print("Device disconnected. device=%s" % device)
            self.connected = None
            self.update_discoverable()
            subprocess.Popen(config.get('btaudio', 'disconnect_command'), shell=True)
示例#3
0
class AutoAcceptSingleAudioAgent(BTAgent):
    """
    Accepts one client unconditionally and hides the device once connected.
    As long as the client is connected no other devices may connect.
    This 'first comes first served' is not necessarily the 'bluetooth way' of
    connecting devices but the easiest to implement.
    """
    def __init__(self, connect_callback, disconnect_callback, track_callback):
        BTAgent.__init__(self,
                         default_pin_code=config.get('bluez', 'pin_code')
                         or '0000',
                         cb_notify_on_authorize=self.auto_accept_one)
        self.adapter = BTAdapter(config.get('bluez', 'device_path'))
        self.adapter.set_property('Discoverable',
                                  config.getboolean('bluez', 'discoverable'))
        self.allowed_uuids = [
            SERVICES["AdvancedAudioDistribution"].uuid,
            SERVICES["AVRemoteControl"].uuid
        ]
        self.connected = None
        self.tracked_devices = []
        self.connect_callback = connect_callback
        self.disconnect_callback = disconnect_callback
        self.track_callback = track_callback
        self.update_discoverable()

    def update_discoverable(self):
        if not config.getboolean('bluez', 'discoverable'):
            return

        if bool(self.connected):
            print("Hiding adapter from all devices.")
            self.adapter.set_property('Discoverable', False)
        else:
            print("Showing adapter to all devices.")
            self.adapter.set_property('Discoverable', True)

    def auto_accept_one(self, method, device, uuid):
        if not BTUUID(uuid).uuid in self.allowed_uuids: return False
        if self.connected and self.connected != device:
            print(
                "Rejecting device, because another one is already connected. connected_device=%s, device=%s"
                % (self.connected, device))
            return False

        # track connection state of the device (is there a better way?)
        if not device in self.tracked_devices:
            self.tracked_devices.append(device)
            self.adapter._bus.add_signal_receiver(
                self._track_connection_state,
                path=device,
                signal_name='PropertiesChanged',
                dbus_interface='org.freedesktop.DBus.Properties',
                path_keyword='device')
            self.adapter._bus.add_signal_receiver(
                self._watch_track,
                path=device + '/player0',
                signal_name='PropertiesChanged',
                dbus_interface='org.freedesktop.DBus.Properties',
                path_keyword='device')

        return True

    def _watch_track(self, addr, properties, signature, device):
        if not 'Track' in properties: return
        self.track_callback(properties['Track'])

    def _track_connection_state(self, addr, properties, signature, device):
        if self.connected and self.connected != device: return
        if not 'Connected' in properties: return

        if not self.connected and bool(properties['Connected']):
            print("Device connected. device=%s" % device)
            self.connected = device
            self.update_discoverable()
            self.connect_callback()

        elif self.connected and not bool(properties['Connected']):
            print("Device disconnected. device=%s" % device)
            self.connected = None
            self.update_discoverable()
            self.disconnect_callback()