MICILE EZ_Bluetooth Documentation
version: 0.9
A few notes about our documentation:
  • The intentions of this documentation are to keep things simple.
  • Please see the examples at the end of each section for concrete examples of how to use each function.
  • If in doubt how to use a function, experiment!
  • We don't believe in writing more words just to make the documentation look more substantial
  • Quick Reference
    Function List
    Compatible Devices
    Rocketfish Bluetooth USB Adapter
    Any brand recent Broadcom chipset USB bluetooth adapters should work.
    Generic 5V Bluetooth Wireless RS232 TTL Module
    eBay / Amazon
    Generic Bluetooth Wireless RS232 TTL Module
    eBay / Amazon
    Generic Bluetooth ELM327 OBD2 Dongle
    See ez_obd2 Library
    eBay / Amazon
    GlobalSat BT-308 Bluetooth GPS Receiver
    See the ez_gps library for demo. Any NMEA compatible Bluetooth GPS receiver should work.

    Any bluetooth devices that support the Bluetooth SPP profile are compatible

    * Please contact us for information on adding support for a device.


    Bluetooth Functions
    Note! device_addr parameters should specify the MAC address of a bluetooth device as a string in the format "xx:xx:xx:xx:xx:xx" i.e. "00:10:07:06:00:12"
    bluetooth_discover_devices([scantime])
    Discovers bluetooth devices in range. An optional scantime can be specified to set the amount of time to
    scan for in seconds. If scantime is not specified, then scanning will occur for twenty seconds. Some devices
    may require a long scantime to wake up and be detected.

    The function returns a dictionary of devices with the keys being the device address of the detected bluetooth device. Typical
    properties of each device are Name, Paired, Alias, Address, RSSI, and Class
    bluetooth_is_device_paired(device_addr)
    Returns True if the device specified in the device_addr parameter has already been paired, returns False if the device has not been paired succesfully.
    bluetooth_pair_with_device(device_addr, [pincode])
    Pairs the micile tablet with a bluetooth device. The device to pair with is specified in the device_addr parameter.

    If the device requires a pincode, the pincode should be specified in the optional pincode parameter as a string. i.e. "1234"

    Before a connection can be made with a device, it must be paired.
    bluetooth_spp_connect(device_addr)
    Creates a bluetooth serial port connection to the bluetooth device with MAC address specifed in the device_addr directory.

    The device must be already paired for this function to succeed.

    Returns the name of the serial port which is bound to the bluetooth serial port connection.
    bluetooth_spp_disconnect(device_addr)
    Disconnects an existing bluetooth serial port connection that was previously created using the bluetooth_spp_conect function.
    bluetooth_unpair_with_device(device_addr)
    Unpairs the micile tablet with the bluetooth device which has the MAC address specified by the device_addr parameter.
    This effectively makes the micile tablet forget the pincode for the device.
    # --------------------------------------------------
    # bluetooth_discovery_demo.py
    # --------------------------------------------------
     
    # import the graphics library and the bluetooth library
    from ez_bluetooth_09 import *
    from ez_graphics_09 import *
     
    # clear
    clear_screen('black')
    set_text_size(20)
    draw_text('Discovering Devices'1010)
     
    # discover devices
    devs = bluetooth_discover_devices()
     
    # loop through all of the discovered devices
    y = 30
    for dev_addr in devs:
        # get the device
        device = devs[dev_addr]
        
        # get the properties of the device
        bt_name    = device['Name']     # linvor
        bt_paired  = device['Paired']   # True
        bt_alias   = device['Alias']    # linvor
        bt_address = device['Address']  # 00:10:07:06:00:12
        bt_rssi    = device['RSSI']     # -63
        bt_class   = device['Class']    # 7936
        
        # print to the debug window
        s = "%s %s %s %s %s %s" % (bt_namebt_pairedbt_aliasbt_addressbt_rssibt_class) 
        draw_text(s50y'yellow')
        y = y + 20

    Screen Shot
    # --------------------------------------------------
    # bluetooth_serialport_demo.py
    # --------------------------------------------------
     
    # import the graphics library and the bluetooth library
    from ez_graphics_09 import *
    from ez_bluetooth_09 import *
    import serial
    import time
     
    # clear the screen and start writing text
    set_text_size(30)
    clear_screen('black')
    draw_text('Bluetooth Demo'1010'white')
     
    # Constants - Change as needed to suit bluetooth device
    BT_DEVICE_ADDRESS='00:10:07:06:00:12'
    BT_DEVICE_PINCODE='1234'
     
    # For testing purposes, we will always unpair the device first
    bluetooth_unpair_with_device(BT_DEVICE_ADDRESS)
     
    # Check if we need to pair with the device
    draw_text('Checking if device %s is paired' % BT_DEVICE_ADDRESS1050'white')
    if bluetooth_is_device_paired(BT_DEVICE_ADDRESS) == False:
        # we could be more user friendly here and use the ez_ui libraries to show a keyboard and ask
        # the user to input a pincode, but for now, just use the preset PINCODE
        draw_text('Pairing the device with pincode of %s' % BT_DEVICE_PINCODE1090'white')
        try:
            bluetooth_pair_with_device(BT_DEVICE_ADDRESSBT_DEVICE_PINCODE)
        except Exceptione:
            # most likely exception is Authentication Failed because of wrong pincode
            draw_text('Pairing Error: %s' % e10130'red')
            exit()
     
     
    # Connect to the bluetooth serial port
    draw_text('Connecting to bluetooth serial port'10130'white')
    bt_serial_port_name = bluetooth_spp_connect(BT_DEVICE_ADDRESS)
    bt_serial_port = serial.Serial(bt_serial_port_nametimeout=0.01)
    bt_serial_port.write('Ready!\r\n')
     
    # Loop to read and write to the serial port
    draw_text('Reading and Writing to serial port'10170'white')
    fill_rect(1021010040'black''yellow')
    while True:
        # this should be replaced with code that actually does something with your bluetooth device
        # here we will just read data, print it to the screen and send the same data back to the client with a star after it
        s = bt_serial_port.read()
        if s != '':
            fill_rect(1021010040'black''yellow')
            draw_text(s12212'cyan')
            bt_serial_port.write(s + '*')

    Screen Shot