mirror of
https://github.com/DanielPollithy/bluetooth_drone.git
synced 2025-10-16 11:45:38 +00:00
client server example
This commit is contained in:
parent
bf7044e301
commit
7e2eb22e77
23
client.py
23
client.py
@ -1,5 +1,6 @@
|
||||
import bluetooth
|
||||
import time
|
||||
import json
|
||||
|
||||
import settings
|
||||
|
||||
@ -7,6 +8,27 @@ bt_mac = settings.get_own_bt_address()
|
||||
print('This devices bluetooth address is: {}'.format(bt_mac))
|
||||
|
||||
|
||||
def send_payload(address):
|
||||
payload = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
|
||||
sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
|
||||
|
||||
bt_addr = address
|
||||
port = 0x1001
|
||||
|
||||
print("trying to connect to %s on PSM 0x%X" % (bt_addr, port))
|
||||
|
||||
sock.connect((bt_addr, port))
|
||||
|
||||
print("connected. type stuff")
|
||||
|
||||
data = payload
|
||||
sock.send(data)
|
||||
data = sock.recv(1024)
|
||||
print("Data received:", str(data))
|
||||
|
||||
sock.close()
|
||||
|
||||
|
||||
while True:
|
||||
nearby_devices = bluetooth.discover_devices(lookup_names=True)
|
||||
print("found %d devices" % len(nearby_devices))
|
||||
@ -15,5 +37,6 @@ while True:
|
||||
print(" %s - %s" % (addr, name))
|
||||
if addr == settings.PEER_BT_ADDRESS:
|
||||
print("PEERING PARTNER FOUND")
|
||||
send_payload(addr)
|
||||
|
||||
time.sleep(settings.BT_SLEEP)
|
||||
@ -1,5 +1,10 @@
|
||||
import bluetooth
|
||||
|
||||
import settings
|
||||
|
||||
bt_mac = settings.get_own_bt_address()
|
||||
print('This devices bluetooth address is: {}'.format(bt_mac))
|
||||
|
||||
server_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
|
||||
|
||||
port = 0x1001
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
# this is the address of the hikey
|
||||
# PEER_BT_ADDRESS = '98:7B:F3:19:FE:57'
|
||||
PEER_BT_ADDRESS = '7C:E9:D3:DD:66:23'
|
||||
BT_SLEEP = 2 # seconds
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
|
||||
Loading…
Reference in New Issue
Block a user