def main(): pybt.start_scan() peripheral = pybt.find_peripheral_by_name('LightBlue') battery_service = peripheral['180f'] battery_level_characteristic = battery_service['2a19'] value = battery_level_characteristic.sync_read() print "Battery level: %r" % value
def main(): pybt.start_scan() # find Keyfobdemo peripheral peripheral = pybt.find_peripheral_by_name('Keyfobdemo') print "Found peripheral %s" % (peripheral.name()) # find accelerometer service accelerometer = peripheral['ffa0'] print "Found accelerometer service %s" % (accelerometer.UUID()) # find accelerometer enabler = accelerometer['ffa1'] print "Found accelerometer/enable enabler %s" % enabler.UUID() # say whether accelerometer is enabled enabled = enabler.sync_read() print "Accelerometer enabled: %r" % enabled # turn on accelerometer didWrite = enabler.write_confirm(bytearray([1])) if didWrite: print "Enabled accelerometer" else: print "Could not turn on accelerometer" sys.exit(0) # get XYZ accelerometer characteristics x_axis = accelerometer['ffa3'] print "Found X axis characteristic %s" % (x_axis.UUID()) y_axis = accelerometer['ffa4'] print "Found Y axis characteristic %s" % (y_axis.UUID()) z_axis = accelerometer['ffa5'] print "Found Z axis characteristic %s" % (z_axis.UUID()) # subscribe with synchronous reads print "If you don't get any output here, it's very likely the KeyFobSim app on your phone has crashed. You _need_ to power off your iPhone to fix this :(. Power off phone, power on phone, restart the KeyFobSim app, Ctrl-Z, kill %1 and run again." x_subscription = x_axis.subscribe() y_subscription = y_axis.subscribe() z_subscription = z_axis.subscribe() for i in range(1, 50): x = x_subscription.read() y = y_subscription.read() z = z_subscription.read() print "Accelerometer: X=%r Y=%r Z=%r" % (x, y, z) x_subscription.unsubscribe() y_subscription.unsubscribe() z_subscription.unsubscribe()
def connect(self): pybt.start_scan() log.debug('Connecting to %s...' % self.name) self.peripheral = pybt.find_peripheral_by_name(self.name) log.debug('Found %s' % self.name) self.debug_service = self.peripheral[UUID.SERVICE.DEBUG] log.debug('Found debug service: %s' % self.debug_service.UUID()) self.control = self.debug_service[UUID.CHARACTERISTIC.CONTROL] log.debug('Found control characteristic: %s' % self.control.UUID())
def main(): pybt.start_scan() peripheral = pybt.find_peripheral_by_name('Band (DFU Mode)') print "Found peripheral %s" % (peripheral.name()) service = peripheral['1800'] print "Found service %s" % (service.UUID()) characteristic = service['2a00'] print "Found characteristic %s" % (characteristic.UUID()) print "18002a00 says: %s" % characteristic.sync_read() service = peripheral[BATTERY_SERVICE] characteristic = service[BATTERY_LEVEL_CHARACTERISTIC] print "Battery is: %d" % characteristic.sync_read()[0]
def main(argv): arg_parser = argparse.ArgumentParser(description='Set time on the Band.') arg_parser.add_argument( 'band_name', help='name of the Band, e.g. Band, Andre') args = arg_parser.parse_args(argv[1:]) pybt.start_scan() peripheral = pybt.find_peripheral_by_name(args.band_name) print peripheral service = peripheral[uuid_alpha0_service] print service.UUID() control_characteristic = service[uuid_control_characteristic] print control_characteristic.UUID() data = struct.pack('<BI', ControlCodes.SET_TIME, int(time.time())) control_characteristic.write_no_confirm(bytearray(data))
def main(argv): arg_parser = argparse.ArgumentParser( description= 'Read sensor data from Band and upload it to Ingress server.') arg_parser.add_argument('band_name', help='name of the Band, e.g. Band, Andre') args = arg_parser.parse_args(argv[1:]) pybt.start_scan() peripheral = pybt.find_peripheral_by_name(args.band_name) print peripheral service = peripheral[uuid_alpha0_service] print service.UUID() control_characteristic = service[uuid_control_characteristic] print control_characteristic.UUID() # control_response_characteristic = peripheral[uuid_control_response_characteristic] # print control_response_characteristic.UUID() data_characteristic = service[uuid_data_characteristic] print data_characteristic.UUID() # data_response_characteristic = peripheral[uuid_data_response_characteristic] # print data_response_characteristic.UUID() data_subscription = data_characteristic.subscribe() control_characteristic.write_no_confirm( bytearray([ControlCodes.SEND_SENSOR_DATA])) print "Wrote 0x02 to 0xDEED" packets = [bytearray()] * 15 expected_packets = 0 received_packets = 0 while True: data = data_subscription.read() print "Received %d bytes: %r" % (len(data), data) received_packets += 1 sequence_number = data[0] if sequence_number == 0: print "Got header packet" expected_packets = data[1] packets = packets[:expected_packets] packet = data[2:] else: packet = data[1:] packets[sequence_number] = packet print "Received %d of %d packets" % (received_packets, expected_packets) if received_packets == expected_packets: # last packet received; now remove SHA-1 from packets and compute it all_data = reduce(lambda lhs, rhs: lhs + rhs, packets[0:expected_packets - 1]) actual_sha1 = bytearray(sha.new(all_data).digest()[0:19]) expected_sha1 = packets[-1] print "Actual SHA-1: %r" % actual_sha1 print "Expected SHA-1: %r" % expected_sha1 if actual_sha1 == expected_sha1: print "Data integrity verified." else: print "Uh oh" break
def main(argv): arg_parser = argparse.ArgumentParser(description='Send firmware to Band.') arg_parser.add_argument( 'band_name', help='name of the Band, e.g. Band, Andre') arg_parser.add_argument( 'firmware_path', help='path to firmware .bin file') arg_parser.add_argument( '-w', '--wait', dest='packet_notification_count', help='wait for acknowledgement from Band when sending data after PACKET_NOTIFICATION_COUNT packets (where one packet is %d bytes)' % PACKET_SIZE, type=int, default=0) args = arg_parser.parse_args(argv[1:]) firmware_data = bytearray(open(args.firmware_path, 'rb').read()) firmware_size = len(firmware_data) print "firmware is %d bytes" % firmware_size sha_object = sha.new(firmware_data) firmware_sha1 = sha_object.digest() print "firmware SHA1 is %s" % sha_object.hexdigest() pybt.start_scan() packet_notification_count = args.packet_notification_count # need to implement data_received_handler peripheral = pybt.find_peripheral_by_name(args.band_name) print peripheral dfu = peripheral[uuid_dfu_service] print dfu.UUID() dfu_control_point = dfu[uuid_dfu_control_state_characteristic] print dfu_control_point.UUID() dfu_packet = dfu[uuid_dfu_packet_characteristic] print dfu_packet.UUID() control_point_subscription = dfu_control_point.subscribe() did_write = dfu_control_point.write_confirm(bytearray([OpCodes.START_DFU])) print "wrote START_DFU: %d" % did_write dfu_packet.write_no_confirm(uint32_bytearray(firmware_size)) print "wrote firmware_size=%d" % firmware_size validate_dfu_response(control_point_subscription.read()) did_write = dfu_control_point.write_confirm(bytearray([OpCodes.INITIALIZE_DFU])) print "wrote INITIALIZE_DFU: %d" % did_write dfu_packet.write_no_confirm(bytearray(firmware_sha1)) print "wrote firmware_sha1=%r" % firmware_sha1 if packet_notification_count > 0: did_write = dfu_control_point.write_confirm( bytearray([OpCodes.REQ_PKT_RCPT_NOTIF]) + uint16_bytearray(packet_notification_count)) print "wrote REQ_PKT_RCPT_NOTIF: %d" % did_write did_write = dfu_control_point.write_confirm(bytearray([OpCodes.RECEIVE_FIRMWARE_IMAGE])) print "wrote RECEIVE_FIRMWARE_IMAGE: %d" % did_write validate_dfu_response(control_point_subscription.read()) for packet_count, i in enumerate(range(0, firmware_size, PACKET_SIZE)): data = firmware_data[i:i+PACKET_SIZE] dfu_packet.write_no_confirm(data) sys.stdout.write('.') if packet_notification_count > 0 and packet_count % packet_notification_count == 0: validate_dfu_response(control_point_subscription.read()) print '\nwrote %d bytes (%d packets)' % (firmware_size, packet_count) did_write = dfu_control_point.write_confirm(bytearray([OpCodes.VALIDATE_FIRMWARE_IMAGE])) print "wrote VALIDATE_FIRMWARE_IMAGE: %d" % did_write validate_dfu_response(control_point_subscription.read()) time.sleep(1) did_write = dfu_control_point.write_confirm(bytearray([OpCodes.ACTIVATE_FIRMWARE_AND_RESET])) print "wrote ACTIVATE_FIRMWARE_AND_RESET: %d" % did_write validate_dfu_response(control_point_subscription.read())