def main(): print(" +---------------------------------------------------+") print(" | XBee Python Library Receive Bluetooth File Sample |") print(" +---------------------------------------------------+\n") file = None device = XBeeDevice(PORT, BAUD_RATE) try: device.open() def bluetooth_data_callback(data): global file # Check if the data is 'START' or 'END'. if data.startswith(bytearray(MSG_START, 'utf-8')): # Get the file name. file_name = data.decode('utf-8').split(SEPARATOR)[1] # Open the file for writing. file = open(file_name, "w+b") print(">> START message received, saving data to file...") send_ack(device) elif data == bytearray(MSG_END, 'utf-8'): file.close() print(">> END message received, file '%s'\n" % file.name) send_ack(device) elif file is not None: payload = data[:-1] checksum = data[-1] # Validate the checksum. if 0xFF - (sum(payload) & 0xFF) == checksum: # Write block to file. file.write(payload) send_ack(device) device.add_bluetooth_data_received_callback(bluetooth_data_callback) print("Waiting for data from the Bluetooth interface...\n") input() finally: if file is not None and file.closed: file.close() if device is not None and device.is_open(): device.close()
def main(): print(" +---------------------------------------------------+") print(" | XBee Python Library Receive Bluetooth Data Sample |") print(" +---------------------------------------------------+\n") device = XBeeDevice(PORT, BAUD_RATE) try: device.open() def bluetooth_data_callback(data): print("Data received from Bluetooth >> '%s'" % data.decode("utf-8")) device.add_bluetooth_data_received_callback(bluetooth_data_callback) print("Waiting for data from the Bluetooth interface...\n") input() finally: if device is not None and device.is_open(): device.close()