Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Last active April 7, 2023 13:49
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save keithweaver/3d5dbf38074cee4250c7d9807510c7c3 to your computer and use it in GitHub Desktop.
Save keithweaver/3d5dbf38074cee4250c7d9807510c7c3 to your computer and use it in GitHub Desktop.
Sending information with bluetooth on Raspberry Pi (Python)
# Uses Bluez for Linux
#
# sudo apt-get install bluez python-bluez
#
# Taken from: https://people.csail.mit.edu/albert/bluez-intro/x232.html
# Taken from: https://people.csail.mit.edu/albert/bluez-intro/c212.html
import bluetooth
def receiveMessages():
server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
port = 1
server_sock.bind(("",port))
server_sock.listen(1)
client_sock,address = server_sock.accept()
print "Accepted connection from " + str(address)
data = client_sock.recv(1024)
print "received [%s]" % data
client_sock.close()
server_sock.close()
def sendMessageTo(targetBluetoothMacAddress):
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((targetBluetoothMacAddress, port))
sock.send("hello!!")
sock.close()
def lookUpNearbyBluetoothDevices():
nearby_devices = bluetooth.discover_devices()
for bdaddr in nearby_devices:
print str(bluetooth.lookup_name( bdaddr )) + " [" + str(bdaddr) + "]"
lookUpNearbyBluetoothDevices()
@OldGrey80
Copy link

When I run this code on my Pi 3B+ I get an empty list [] from bluetooth.discover_devices(). Running the graphic interface or sudo bluetoothctl from the command line I see a whole bunch of devices, including my SiliconLabs BGX13P and my Sensirion Humidity and temperature sensor that I am trying to use as guinea pigs. Any suggestions?

@Amitjipathak
Copy link

Amitjipathak commented Nov 5, 2019

print str(bluetooth.lookup_name( bdaddr )) + " [" + str(bdaddr) + "]"
python3.5 shows invalid syntax in this particular line.... with providind bt address and without providing address the error is same

error will look like:
Traceback (most recent call last):
File "/home/pi/bluepy/bluepy/PitoBlutooth1.py", line 11
print "Accepted connection from " + str(address)
^
SyntaxError: invalid syntax

@hmimo285
Copy link

hmimo285 commented Nov 5, 2019

On python3, need to change the print function
print( "Accepted connection from " + str(address) )

@Amitjipathak
Copy link

On python3, need to change the print function
print( "Accepted connection from " + str(address) )

yes now no errors shows but still shell window is blank no result is showing

@jishincreo
Copy link

Does anyone know how we can setup multiple channels for a socket over bluetooth?

@NotExactlyMeerkat
Copy link

When I run this code on my Pi 3B+ I get an empty list [] from bluetooth.discover_devices(). Running the graphic interface or sudo bluetoothctl from the command line I see a whole bunch of devices, including my SiliconLabs BGX13P and my Sensirion Humidity and temperature sensor that I am trying to use as guinea pigs. Any suggestions?

What guinea pigs? Are you torturing some pigs?

@herbie-c
Copy link

I got an issue with this program:
Detecting bluetooth devices works just fine, I can detect my phone and my other bluetooth devices, but when sending messages to my phone a "connection refused" error raises (but that's probably because it's got some built-in protection against scary Linux devices). So that's when I thought 'hey, what if I try it using my pi zero w?'

so I used two pi's for my tryout of this program:

  • a headless pi zero w automatically loads the script (using crontab), detects bluetooth devices using lookUpNearbyBluetoothDevices() and sends a message to all of them (by using sendMessageTo in a try function)
  • a pi 4 B connected to a monitor is running the function receiveMessages(), but doesn't receive anything at all

And that's the problem! They're both pi's , and I suppose sending messages between two pi's using this program would work.
Can anyone help me out please?

PS: I like your program, even when only a small part of it works that's a lot better than most internet pieces of code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment