示例#1
0
def main():
    # io_callbacks must be called before `ble.config` to enable
    # Require MITM.
    ble.io_callbacks(display_cb=display_cb,
                     request_cb=request_cb,
                     confirm_cb=confirm_cb)
    ble.config(security=ble.PAIRING_REQUIRE_MITM)
    # Comment the line above, and uncomment the line below to use
    # bonding. Once bonded the pairing activity will no longer be
    # necessary on each connection as long as the keys are retained by
    # the devices.
    # ble.config(security=ble.PAIRING_REQUIRE_MITM | ble.PAIRING_REQUIRE_BONDING)

    print("Connecting")
    conn = ble.gap_connect(address_type, address)
    print("Connected")

    # The delay is not necessary, just here to easily observe the
    # secured vs unsecured state.
    print("Wait for a bit before securing")
    time.sleep(5)

    print("Securing")
    conn.secure(secure_cb)

    print("Sleep forever")
    while True:
        time.sleep(1)
示例#2
0
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from digi import ble


def form_mac_address(addr: bytes) -> str:
    return ":".join('{:02x}'.format(b) for b in addr)

# Turn on Bluetooth
ble.active(True)
print("Started Bluetooth with address of: {}".format(form_mac_address(ble.config("mac"))))

scanner = None
try:
    # Start a scan to run for 30 seconds
    scanner = ble.gap_scan(30000, interval_us=50000, window_us=50000)
    # Loop through all advertisements until the scan has stopped.
    for adv in scanner:
        print(adv)
finally:
    if scanner is not None:
        scanner.stop()

# Scan with a context manager this time.
with ble.gap_scan(30000, interval_us=50000, window_us=50000) as scanner:
    # Loop through the available advertisements, blocking if there are none. Exits when the scan stops.
示例#3
0
    time.sleep(time_sec)
    # stop advertising
    ble.gap_advertise(None)
    # Calculate the number of advertisements sent
    return time_sec * 1000000 // interval_usec


# Advertising constants
TIME_BETWEEN_BEACON_TYPES_SEC = 10
ADVERTISE_INTERVAL_USEC = 50000

# Turn on BLE
ble.active(True)
# Print out the BLE Mac
print("Started Bluetooth with address of: {}".format(
    form_mac_address(ble.config("mac"))))

# The Ranging Data is the Tx power in dBm emitted by the beacon at 0 meters.
calibrated_ranging_data = -10  # This will need to be configured to your beacons actual value.

# The Eddystone TLM frame keeps track of the beacon's time alive and advertisement count.
count = 0
start_time = time.ticks_ms()

# A URL to advertise
url = "http://www.digi.com/"

# This is a unique ID given to each beacon, for this example it will be random.
uid = os.urandom(16)

# The TLM frame advertises the beacon's battery voltage if available. It is set to 0 if unavailable.