Last active
May 27, 2021 15:02
-
-
Save Aenigma/2ea6bad30a25a2271ed2c05331f77638 to your computer and use it in GitHub Desktop.
Rfcomm Example
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
[Unit] | |
Description=BlueTooth Relay Service | |
Wants=bluetooth.service | |
After=bluetooth.service | |
[Service] | |
ExecStart=/home/pi/projects/bluetooth/rfcomm-server.py | |
Restart=on-failure | |
[Install] | |
WantedBy=multi-user.target |
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
import bluetooth | |
print("performing inquiry...") | |
nearby_devices = bluetooth.discover_devices(duration=8, lookup_names=True, flush_cache=True, lookup_class=False) | |
print("found %d devices" % len(nearby_devices)) | |
for addr, name in nearby_devices: | |
try: | |
print(" %s - %s" % (addr, name)) | |
except UnicodeEncodeError: | |
print(" %s - %s" % (addr, name.encode('utf-8', 'replace'))) |
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
# file: rfcomm-client.py | |
# auth: Albert Huang <albert@csail.mit.edu> | |
# desc: simple demonstration of a client application that uses RFCOMM sockets | |
# intended for use with rfcomm-server | |
# | |
# $Id: rfcomm-client.py 424 2006-08-24 03:35:54Z albert $ | |
from bluetooth import * | |
import sys | |
if sys.version < '3': | |
input = raw_input | |
addr = None | |
if len(sys.argv) < 2: | |
print("no device specified. Searching all nearby bluetooth devices for") | |
print("the SampleServer service") | |
else: | |
addr = sys.argv[1] | |
print("Searching for SampleServer on %s" % addr) | |
# search for the SampleServer service | |
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" | |
service_matches = find_service( uuid = uuid, address = addr ) | |
if len(service_matches) == 0: | |
print("couldn't find the SampleServer service =(") | |
sys.exit(0) | |
first_match = service_matches[0] | |
port = first_match["port"] | |
name = first_match["name"] | |
host = first_match["host"] | |
print("connecting to \"%s\" on %s" % (name, host)) | |
# Create the client socket | |
sock=BluetoothSocket( RFCOMM ) | |
sock.connect((host, port)) | |
print("connected. type stuff") | |
while True: | |
data = input() | |
if len(data) == 0: break | |
sock.send(data) | |
sock.close() |
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
#!/usr/bin/python3 | |
# file: rfcomm-server.py | |
# auth: Albert Huang <albert@csail.mit.edu> | |
# desc: simple demonstration of a server application that uses RFCOMM sockets | |
# | |
# $Id: rfcomm-server.py 518 2007-08-10 07:20:07Z albert $ | |
from threading import Thread | |
from bluetooth import * | |
import RPi.GPIO as GPIO | |
OUT_PIN = 4 | |
def handle(client_sock): | |
print("Accepted connection from ", client_info) | |
try: | |
while True: | |
data = client_sock.recv(1024).decode("utf-8") | |
if len(data) == 0: break | |
print("received [%s]" % data) | |
if data == "on": | |
print("Turning %s on" % OUT_PIN) | |
GPIO.output(OUT_PIN, GPIO.HIGH) | |
elif data == "off": | |
print("Turning %s off" % OUT_PIN) | |
GPIO.output(OUT_PIN, GPIO.LOW) | |
client_sock.send(data) | |
except IOError as err: | |
print(err) | |
print("disconnected") | |
client_sock.close() | |
server_sock=BluetoothSocket( RFCOMM ) | |
server_sock.bind(("",PORT_ANY)) | |
server_sock.listen(1) | |
port = server_sock.getsockname()[1] | |
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" | |
advertise_service( server_sock, "SampleServer", | |
service_id = uuid, | |
service_classes = [ uuid, SERIAL_PORT_CLASS ], | |
profiles = [ SERIAL_PORT_PROFILE ], | |
# protocols = [ OBEX_UUID ] | |
) | |
print("Waiting for connection on RFCOMM channel %d" % port) | |
try: | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(OUT_PIN, GPIO.OUT) | |
while True: | |
client_sock, client_info = server_sock.accept() | |
handle(client_sock) | |
except KeyboardInterrupt: | |
pass | |
finally: | |
server_sock.close() | |
GPIO.cleanup() | |
print("all done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment