From 7e2eb22e77b291814032fc72c68f5fcc30b1bdd5 Mon Sep 17 00:00:00 2001 From: Daniel Pollithy Date: Tue, 29 Aug 2017 11:56:16 +0200 Subject: [PATCH] client server example --- client.py | 23 +++++++++++++++++++++++ server.py | 5 +++++ settings.py | 1 + 3 files changed, 29 insertions(+) diff --git a/client.py b/client.py index 3e3465a..f1d8654 100644 --- a/client.py +++ b/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) \ No newline at end of file diff --git a/server.py b/server.py index d892544..80413cb 100644 --- a/server.py +++ b/server.py @@ -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 diff --git a/settings.py b/settings.py index 577efdd..c00b946 100644 --- a/settings.py +++ b/settings.py @@ -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