#SPP demo program """ This example demonstrates how to transmit data between a mobile phone and a device through SPP. To use this example: 1. Install a Bluetooth serial port APP, such as BlueSPP, on your Android phone and open the APP before running the program. 2. Modify the target device's Bluetooth name in the program by changing the value of DST_DEVICE_INFO['dev_name'] to the name of the phone you want to connect to. 3. Run the program, which will search for nearby devices until the target device is found and connect to the target device through SPP. 4. Check your phone for a Bluetooth pairing request and click "Pair" if prompted. 5. Once the pairing is successful, you can use the Bluetooth serial port interface to send data to the device. The device will respond with "I have received the data you sent" after receiving the data. 6. To end the program, click "Disconnect" in the mobile APP. """ import bt import utime import _thread from queue import Queue BT_NAME = 'QuecPython-SPP' BT_EVENT = { 'BT_START_STATUS_IND': 0, # bt/ble start 'BT_STOP_STATUS_IND': 1, # bt/ble stop 'BT_SPP_INQUIRY_IND': 6, # bt spp inquiry ind 'BT_SPP_INQUIRY_END_IND': 7, # bt spp inquiry end ind 'BT_SPP_RECV_DATA_IND': 14, # bt spp recv data ind 'BT_SPP_CONNECT_IND': 61, # bt spp connect ind 'BT_SPP_DISCONNECT_IND': 62, # bt spp disconnect ind } DST_DEVICE_INFO = { 'dev_name': 'OBDII', # The Bluetooth name of the device you want to connect to Galaxy J7 (2016) 'bt_addr': None } BT_IS_RUN = 0 msg_queue = Queue(30) def bt_callback(args): global msg_queue msg_queue.put(args) def bt_event_proc_task(): global msg_queue global BT_IS_RUN global DST_DEVICE_INFO while True: print('wait msg...') msg = msg_queue.get() # It will be blocked here when there is no message. event_id = msg[0] status = msg[1] if event_id == BT_EVENT['BT_START_STATUS_IND']: print('event: BT_START_STATUS_IND') if status == 0: print('BT start successfully.') BT_IS_RUN = 1 print('Set BT name to {}'.format(BT_NAME)) retval = bt.setLocalName(0, BT_NAME) if retval != -1: print('BT name set successfully.') else: print('BT name set failed.') bt.stop() continue retval = bt.setVisibleMode(3) if retval == 0: mode = bt.getVisibleMode() if mode == 3: print('BT visible mode set successfully.') else: print('BT visible mode set failed.') bt.stop() continue else: print('BT visible mode set failed.') bt.stop() continue retval = bt.startInquiry(15) if retval != 0: print('Inquiry error.') bt.stop() continue else: print('BT start failed.') bt.stop() continue elif event_id == BT_EVENT['BT_STOP_STATUS_IND']: print('event: BT_STOP_STATUS_IND') if status == 0: BT_IS_RUN = 0 print('BT stop successfully.') else: print('BT stop failed.') retval = bt.sppRelease() if retval == 0: print('SPP release successfully.') else: print('SPP release failed.') retval = bt.release() if retval == 0: print('BT release successfully.') else: print('BT release failed.') break elif event_id == BT_EVENT['BT_SPP_INQUIRY_IND']: print('event: BT_SPP_INQUIRY_IND') if status == 0: rssi = msg[2] name = msg[4] addr = msg[5] mac = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]) print('name: {}, addr: {}, rssi: {}'.format(name, mac, rssi)) if name == DST_DEVICE_INFO['dev_name']: print('The target device is found, device name {}'.format(name)) DST_DEVICE_INFO['bt_addr'] = addr retval = bt.cancelInquiry() if retval != 0: print('cancel inquiry failed.') continue else: print('BT inquiry failed.') bt.stop() continue elif event_id == BT_EVENT['BT_SPP_INQUIRY_END_IND']: print('event: BT_SPP_INQUIRY_END_IND') if status == 0: print('BT inquiry has ended.') inquiry_sta = msg[2] if inquiry_sta == 0: if DST_DEVICE_INFO['bt_addr'] is not None: print('Ready to connect to the target device : {}'.format(DST_DEVICE_INFO['dev_name'])) retval = bt.sppConnect(DST_DEVICE_INFO['bt_addr']) if retval != 0: print('SPP connect failed.') bt.stop() continue else: print('Not found device [{}], continue to inquiry.'.format(DST_DEVICE_INFO['dev_name'])) bt.cancelInquiry() bt.startInquiry(15) else: print('Inquiry end failed.') bt.stop() continue elif event_id == BT_EVENT['BT_SPP_RECV_DATA_IND']: print('event: BT_SPP_RECV_DATA_IND') if status == 0: datalen = msg[2] data = msg[3] print('recv {} bytes data: {}'.format(datalen, data)) send_data = 'I have received the data you sent.' print('send data: {}'.format(send_data)) retval = bt.sppSend(send_data) if retval != 0: print('send data faied.') else: print('Recv data failed.') bt.stop() continue elif event_id == BT_EVENT['BT_SPP_CONNECT_IND']: print('event: BT_SPP_CONNECT_IND') if status == 0: conn_sta = msg[2] addr = msg[3] mac = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]) print('SPP connect successful, conn_sta = {}, addr {}'.format(conn_sta, mac)) bt.sppSend('ATZ\r') else: print('Connect failed.') bt.stop() continue elif event_id == BT_EVENT['BT_SPP_DISCONNECT_IND']: print('event: BT_SPP_DISCONNECT_IND') conn_sta = msg[2] addr = msg[3] mac = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]) print('SPP disconnect successful, conn_sta = {}, addr {}'.format(conn_sta, mac)) bt.stop() continue def main(): global BT_IS_RUN _thread.start_new_thread(bt_event_proc_task, ()) retval = bt.init(bt_callback) if retval == 0: print('BT init successful.') else: print('BT init failed.') return -1 retval = bt.sppInit() if retval == 0: print('SPP init successful.') else: print('SPP init failed.') return -1 retval = bt.start() if retval == 0: print('BT start successful.') else: print('BT start failed.') retval = bt.sppRelease() if retval == 0: print('SPP release successful.') else: print('SPP release failed.') return -1 count = 0 while True: utime.sleep(1) count += 1 cur_time = utime.localtime() timestamp = "{:02d}:{:02d}:{:02d}".format(cur_time[3], cur_time[4], cur_time[5]) if count % 5 == 0: if BT_IS_RUN == 1: print('[{}] BT SPP is running, count = {}......'.format(timestamp, count)) print('') else: print('BT SPP has stopped running, ready to exit.') break if __name__ == '__main__': main()