client server example

This commit is contained in:
Daniel Pollithy 2017-08-29 11:56:16 +02:00
parent bf7044e301
commit 7e2eb22e77
3 changed files with 29 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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