#!/usr/bin/env python3 # Before running this, you should already be paired # to smartphone using bluetoothctl # Bluetooth pairs as slave to smartphone (my phone won't pair if Pi is master) # Connect using BlueTerm app after the code starts! # Doesn't seem to work with most other Bluetooth apps. # (c) 2018 Bradley Knockel # http://pages.iu.edu/~rwisman/c490/html/pythonandbluetooth.htm import bluetooth socket = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) port = 1 socket.bind(("",port)) socket.listen(1) print("Connect to Pi") connection,address = socket.accept() print("Accepted connection from",address[0]) print('Send "q" to Pi to quit') connection.send("\r\nSend \"q\" to Pi to quit \r\n") # put the code in in a try block so that you can clean up try: while 1: data = connection.recv(1024).decode() print("Received: %s" % data) connection.send(" OK \r\n") if (data == "q"): print("Quit") break except: pass # clean up connection.close() socket.close()