Пример #1
0
    def start(self):
        """Start the application.

        This function performs the following steps to start the BLE peripheral
        application:

        1. Registers the Bluetooth adapter and turns it on.
        2. Gets the Bluez D-Bus advertising manager interface.
        3. Gets the Bluez D-Bus gatt manager interface.
        4. Creates an advertisement with the primary application service.
        5. Registers the advertisement with the Bluez advertising manager.
        6. Registers the application with the Bluez gatt manager.
        7. Runs the program loop

        The application must first have had a service added to it.

        :Example:

        >>> app = peripheral.Application()
        >>> app.add_service(your_service)
        >>> app.start()

        It is good practice to put the ``app.start()`` in a
        ``try-except-finally`` block to enable keyboard interrupts.
        """
        # Register the Bluetooth adapter
        self.dongle.powered('on')

        # Setup the advertising manager
        print('setup ad_manager')
        self.ad_manager = tools.get_advert_manager_interface()

        # Setup the service manager
        print('setup service_manager')
        self.service_manager = tools.get_gatt_manager_interface()

        # Setup the advertisement
        self.service_ad = Advertisement(self, 'peripheral')
        for service in self.services:
            if service.primary:
                print('Advertising service ', service.uuid)
                self.service_ad.add_service_uuid(service.uuid)
                self.service_ad.ad_type = service.type
                if service.service_data is not None:
                    print('Adding service data: ',
                          service.uuid,
                          service.service_data)
                    self.service_ad.add_service_data(service.uuid,
                                                     service.service_data)

        # Register the advertisement
        print('Register Adver', self.service_ad.get_path())
        self.ad_manager.RegisterAdvertisement(
            self.service_ad.get_path(), {},
            reply_handler=register_ad_cb,
            error_handler=register_ad_error_cb)

        # Register the application
        print('Register Application ', self.get_path())
        self.service_manager.RegisterApplication(
            self.get_path(), {},
            reply_handler=register_service_cb,
            error_handler=register_service_error_cb)
        try:
            # Run the mainloop
            self.mainloop.run()
        except KeyboardInterrupt:
            print('Closing Mainloop')
            self.mainloop.quit()
Пример #2
0
    def start(self):
        """Start the application.

        This function performs the following steps to start the BLE peripheral
        application:

        1. Registers the Bluetooth adapter and turns it on.
        2. Gets the Bluez D-Bus advertising manager interface.
        3. Gets the Bluez D-Bus gatt manager interface.
        4. Creates an advertisement with the primary application service.
        5. Registers the advertisement with the Bluez advertising manager.
        6. Registers the application with the Bluez gatt manager.
        7. Runs the program loop

        The application must first have had a service added to it.

        :Example:

        >>> app = peripheral.Application()
        >>> app.add_service(your_service)
        >>> app.start()

        It is good practice to put the ``app.start()`` in a
        ``try-except-finally`` block to enable keyboard interrupts.
        """
        # Register the Bluetooth adapter
        self.dongle.powered = True

        # Setup the advertising manager
        print('setup ad_manager')
        self.ad_manager = tools.get_advert_manager_interface()

        # Setup the service manager
        print('setup service_manager')
        self.service_manager = tools.get_gatt_manager_interface()

        # Setup the advertisement
        self.service_ad = Advertisement(self, 'peripheral')
        for service in self.services:
            if service.primary:
                print('Advertising service ', service.uuid)
                self.service_ad.add_service_uuid(service.uuid)
                self.service_ad.ad_type = service.type
                if service.service_data is not None:
                    print('Adding service data: ', service.uuid,
                          service.service_data)
                    self.service_ad.add_service_data(service.uuid,
                                                     service.service_data)

        # Register the advertisement
        print('Register Adver', self.service_ad.get_path())
        self.ad_manager.RegisterAdvertisement(
            self.service_ad.get_path(), {},
            reply_handler=register_ad_cb,
            error_handler=register_ad_error_cb)

        # Register the application
        print('Register Application ', self.get_path())
        self.service_manager.RegisterApplication(
            self.get_path(), {},
            reply_handler=register_service_cb,
            error_handler=register_service_error_cb)
        try:
            # Run the mainloop
            self.mainloop.run()
        except KeyboardInterrupt:
            print('Closing Mainloop')
            self.mainloop.quit()