def scan_start_cb(data): print("Scan started") def scan_stop_cb(data): print("Scan stopped") #let's start it up again ble.start_scanning(scan_time) try: # initialize BLE driver bledrv.init() # Set GAP name and no security ble.gap("Zerynth",security=(ble.SECURITY_MODE_1,ble.SECURITY_LEVEL_1)) ble.add_callback(ble.EVT_SCAN_REPORT,scan_report_cb) ble.add_callback(ble.EVT_SCAN_STARTED,scan_start_cb) ble.add_callback(ble.EVT_SCAN_STOPPED,scan_stop_cb) #set scanning parameters: every 100ms for 50ms and no duplicates ble.scanning(100,50,duplicates=0) # Start the BLE stack ble.start() # Now start scanning for 30 seconds ble.start_scanning(scan_time) except Exception as e: print(e)
# Let's define some security callbacks def show_key_cb(passkey): print("ENTER THIS PIN ON THE MASTER:", passkey) try: # initialize BLE driver bledrv.init() # Set GAP name and LEVEL 2 security # !!! If security is not set, no secure connection will be possible ble.gap("ZNotifier", security=(ble.SECURITY_MODE_1, ble.SECURITY_LEVEL_2)) # add some GAP callbacks ble.add_callback(ble.EVT_CONNECTED, connection_cb) ble.add_callback(ble.EVT_DISCONNECTED, disconnection_cb) # Create a GATT Service: let's try an Alert Notification Service # (here are the specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.alert_notification.xml) s = ble.Service(0x1811) # The Alert Notification service has multiple characteristics. Let's add them one by one # Create a GATT Characteristic for counting new alerts. # specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.supported_new_alert_category.xml cn = ble.Characteristic(0x2A47, ble.NOTIFY | ble.READ, 16, "New Alerts", ble.BYTES) # Add the GATT Characteristic to the Service s.add_characteristic(cn)
return sleep(1000) ble.confirm_passkey(0) print("Not confirmed!") try: # initialize BLE driver bledrv.init() # Set GAP name and LEVEL 2 security # !!! If security is not set, no secure connection will be possible ble.gap("ZNotifier", security=(ble.SECURITY_MODE_1, ble.SECURITY_LEVEL_2)) # add some GAP callbacks ble.add_callback(ble.EVT_CONNECTED, connection_cb) ble.add_callback(ble.EVT_DISCONNECTED, disconnection_cb) # Create a GATT Service: let's try an Alert Notification Service # (here are the specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.alert_notification.xml) s = ble.Service(0x1811) # The Alert Notification service has multiple characteristics. Let's add them one by one # Create a GATT Characteristic for counting new alerts. # specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.supported_new_alert_category.xml cn = ble.Characteristic(0x2A47, ble.NOTIFY | ble.READ, 16, "New Alerts", ble.BYTES) # Add the GATT Characteristic to the Service s.add_characteristic(cn)
def scan_stop_cb(data): global _isStop _isStop = True gc.collect() #let's start it up again try: # initialize BLE driver bledrv.init() # Set GAP name and no security ble.gap("ct2", security=(ble.SECURITY_MODE_1, ble.SECURITY_LEVEL_1)) ble.add_callback(ble.EVT_CONNECTED, connected_cb) ble.add_callback(ble.EVT_DISCONNECTED, disconnected_cb) service_object_transfer = ble.Service(0x1825) characteristic_object_changed = ble.Characteristic(0x2AC8, ble.NOTIFY | ble.READ, 16, "Object Changed", ble.BYTES) characteristic_object_control = ble.Characteristic(0x2AC5, ble.WRITE, 1, "Object Control Point", ble.BYTES) service_object_transfer.add_characteristic(characteristic_object_changed) service_object_transfer.add_characteristic(characteristic_object_control)
global connected print("Disconnected from", ble.btos(address)) # let's start advertising again ble.start_advertising() connected = False try: # initialize BLE driver bledrv.init() # Set GAP name and no security ble.gap("ZNotifier", security=(ble.SECURITY_MODE_1, ble.SECURITY_LEVEL_1)) # add some GAP callbacks ble.add_callback(ble.EVT_CONNECTED, connection_cb) ble.add_callback(ble.EVT_DISCONNECTED, disconnection_cb) # Create a GATT Service: let's try an Alert Notification Service # (here are the specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.service.alert_notification.xml) s = ble.Service(0x1811) # The Alert Notification service has multiple characteristics. Let's add them one by one # Create a GATT Characteristic for counting new alerts. # specs: https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.supported_new_alert_category.xml cn = ble.Characteristic(0x2A47, ble.NOTIFY | ble.READ, 16, "New Alerts", ble.BYTES) # Add the GATT Characteristic to the Service s.add_characteristic(cn)
payload = payloads[adv_content] ble.advertising(interval, timeout=timeout, payload=payload, mode=ble.ADV_UNCN_UND) ble.start_advertising() print("Advertising restarted with", ble.btos(payload)) try: # initialize BLE driver bledrv.init() # Set GAP name and no security ble.gap("Zerynth", security=(ble.SECURITY_MODE_1, ble.SECURITY_LEVEL_1)) ble.add_callback(ble.EVT_ADV_STOPPED, adv_stop_cb) # set advertising options: advertise every second with custom payload in non connectable undirected mode # after 10 seconds, stop and change payload ble.advertising(100, timeout=10000, payload=payloads[adv_content], mode=ble.ADV_UNCN_UND) # Start the BLE stack ble.start() # Now start scanning for 30 seconds ble.start_advertising() except Exception as e: