def adv_stop_cb(data):
    global pdu_count, adv_content
    print("Advertising stopped")
    adv_content = (adv_content + 1) % 3
    if adv_content == 0:
        # advertise UID
        interval = 100
        timeout = 10000
    elif adv_content == 1:
        # advertise URL
        interval = 100
        timeout = 15000
    else:
        # advertise TLM
        interval = 100
        timeout = 150
        pdu_count += 1
        payloads[2] = bb.eddy_encode_tlm(battery_level, temperature, pdu_count,
                                         uptime.get() /
                                         1000)  # TLM Eddystone payload

    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))
def disconnection_cb(address):
    global connected
    print("Disconnected from", ble.btos(address))
    # let's start advertising again
    ble.start_advertising()
    connected = False
    ble.security(capabilities=ble.CAP_DISPLAY_ONLY,
                 bonding=ble.AUTH_BOND,
                 scheme=ble.AUTH_SC | ble.AUTH_MITM,
                 key_size=16,
                 passkey=225575)
    # To do so, we need a callback to display the passkey when needed
    ble.add_callback(ble.EVT_SHOW_PASSKEY, show_key_cb)

    # Setup advertising to 50ms
    ble.advertising(50)

    # Start the BLE stack
    ble.start()

    # Now start advertising
    ble.start_advertising()

except Exception as e:
    print(e)

# loop forever
while True:
    print(".")
    if random(0, 100) < 50 and notifications_enabled and connected:
        value = bytearray(cn.get_value())
        value[0] = 0  # simple alert type
        if value[1] < 255:
            value[1] = value[1] + 1  # add a notification
        print("Adding a new notification, total of", value[1])
        # the remaining 14 bytes can be some text
        value[2:10] = "Zerynth!"
Exemple #4
0
def disconnected_cb(address):
    global connected
    print(ble.btos(address))
    ble.start_advertising()
    connected = False
    set_disconnect_led()