Forked from keithweaver/bluetooth-raspberry-pi-communication.py
Created
February 4, 2019 12:23
-
-
Save fpersson/4a367fcf991d079ca18fdba97e53c7d0 to your computer and use it in GitHub Desktop.
Sending information with bluetooth on Raspberry Pi (Python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment