-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbluetooth.py
More file actions
17 lines (15 loc) · 865 Bytes
/
bluetooth.py
File metadata and controls
17 lines (15 loc) · 865 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import serial
import time
print("Start")
port="/dev/tty.HC-06-DevB" #This will be different for various devices and on windows it will probably be a COM port.
bluetooth=serial.Serial(port, 9600)#Start communications with the bluetooth unit
print("Connected")
bluetooth.flushInput() #This gives the bluetooth a little kick
for i in range(5): #send 5 groups of data to the bluetooth
print("Ping")
bluetooth.write(b"BOOP "+str.encode(str(i)))#These need to be bytes not unicode, plus a number
input_data=bluetooth.readline()#This reads the incoming data. In this particular example it will be the "Hello from Blue" line
print(input_data.decode())#These are bytes coming in so a decode is needed
time.sleep(0.1) #A pause between bursts
bluetooth.close() #Otherwise the connection will remain open until a timeout which ties up the /dev/thingamabob
print("Done")