def main():
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    #    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = UART.find_device()
        if device is None:
            raise RuntimeError('Failed to find UART device!')
        else:
            print 'uart device is gevonden'
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect(
    )  # Will time out after 60 seconds, specify timeout_sec parameter
    # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    try:
        # Wait for service discovery to complete for the DIS service.  Will
        # time out after 60 seconds (specify timeout_sec parameter to override).
        print('Discovering services...')
        DeviceInformation.discover(device)

        # Once service discovery is complete create an instance of the service
        # and start interacting with it.
        dis = DeviceInformation(device)

        # Print out the DIS characteristics.
        print('Manufacturer: {0}'.format(dis.manufacturer))
        print('Model: {0}'.format(dis.model))
        print('Serial: {0}'.format(dis.serial))
        print('Hardware Revision: {0}'.format(dis.hw_revision))
        print('Software Revision: {0}'.format(dis.sw_revision))
        print('Firmware Revision: {0}'.format(dis.fw_revision))
        print('System ID: {0}'.format(dis.system_id))
        print('Regulatory Cert: {0}'.format(dis.regulatory_cert))
        print('PnP ID: {0}'.format(dis.pnp_id))
    finally:
        # Make sure device is disconnected on exit.
        device.disconnect()
def main(nuimo_uuid):
    print nuimo_uuid
    # Clear previously received data from the controller/adapter
    ble.clear_cached_data()

    # Connect to the default or first adapter and start scan
    adapter = ble.get_default_adapter()
    adapter.power_on()
    adapter.start_scan()

    # It needs bit of time to scan the devices. Some Peripherals advertise in different
    time.sleep(2)

    # Find all the ble devices with the name nuimo,this filtering part is done by Adafruit
    # devices -> interfaces/device/bluez-device, properties and method can be found there
    # id is MAC on Linux and on OSX uuid generated by OSX
    devices = ble.find_devices()

    for device in devices:
        print('Found device: {0}, id: {1}'.format(device.name, device.id))

        if device.id == uuid.UUID(nuimo_uuid):
            device.connect()
            DeviceInformation.discover(device)
            dis = DeviceInformation(device)
            print('Firmware Revision: {0}'.format(dis.fw_revision))
            device.disconnect()

    adapter.power_off()
def main():
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = UART.find_device()
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect()  # Will time out after 60 seconds, specify timeout_sec parameter
                      # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    try:
        # Wait for service discovery to complete for the DIS service.  Will
        # time out after 60 seconds (specify timeout_sec parameter to override).
        print('Discovering services...')
        DeviceInformation.discover(device)

        # Once service discovery is complete create an instance of the service
        # and start interacting with it.
        dis = DeviceInformation(device)

        # Print out the DIS characteristics.
        print('Manufacturer: {0}'.format(dis.manufacturer))
        print('Model: {0}'.format(dis.model))
        print('Serial: {0}'.format(dis.serial))
        print('Hardware Revision: {0}'.format(dis.hw_revision))
        print('Software Revision: {0}'.format(dis.sw_revision))
        print('Firmware Revision: {0}'.format(dis.fw_revision))
        print('System ID: {0}'.format(dis.system_id))
        print('Regulatory Cert: {0}'.format(dis.regulatory_cert))
        print('PnP ID: {0}'.format(dis.pnp_id))
    finally:
        # Make sure device is disconnected on exit.
        device.disconnect()
Example #4
0
def main():
    ble.clear_cached_data()
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    print('Searching for UART device...')
    try:
        adapter.start_scan()
        device = UART.find_device()
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        adapter.stop_scan()

    print("Connecting to a UART device...")
    device.connect()
    print("Connected to " + device.name + "!")

    # call method to launch gui, implement later
    # getch library for sending stuff
    # launchGUI()

    try:
        print('Discovering services...')
        DeviceInformation.discover(device)
        dis = DeviceInformation(device)

        # Print out the DIS characteristics.
        print('Manufacturer: {0}'.format(dis.manufacturer))
        print('Model: {0}'.format(dis.model))
        print('Serial: {0}'.format(dis.serial))
        print('Hardware Revision: {0}'.format(dis.hw_revision))
        print('Software Revision: {0}'.format(dis.sw_revision))
        print('Firmware Revision: {0}'.format(dis.fw_revision))
        print('System ID: {0}'.format(dis.system_id))
        print('Regulatory Cert: {0}'.format(dis.regulatory_cert))
        print('PnP ID: {0}'.format(dis.pnp_id))
    finally:
        device.disconnect()
Example #5
0
 def _find_pulse_data_characteristic(self):
     DeviceInformation.discover(self._device)
     heartbeat_service = None
     for s in self._device.list_services():
         logging.info('Discovered service %s: %s', str(s), s.uuid)
         if s.uuid == DATA_SERVICE_UUID:
             logging.info('Found heartbeat GATT service.')
             heartbeat_service = s
     if not heartbeat_service:
         raise RuntimeError("Couldn't find heartbeat service with uuid %s", DATA_SERVICE_UUID)
     receive_data_characteristic = None
     for c in heartbeat_service.list_characteristics():
         if c.uuid == RECEIVE_CHARACTERISTIC:
             logging.info('Found "receive" characteristic of heartbeat service.')
             receive_data_characteristic = c
     if not receive_data_characteristic:
         raise RuntimeError("Couldn't find receive data characteristic of heartbeat service (uuid %s)",
                            RECEIVE_CHARACTERISTIC)
     return receive_data_characteristic
Example #6
0
def uart_service():
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected UART devices...')
    UART.disconnect_devices()

    # Scan for UART devices.
    print('Searching for UART device...')
    try:
        adapter.start_scan()
        # Search for the first UART device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = UART.find_device()
        if device is None:
            raise RuntimeError('Failed to find UART device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()

    print('Connecting to device...')
    device.connect(
    )  # Will time out after 60 seconds, specify timeout_sec parameter
    # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    try:
        # Wait for service discovery to complete for the UART service.  Will
        # time out after 60 seconds (specify timeout_sec parameter to override).
        print('Discovering services...')
        UART.discover(device)
        DeviceInformation.discover(device)

        # Once service discovery is complete create an instance of the service
        # and start interacting with it.
        uart = UART(device)
        dis = DeviceInformation(device)

        print("Conntected to device: name: {}, id: {}".format(
            device.name, device.id))

        # Write a string to the TX characteristic.
        uart.write('Hello world!\r\n')
        print("Sent 'Hello world!' to the device.")

        # Now wait up to one minute to receive data from the device.
        print('Waiting up to 60 seconds to receive data from the device...')
        while (True):
            received = uart.read(timeout_sec=40)
            if received is not None:
                # Received data, print it out.
                print('Received: {0}'.format(received))
            else:
                # Timeout waiting for data, None is returned.
                print('Received no data!')
    finally:
        print "Interrupted"
        device.disconnect()
Example #7
0
def main():
    #    provider.clear_cached_data()

    adapter = provider.get_default_adapter()

    print "Waiting powered on..."
    adapter.power_on()
    while not adapter.is_powered:
        time.sleep(1)

    print 'Using adapter {0}'.format(adapter.name)

    device = None

    try:
        print 'Start scanning...'
        adapter.start_scan()
        device = provider.find_device([], SENSOR_NAME, SCAN_TIMEOUT)
    finally:
        adapter.stop_scan()

    if device is None:
        print 'SensorTag not found'
        return

    try:
        print 'Connecting...'
        device.connect(CONNECT_TIMEOUT)
    except RuntimeError as e:
        print e
        return

    try:
        print 'Discovering device information...'
        DeviceInformation.discover(device)

        di = DeviceInformation(device)
        print 'Manufacture: {0}'.format(di.manufacturer)
        print 'Model: {0}'.format(di.model)
        print 'Serial: {0}'.format(di.serial)
        print 'Hardware Revision: {0}'.format(di.hw_revision)
        print 'Software Revision: {0}'.format(di.sw_revision)
        print 'Firmware Revision: {0}'.format(di.fw_revision)
        print 'System ID: {0}'.format(di.system_id)

        print 'Discovering service...'
        try:
            device.discover([MOVEMENT_SENSOR_SRV], [
                MOVEMENT_SENSOR_CHR_CONFIG, MOVEMENT_SENSOR_CHR_DATA,
                MOVEMENT_SENSOR_CHR_PERIOD
            ], DISCOVER_TIMEOUT)
        except RuntimeError as e:
            print e
            return

        service = device.find_service(MOVEMENT_SENSOR_SRV)
        if service is None:
            print 'Service not found'
            return

        print 'Setting period...'
        c_period = service.find_characteristic(MOVEMENT_SENSOR_CHR_PERIOD)
        if c_period is None:
            print 'Period Characteristics not found'
            return
        c_period.write_value('\x0a')  # 100 msec

        #
        # Notification callback
        #
        def on_notified(data):
            [gx, gy, gz, ax, ay, az, mx, my, mz] = gam.raw2nineaxis(data)
            ahrs.update(gam.deg2rad(gx), gam.deg2rad(gy), gam.deg2rad(gz), ax,
                        ay, az, mx, my, mz)
            roll, pitch, yaw = gam.quaternion2euler(ahrs.quaternion)
            print '%10.5f %10.5f %10.5f' % (
                math.degrees(roll), math.degrees(pitch), math.degrees(yaw))

        print 'Subsribing notification...'
        c_data = service.find_characteristic(MOVEMENT_SENSOR_CHR_DATA)
        if c_data is None:
            print 'Data Characteristics not found'
            return
        c_data.start_notify(on_notified)

        print 'Enabling notification...'
        c_data._device._peripheral.setNotifyValue_forCharacteristic_(
            True, c_data._characteristic)

        print 'Enabling sensor...'
        c_config = service.find_characteristic(MOVEMENT_SENSOR_CHR_CONFIG)
        if c_config is None:
            print 'Config Characteristics not found'
            return
        c_config.write_value('\x7f\x02')  # ACC RANGE 8G

        print 'Now, sleeping 60 seconds...'
        time.sleep(60)

    finally:
        print 'Disconnecting device...'
        device.disconnect()
def main():
    # Clear any cached data because both bluez and CoreBluetooth have issues with
    # caching data and it going stale.
    ble.clear_cached_data()

    # Get the first available BLE network adapter and make sure it's powered on.
    adapter = ble.get_default_adapter()
    adapter.power_on()
    print('Using adapter: {0}'.format(adapter.name))

    # Disconnect any currently connected UART devices.  Good for cleaning up and
    # starting from a fresh state.
    print('Disconnecting any connected Timer devices...')
    TimerService.disconnect_devices()

    # Scan for devices.
    print('Searching for Timer device...')
    try:
        adapter.start_scan()
        # Search for the first device found (will time out after 60 seconds
        # but you can specify an optional timeout_sec parameter to change it).
        device = ble.find_device(name=device_name)
        if device is None:
            raise RuntimeError('Failed to find Timer device!')
    finally:
        # Make sure scanning is stopped before exiting.
        adapter.stop_scan()
        pass

    print('Connecting to device...')
    device.connect()  # Will time out after 60 seconds, specify timeout_sec parameter
                      # to change the timeout.

    # Once connected do everything else in a try/finally to make sure the device
    # is disconnected when done.
    # Wait for service discovery to complete for the DIS service.  Will
    # time out after 60 seconds (specify timeout_sec parameter to override).
    try:
        print('Discovering services...')
        DeviceInformation.discover(device)

        # Once service discovery is complete create an instance of the service
        # and start interacting with it.
        dis = DeviceInformation(device)

        # Print out the DIS characteristics.
        print('Manufacturer: {}'.format(dis.manufacturer))
        print('Model: {}'.format(dis.model))
        print('Serial: {}'.format(dis.serial))
        print('Hardware Revision: {}'.format(dis.hw_revision))
        print('Software Revision: {}'.format(dis.sw_revision))
        print('Firmware Revision: {}'.format(dis.fw_revision))
        print('System ID: {}'.format(dis.system_id))
        print('Regulatory Cert: {}'.format(dis.regulatory_cert))
        print('PnP ID: {}'.format(dis.pnp_id))
        print('RSSI: {}'.format(device.rssi))

        # loop through services
        for service in device.list_services():
            print("")
            print("Service: {}".format(service.uuid))
            print("Characteristics:")
            for char in service.list_characteristics():
                val = char.read_value()
                print(" {} - {} - {}".format(char.uuid, val, bytes_to_str(val)))
                """
                print(" Descriptors:")
                for desc in char.list_descriptors():
                    try:
                        print("   {} - {}".format(desc.uuid, desc.read_value()))
                    except AttributeError as e:
                        print("   {} - not readable".format(desc.uuid))
                """
    finally:
        # Make sure device is disconnected on exit.
        device.disconnect()