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
7e2eb22e77
commit
e0f2ced265
6
bluetooth_visibility.sh
Normal file
6
bluetooth_visibility.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
sudo hciconfig hci0 piscan
|
||||||
|
|
||||||
|
# sudo hciconfig -a
|
||||||
|
# should contain PICSCAN flag
|
||||||
25
client.py
25
client.py
@ -10,7 +10,7 @@ print('This devices bluetooth address is: {}'.format(bt_mac))
|
|||||||
|
|
||||||
def send_payload(address):
|
def send_payload(address):
|
||||||
payload = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
|
payload = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
|
||||||
sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
|
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
|
||||||
|
|
||||||
bt_addr = address
|
bt_addr = address
|
||||||
port = 0x1001
|
port = 0x1001
|
||||||
@ -26,17 +26,18 @@ def send_payload(address):
|
|||||||
data = sock.recv(1024)
|
data = sock.recv(1024)
|
||||||
print("Data received:", str(data))
|
print("Data received:", str(data))
|
||||||
|
|
||||||
|
a = input("exit: ")
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
|
|
||||||
while True:
|
# while True:
|
||||||
nearby_devices = bluetooth.discover_devices(lookup_names=True)
|
# nearby_devices = bluetooth.discover_devices(lookup_names=True)
|
||||||
print("found %d devices" % len(nearby_devices))
|
# print("found %d devices" % len(nearby_devices))
|
||||||
|
#
|
||||||
for addr, name in nearby_devices:
|
# for addr, name in nearby_devices:
|
||||||
print(" %s - %s" % (addr, name))
|
# print(" %s - %s" % (addr, name))
|
||||||
if addr == settings.PEER_BT_ADDRESS:
|
# if addr == settings.PEER_BT_ADDRESS:
|
||||||
print("PEERING PARTNER FOUND")
|
# print("PEERING PARTNER FOUND")
|
||||||
send_payload(addr)
|
# send_payload(addr)
|
||||||
|
#
|
||||||
time.sleep(settings.BT_SLEEP)
|
# time.sleep(settings.BT_SLEEP)
|
||||||
27
server.py
27
server.py
@ -5,23 +5,30 @@ import settings
|
|||||||
bt_mac = settings.get_own_bt_address()
|
bt_mac = settings.get_own_bt_address()
|
||||||
print('This devices bluetooth address is: {}'.format(bt_mac))
|
print('This devices bluetooth address is: {}'.format(bt_mac))
|
||||||
|
|
||||||
server_sock = bluetooth.BluetoothSocket(bluetooth.L2CAP)
|
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
|
||||||
|
|
||||||
port = 0x1001
|
port = 0x1001
|
||||||
|
|
||||||
server_sock.bind(("", port))
|
server_sock.bind(("", port))
|
||||||
server_sock.listen(1)
|
server_sock.listen(1)
|
||||||
|
|
||||||
client_sock, address = server_sock.accept()
|
client_sock = False
|
||||||
print("Accepted connection from ", address)
|
|
||||||
|
|
||||||
data = client_sock.recv(1024)
|
try:
|
||||||
print("Data received: ", str(data))
|
client_sock, address = server_sock.accept()
|
||||||
|
print("Accepted connection from ", address)
|
||||||
|
|
||||||
while data:
|
|
||||||
client_sock.send('Echo => ' + str(data))
|
|
||||||
data = client_sock.recv(1024)
|
data = client_sock.recv(1024)
|
||||||
print("Data received:", str(data))
|
print("Data received: ", str(data))
|
||||||
|
|
||||||
|
while data:
|
||||||
|
client_sock.send('Echo => ' + str(data))
|
||||||
|
data = client_sock.recv(1024)
|
||||||
|
print("Data received:", str(data))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("closing")
|
||||||
|
finally:
|
||||||
|
if client_sock:
|
||||||
|
client_sock.close()
|
||||||
|
server_sock.close()
|
||||||
|
|
||||||
client_sock.close()
|
|
||||||
server_sock.close()
|
|
||||||
10
settings.py
10
settings.py
@ -1,7 +1,9 @@
|
|||||||
# this is the address of the hikey
|
# this is the address of the hikey
|
||||||
# PEER_BT_ADDRESS = '98:7B:F3:19:FE:57'
|
HIKEY_BT_ADDRESS = '98:7B:F3:19:FE:57'
|
||||||
PEER_BT_ADDRESS = '7C:E9:D3:DD:66:23'
|
# raspy
|
||||||
BT_SLEEP = 2 # seconds
|
RASPY_BT_ADDRESS = 'B8:27:EB:76:09:0B'
|
||||||
|
PEER_BT_ADDRESS = RASPY_BT_ADDRESS
|
||||||
|
BT_SLEEP = 0.01 # seconds
|
||||||
|
|
||||||
from subprocess import Popen, PIPE
|
from subprocess import Popen, PIPE
|
||||||
import re
|
import re
|
||||||
@ -25,3 +27,5 @@ def get_own_bt_address():
|
|||||||
|
|
||||||
return bt_addresses[0]
|
return bt_addresses[0]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user