Skip to content

Instantly share code, notes, and snippets.

@kingychiu
Created June 20, 2017 17:29
Show Gist options
  • Save kingychiu/91c302e7688b0665fe0e0addaa0f774e to your computer and use it in GitHub Desktop.
Save kingychiu/91c302e7688b0665fe0e0addaa0f774e to your computer and use it in GitHub Desktop.
Communication between Raspberry Pi and multiple Arduinos via Bluetooth Low Power (BLE)
from bluepy.btle import Scanner, DefaultDelegate, Peripheral
import threading
class NotificationDelegate(DefaultDelegate):
def __init__(self, number):
DefaultDelegate.__init__(self)
self.number = number
def handleNotification(self, cHandle, data):
print 'Notification:\nConnection:'+str(self.number)+'\nHandler:'+str(cHandle)+'\nMsg:'+data
bt_addrs = ['00:15:83:00:45:98', '00:15:83:00:86:72']
connections = []
connection_threads = []
scanner = Scanner(0)
class ConnectionHandlerThread (threading.Thread):
def __init__(self, connection_index):
threading.Thread.__init__(self)
self.connection_index = connection_index
def run(self):
connection = connections[self.connection_index]
connection.setDelegate(NotificationDelegate(self.connection_index))
while True:
if connection.waitForNotifications(1):
connection.writeCharacteristic(37, 'Thank you for the notification!')
while True:
print'Connected: '+str(len(connection_threads))
print 'Scanning...'
devices = scanner.scan(2)
for d in devices:
print(d.addr)
if d.addr in bt_addrs:
p = Peripheral(d)
connections.append(p)
t = ConnectionHandlerThread(len(connections)-1)
t.start()
connection_threads.append(t)
@42jerrykim
Copy link

Is there a way to send messages to each client via Bluetooth?

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