Package: py-ble-manager
py_ble_managerA python library for controlling Renesas BLE devices.PurposeThe intent of this library is to provide a python interface similar toSDK10for controlling BLE of DA14xxx devices. This is achieved by communicating with a development kit running Generic Transport Layer (GTL) supported firmware over a USB port on your PC:The primary intent is for use as a central device for benchtop testing, continuous integration, or as an end-of-line tool. For additional information on the GTL please see theGTL User Manual.Quick StartRefer to thehardware setupto setup the jumpers on your development kit.Call:pip install py-ble-manager[dev]to install thepy_ble_managerpackage and its dependencies.NOTE:Specifying [dev] will install optional dependency:prompt_toolkit.prompt_toolkitis used in some of the examples to provide a command line interface.NOTE:This library requires Python v3.10.5 or later.NOTE:It is recommended to install the library using a virtual environment. To setup a virtual environment usingvenvcall:$ python -m venv ./<name_of_your_env>. Note to create a virtual environment that uses Python 3.10.5, you must already have Python 3.10.5 downloaded on your computer. To use the above command to create a Python 3.10.5 environment, Python 3.10.5 must be configured in your PATH. You can download it from thepython website.Downloadthe py_ble_manager compatible firmware binary to the development kit by calling thepy_ble_manager_programmerutility from the terminal.The package is now installed and you are ready to run one of theexamples!Basic UsageCreate a BLE Central object and perform initializationimportpy_ble_managerasblecentral=ble.BleCentral("COM54")# Initialize the Python BLE Frameworkcentral.init()# Start operating as a BLE Centralcentral.start()# Set the IO capabilitiescentral.set_io_cap(ble.GAP_IO_CAPABILITIES.GAP_IO_CAP_KEYBOARD_DISP)Initiate a BLE OperationSome examples include:Scanning:central.scan_start(type=ble.GAP_SCAN_TYPE.GAP_SCAN_ACTIVE,mode=ble.GAP_SCAN_MODE.GAP_SCAN_GEN_DISC_MODE,interval_ms=100,window_ms=50,filt_wlist=False,filt_dupl=True)Connecting:peripheral_addr:ble.BdAddress=ble.BleUtils.str_to_bd_addr("48:23:35:00:1b:53,P")connection_params=ble.GapConnParams(interval_min_ms=50,interval_max_ms=70,slave_latency=0,sup_timeout_ms=420)central.connect(peripheral_addr,connection_params)Read a characteristic valuecentral.read(conn_idx=0,handle=24,offset=0)Write a characteristic valuecentral.write(conn_idx=0,handle=24,offset=0,value=1234)Disconnectcentral.disconnect(conn_idx=0)Handle asynchronous eventsThe framework returns asynchronous events to the application through an event queue. CallingBleCentral.get_event()will get an event from the queue. All of the events returned byBleCentral.get_event()are a subclass ofBleEventBase.
A variety of different events occur throughout the life a BLE application. Some example events includeBleEventGapConnectionCompleted,BleEventGapDisconnected,BleEventGattcReadCompleted,BleEventGattcWriteCompleted.
Each event has anevt_codeto identify the type of event.For example, after you initiate a write you will receive aBleEventGattcWriteCompletedevent which has anevt_codeofBLE_EVT_GATTC.BLE_EVT_GATTC_WRITE_COMPLETED. Your application can
handle the event however it sees fit. If your application does not handle the event, callBleCentral.handle_event_default()to have the BLE framework process the event for you.# This call will block until an event is available. Use the timeout parameter to block for a specified period of timeevt=central.get_event()# Determine which event occurred. It will be of type BLE_EVT_GAP, BLE_EVT_GATTC, or BLE_EVT_GATTSmatchevt.evt_code:# Handle the eventcaseble.BLE_EVT_GAP.BLE_EVT_GAP_ADV_REPORT:# Define your own handling function to process the eventhandle_evt_gap_adv_report(evt)caseble.BLE_EVT_GAP.BLE_EVT_GAP_SCAN_COMPLETED:handle_evt_gap_scan_completed(evt)caseble.BLE_EVT_GAP.BLE_EVT_GAP_CONNECTED:handle_evt_gap_connected(evt)caseble.BLE_EVT_GAP.BLE_EVT_GAP_CONNECTION_COMPLETED:handle_evt_gap_connection_completed(evt)caseble.BLE_EVT_GAP.BLE_EVT_GAP_DISCONNECTED:handle_evt_gap_disconnected(evt)caseble.BLE_EVT_GATTC.BLE_EVT_GATTC_BROWSE_SVC:handle_evt_gattc_browse_svc(evt)caseble.BLE_EVT_GATTC.BLE_EVT_GATTC_BROWSE_COMPLETED:handle_evt_gattc_browse_completed(evt)caseble.BLE_EVT_GATTC.BLE_EVT_GATTC_NOTIFICATION:handle_evt_gattc_notification(evt)caseble.BLE_EVT_GATTC.BLE_EVT_GATTC_WRITE_COMPLETED:handle_evt_gattc_write_completed(evt)caseble.BLE_EVT_GATTC.BLE_EVT_GATTC_READ_COMPLETED:handle_evt_gattc_read_completed(evt)case_:# For any events not handled by your application, call the BleCentral default handler to process the eventcentral.handle_event_default(evt)ArchitectureRefer to thearchitecturedescription.