### Create a PhysicalWeb (Eddystone) Beacon Source: https://bleson.readthedocs.io/en/latest/examples.html This example shows how to create an Eddystone beacon that advertises a specific URL. The beacon starts, waits for 2 seconds, and then stops. ```python from time import sleep from bleson import get_default_adapter, EddystoneBeacon adapter = get_default_adapter() beacon = EddystoneBeacon(adapter) beacon.url = 'https://www.bluetooth.com/' beacon.start() sleep(2) beacon.stop() ``` -------------------------------- ### Install Bleson on Linux Source: https://bleson.readthedocs.io/en/latest/installing.html Use pip3 to install the Bleson library on Linux systems. ```bash $ sudo pip3 install bleson ``` -------------------------------- ### Build Docs and Run Doctests Source: https://bleson.readthedocs.io/en/latest/development.html This command builds the documentation and simultaneously runs the embedded doctests to verify code examples. ```bash python3 setup.py doctest ``` -------------------------------- ### Advertise as a Bluetooth LE Device Source: https://bleson.readthedocs.io/en/latest/examples.html This example shows how to advertise the host with a specific name ('bleson') as a Bluetooth LE device. The advertisement runs for 10 seconds. ```python from time import sleep from bleson import get_provider, Advertiser, Advertisement adapter = get_provider().get_adapter() advertiser = Advertiser(adapter) advertisement = Advertisement() advertisement.name = "bleson" advertiser.advertisement = advertisement advertiser.start() sleep(10) advertiser.stop() ``` -------------------------------- ### Initialize EddystoneBeacon Source: https://bleson.readthedocs.io/en/latest/api_extra_roles.html Example of initializing an EddystoneBeacon with a URL. The URL is limited to a maximum length of 17 characters. ```python print(EddystoneBeacon('www.bluetooth.com')) ``` -------------------------------- ### Install Bleson on Windows 10 Source: https://bleson.readthedocs.io/en/latest/installing.html Install Bleson using pip on Windows 10. Ensure pip version is 9.0 or newer for native component installation. ```bash $ pip install bleson ``` -------------------------------- ### Install Bleson on macOS Source: https://bleson.readthedocs.io/en/latest/installing.html Install Bleson using pip3 on macOS. You may need sudo if not using Homebrew. ```bash $ pip3 install bleson ``` -------------------------------- ### Run Bleson Observer Source: https://bleson.readthedocs.io/en/latest/cli_tools.html Use this command to scan for nearby Bluetooth LE devices and receive Advertising Reports. No additional setup is required. ```bash $ python3 -m bleson --observer ``` -------------------------------- ### Run Bleson Beacon Source: https://bleson.readthedocs.io/en/latest/cli_tools.html Start a PhysicalWeb (Eddystone) beacon that advertises a specified URL. Ensure the URL is valid. ```bash $ python3 -m bleson --beacon --url http://www.google.com/ ``` -------------------------------- ### Provider Interface Source: https://bleson.readthedocs.io/en/latest/api_internal.html The abstract Provider interface defines a method to get an adapter instance. ```APIDOC ## class bleson.interfaces.provider.Provider ### Description Abstract interface for providers. ### Methods #### get_adapter(adapter_id=None) Return an Adapter instance, default to first one available. ``` -------------------------------- ### Scan for Local Bluetooth LE Devices Source: https://bleson.readthedocs.io/en/latest/examples.html Use this snippet to scan for and print advertising data from nearby Bluetooth LE devices. It starts an observer, waits for 2 seconds, and then stops. ```python from time import sleep from bleson import get_provider, Observer def on_advertisement(advertisement): print(advertisement) adapter = get_provider().get_adapter() observer = Observer(adapter) observer.on_advertising_data = on_advertisement observer.start() sleep(2) observer.stop() ``` -------------------------------- ### Advertiser Role Source: https://bleson.readthedocs.io/en/latest/api_core_roles.html The Advertiser role allows a device to advertise Bluetooth Low Energy data. It provides methods to start and stop the advertising process. ```APIDOC ## Advertiser Role ### Description Manages the advertising capabilities for Bluetooth Low Energy. ### Class `bleson.core.roles.Advertiser` ### Methods #### `start()` Starts the advertising role. #### `stop()` Stops the advertising role. ### Notes macOS implementation note: > You can only set the name and the services UUID’s, and the visibility of your advertisement data depends on your app being a foreground or background process. See the CoreBluetooth documentation. ``` -------------------------------- ### Observer Role Source: https://bleson.readthedocs.io/en/latest/api_core_roles.html The Observer role allows a device to scan for and receive Bluetooth Low Energy advertisements. It provides methods to start and stop the observation process. ```APIDOC ## Observer Role ### Description Manages the observation (scanning) capabilities for Bluetooth Low Energy. ### Class `bleson.core.roles.Observer` ### Methods #### `start()` Starts the observer role. #### `stop()` Stops the observer role. ``` -------------------------------- ### Build Documentation Source: https://bleson.readthedocs.io/en/latest/development.html Execute this command to build the project's documentation using setup.py. ```bash python3 setup.py doc ``` -------------------------------- ### Run Test Suite Source: https://bleson.readthedocs.io/en/latest/development.html Invoke this command to execute the project's test suite via setup.py. ```bash python3 setup.py test ``` -------------------------------- ### Initialize Advertisement with device details Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Shows how to create an Advertisement object, including device address, name, RSSI, and transmit power. The 'data_format' can be specified. ```python print(Advertisement(address=BDAddress('12:34:56:78:90:AB'), name='bleson', rssi=-99, tx_power=0)) ``` -------------------------------- ### Initialize Device with address, name, and RSSI Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Demonstrates initializing a Device object with its address, name, and RSSI value. Requires a BDAddress object for the address. ```python print(Device(address=BDAddress('12:34:56:78:90:AB'), name='bleson', rssi=-99)) ``` -------------------------------- ### Upgrade Development Tools Source: https://bleson.readthedocs.io/en/latest/development.html Run this command to ensure you have the latest versions of pip, setuptools, and wheel for optimal development. ```bash pip3 install --upgrade pip setuptools wheel twine ``` -------------------------------- ### Create a Simple Peripheral Source: https://bleson.readthedocs.io/en/latest/examples.html This snippet demonstrates creating a simple Peripheral with a custom service and characteristic, intended for use with devices like the micro:bit. It adds a service and characteristic for writing data. ```python from time import sleep from bleson import get_default_adapter, Peripheral, Service, Characteristic, CHAR_WRITE adapter = get_default_adapter() peripheral = Peripheral(adapter) MICROBIT_LED_SERVICE = 'E95DD91D-251D-470A-A062-FA1922DFA9A8' MICROBIT_LED_CHAR = 'E95D7B77-251D-470A-A062-FA1922DFA9A8' def on_data_received(bytes): print(bytes) led_service = Service(MICROBIT_LED_SERVICE) led_write_char = Characteristic(MICROBIT_LED_CHAR, CHAR_WRITE) led_write_char.on_data_received = on_data_received led_service.add_characteristic(led_write_char) peripheral.add_service(led_service) peripheral.start() sleep(2) peripheral.stop() ``` -------------------------------- ### Initialize BDAddress with a list Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Shows how to create a BDAddress object from a list of bytes representing the Bluetooth device address. The output format is standardized. ```python print(BDAddress([0xab, 0x90, 0x78, 0x56, 0x34, 0x12])) ``` -------------------------------- ### LinuxProvider Implementation Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Provider interface for Linux using HCI Sockets. ```APIDOC ## class bleson.providers.linux.linux_provider.LinuxProvider ### Description Linux provider implementation. ### Methods #### get_adapter(adapter_id=0) Return an Adapter instance, default to first one available. ``` -------------------------------- ### EddystoneBeacon Initialization Source: https://bleson.readthedocs.io/en/latest/api_extra_roles.html Demonstrates how to initialize an EddystoneBeacon object with a URL. ```APIDOC ## EddystoneBeacon Initialization ### Description Initializes a PhysicalWeb (Eddystone) Beacon Advertiser. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method __init__ ### Endpoint N/A (Class constructor) ### Parameters - **adapter** (bleson.interfaces.adapter.Adapter) – Required - bluetooth adapter - **url** (str) – Required - URL to publish, maximum length of 17 ### Request Example ```python print(EddystoneBeacon('www.bluetooth.com')) ``` ### Response #### Success Response (200) Returns an instance of EddystoneBeacon. #### Response Example ``` ``` ``` -------------------------------- ### MacOSProvider Implementation Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Provider interface for macOS using CoreBluetooth. ```APIDOC ## class bleson.providers.macos.macos_provider.MacOSProvider ### Description macOS provider implementation. ### Methods #### get_adapter(adapter_id=0) Return an Adapter instance, default to first one available. ``` -------------------------------- ### Win32Provider Implementation Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Provider interface for Windows using WinRT. ```APIDOC ## class bleson.providers.win32.win32_provider.Win32Provider ### Description Windows provider implementation. ### Methods #### get_adapter(adapter_id=0) Return an Adapter instance, default to first one available. ``` -------------------------------- ### Initialize BDAddress with a bytearray Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Illustrates creating a BDAddress object from a bytearray. This is an alternative to using a list for initializing the device address. ```python print(BDAddress(bytearray([0xab, 0x90, 0x78, 0x56, 0x34, 0x12]))) ``` -------------------------------- ### BluetoothAdapter Implementation (Windows) Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Adapter interface for Windows using WinRT. ```APIDOC ## class bleson.providers.win32.win32_adapter.BluetoothAdapter ### Description Windows Bluetooth adapter implementation. ### Methods #### off() Power off the adapter hardware. #### on() Power on the adapter hardware. #### open() Initialise the adapter. #### start_advertising(advertisement, scan_response=None) Start BLE advertising. #### start_scanning() Start BLE scanning. #### stop_advertising() Stop BLE advertising. #### stop_scanning() Stop BLE scanning. ``` -------------------------------- ### CoreBluetoothAdapter Implementation (macOS) Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Adapter interface for macOS using CoreBluetooth. ```APIDOC ## class bleson.providers.macos.macos_adapter.CoreBluetoothAdapter ### Description macOS CoreBluetooth adapter implementation. ### Methods #### off() Power off the adapter hardware. #### on() Power on the adapter hardware. #### open() Initialise the adapter. #### start_advertising(advertisement, scan_response=None) Start BLE advertising. #### start_scanning() Start BLE scanning. #### stop_advertising() Stop BLE advertising. #### stop_scanning() Stop BLE scanning. ``` -------------------------------- ### Initialize UUID16 with an integer Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Demonstrates initializing a UUID16 object with a 16-bit integer value. The byte order defaults to little-endian. ```python print(UUID16(0xFFFF)) ``` -------------------------------- ### BluetoothHCIAdapter Implementation (Linux) Source: https://bleson.readthedocs.io/en/latest/api_internal.html Implementation of the Adapter interface for Linux using Bluetooth HCI Sockets. ```APIDOC ## class bleson.providers.linux.linux_adapter.BluetoothHCIAdapter ### Description Linux Bluetooth HCI adapter implementation. ### Methods #### off() Power off the adapter hardware. #### on() Power on the adapter hardware. #### open() Initialise the adapter. #### start_advertising(advertisement, scan_response=None) Start BLE advertising. #### start_scanning() Start BLE scanning. #### stop_advertising() Stop BLE advertising. #### stop_scanning() Stop BLE scanning. ``` -------------------------------- ### Initialize UUID16 with a list Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Shows how to initialize a UUID16 object using a list of bytes. The bytes are interpreted in little-endian order by default. ```python print(UUID16([0x34, 0x12])) ``` -------------------------------- ### Grant Bluetooth LE Permissions on Linux Source: https://bleson.readthedocs.io/en/latest/installing.html Grant the Python 3 binary necessary permissions to access the Bluetooth LE adapter without root privileges using setcap. ```bash sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f `which python3`) ``` -------------------------------- ### Adapter Interface Source: https://bleson.readthedocs.io/en/latest/api_internal.html The abstract Adapter interface outlines core BLE operations. ```APIDOC ## class bleson.interfaces.adapter.Adapter ### Description Adapter interface. ### Methods #### off() Power off the adapter hardware. #### on() Power on the adapter hardware. #### open() Initialise the adapter. #### start_advertising(advertisement, scan_response=None) Start BLE advertising. #### start_scanning() Start BLE scanning. #### stop_advertising() Stop BLE advertising. #### stop_scanning() Stop BLE scanning. ``` -------------------------------- ### Advertisement Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Represents a Bluetooth LE Advertisement Report. Initialized with various advertisement details. ```APIDOC ## Class: Advertisement ### Description Bluetooth LE AdvertisementReport ### Parameters * **address** (BDAddress) - Bluetooth Device Adress * **name** (str) - device name * **rssi** (integer) - device RSSI * **tx_power** (integer) - device transmit power * **raw_data** (bytearray on Linux (__HCI data__) or a dictionary on macOS/Win) - pre-rolled advertisement payload * **data_format** (str) - 'simple' by default ### Example ```python print(Advertisement(address=BDAddress('12:34:56:78:90:AB'), name='bleson', rssi=-99, tx_power=0)) ``` ``` -------------------------------- ### Tag Release Source: https://bleson.readthedocs.io/en/latest/development.html Use this command as part of the release process to tag the current version in Git. ```bash python3 setup.py tag ``` -------------------------------- ### bleson.get_provider Source: https://bleson.readthedocs.io/en/latest/api_other.html Obtain the Bluetooth LE provider for the current platform (Linux/macOS/Windows). ```APIDOC ## bleson.get_provider ### Description Obtain the Bluetooth LE provider for the current platform (Linux/macOS/Windows). ### Method Python Function Call ### Endpoint N/A (Python function) ### Parameters None ### Request Example ```python provider = bleson.get_provider() ``` ### Response #### Success Response - **provider** (object) - The Bluetooth LE provider instance for the current platform. ``` -------------------------------- ### Device Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Represents Bluetooth LE Device Information. Initialized with address, name, and RSSI. ```APIDOC ## Class: Device ### Description Bluetooth LE Device Info. ### Parameters * **address** (BDAddress) - Bluetooth Device Address * **name** (str) - device name * **rssi** (integer) - device RSSI ### Example ```python print(Device(address=BDAddress('12:34:56:78:90:AB'), name='bleson', rssi=-99)) ``` ``` -------------------------------- ### Initialize UUID128 with a list Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Illustrates initializing a UUID128 object from a list of bytes. The list represents the 128-bit UUID, and the byte order is configurable. ```python print(UUID128([0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00])) ``` -------------------------------- ### Initialize UUID128 with an integer Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Demonstrates initializing a UUID128 object with a 16-bit integer. The object will represent a full 128-bit UUID based on this integer. ```python print(UUID128(0x1234)) ``` -------------------------------- ### EddystoneBeacon Initialization Output Source: https://bleson.readthedocs.io/en/latest/api_extra_roles.html The expected output when an EddystoneBeacon object is initialized and printed. ```python ``` -------------------------------- ### Role Interface Source: https://bleson.readthedocs.io/en/latest/api_internal.html The abstract Role interface defines methods for managing roles. ```APIDOC ## class bleson.interfaces.role.Role ### Description Role interface. ### Methods #### start() Start the role. #### stop() Stop the role. ``` -------------------------------- ### Stop Bluetooth Service on Linux Source: https://bleson.readthedocs.io/en/latest/installing.html Recommended to stop the bluetoothd service on Linux when using Bleson directly with the HCI socket interface. ```bash sudo service bluetooth stop ``` -------------------------------- ### bleson.logger.set_level Source: https://bleson.readthedocs.io/en/latest/api_other.html Set the Bleson module logging level. ```APIDOC ## bleson.logger.set_level ### Description Set the Bleson module logging level. ### Method Python Function Call ### Endpoint N/A (Python function) ### Parameters #### Path Parameters - **level** (int) - Required - The desired logging level (e.g., logging.DEBUG, logging.INFO). ### Request Example ```python import logging bleson.logger.set_level(logging.INFO) ``` ### Response #### Success Response None ``` -------------------------------- ### ValueObject Interface Source: https://bleson.readthedocs.io/en/latest/api_internal.html The ValueObject interface provides methods for byte representation and length. ```APIDOC ## class bleson.core.types.ValueObject ### Description Value object interface. ### Methods #### __bytes__() Return bytes in little endian byte order. #### __len__() ``` -------------------------------- ### UUID16 Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Represents a 16-bit UUID. Can be initialized with a 16-bit integer or a list of bytes. ```APIDOC ## Class: UUID16 ### Description 16Bit UUID Type. ### Parameters * **uuid** (string, list, tuple, bytes or bytearray) - The 16bit uuid source value * **little_endian** (bool) - Byte order, default True ### Example ```python print(UUID16(0xFFFF)) print(UUID16([0x34, 0x12])) ``` ``` -------------------------------- ### BDAddress Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Represents a Bluetooth Device Address. Can be initialized with a list of bytes or a bytearray. ```APIDOC ## Class: BDAddress ### Description Bluetooth Device Address. ### Parameters * **address** (optional) - The device bluetooth address ### Example ```python print(BDAddress([0xab, 0x90, 0x78, 0x56, 0x34, 0x12])) print(BDAddress(bytearray([0xab, 0x90, 0x78, 0x56, 0x34, 0x12]))) ``` ``` -------------------------------- ### UUID128 Source: https://bleson.readthedocs.io/en/latest/api_datatypes.html Represents a 128-bit UUID. Can be initialized with a list of bytes or a 16-bit integer. ```APIDOC ## Class: UUID128 ### Description 128 Bit Type. ### Parameters * **uuid** (string, list, tuple, bytes or bytearray) - The 16bit uuid source value * **little_endian** (bool) - Byte order, default True ### Example ```python print(UUID128([0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15, 0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00])) print(UUID128(0x1234)) ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.