Skip to content

Commit

Permalink
First commit BLE
Browse files Browse the repository at this point in the history
  • Loading branch information
felipengeletrica committed Apr 12, 2024
1 parent ed7a58e commit 38ed9aa
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@ Example two serial ports:
"port": 1,
"samplingSeconds": 1,
"description": "AGP",
"interface": "bluetooth"
"interface": "bluetooth-gps"
},
{
"address": "E8:31:CD:5C:AC:B2",
"port": 1,
"samplingSeconds": 1,
"description": "DBG",
"interface": "bluetooth"
"interface": "bluetooth-gps"
},
{
"address": "E8:31:CD:5C:AC:B2",
"port": 1,
"samplingSeconds": 1,
"description": "Logs from BLE",
"interface": "bluetooth-BLE"
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions examples/esp32-BLE/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ void setup() {
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();

Serial.println("START BLE SAMPLE");
}

void loop() {
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ setuptools==57.5.0
PyBluez==0.22
RPi.GPIO==0.7.1
keyboard==0.13.5
bluepy
pyserial
35 changes: 35 additions & 0 deletions src/BLEConnector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import threading
from bluepy.btle import Scanner, DefaultDelegate

class BLEConnector(threading.Thread):
def __init__(self, device_name):
self.device_name = device_name
self.service_uuid = None
self.characteristic_uuid = None
def run(self, service_uuid, characteristic_uuid):
self.service_uuid = service_uuid
self.characteristic_uuid = characteristic_uuid
self._process = p = threading.Thread(target=self._process)
p.daemon = True
p.start()

def _process(self):

while True:
try:
device = self.scan_devices()
if not device:
continue
try:
device.connect()
services = device.getServices()
for service in services:
if service.uuid == self.service_uuid:
characteristics = service.getCharacteristics()
for char in characteristics:
if char.uuid == self.characteristic_uuid:
data = char.read()
print(data)
continue
except Exception as e:
return None, str(e)
16 changes: 15 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from SerialLogger import logger
from BluetoothGpsAgrinavi import BluetoothGpsAgrinavi
from BLEConnector import BLEConnector

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
script_path = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -58,7 +59,7 @@ def init_data_instances(datajson):
timeout=devices[index]['timeout'])

# Bluetooth interface
elif "bluetooth" in devices[index]['interface']:
elif "bluetooth-gps" in devices[index]['interface']:
devs.append(
BluetoothGpsAgrinavi(
description=devices[index]['description'],
Expand All @@ -67,6 +68,19 @@ def init_data_instances(datajson):
port=devices[index]['port'],
address=devices[index]['address']
)

# Bluetooth BLE interface
elif "bluetooth-BLE" in devices[index]['interface']:

devs.append(
BLEConnector(
device_name=devices[index]['description']
)
)
devs[index].run(
service_uuid = devices[index]['service_uuid'],
characteristic_uuid = devices[index]['characteristic_uuid'],
)
else:
Exception("Invalid device")
a = 1
Expand Down

0 comments on commit 38ed9aa

Please sign in to comment.