Adding in some example bluetooth files
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import gatt
|
||||
|
||||
class AnyDeviceManager(gatt.DeviceManager):
|
||||
def device_discovered(self, device):
|
||||
print("Discovered [%s] %s" % (device.mac_address, device.alias()))
|
||||
|
||||
manager = AnyDeviceManager(adapter_name='hci0')
|
||||
manager.start_discovery()
|
||||
manager.run()
|
||||
@@ -1,7 +0,0 @@
|
||||
import pygatt
|
||||
|
||||
adapter = pygatt.BGAPIBackend()
|
||||
|
||||
adapter.start()
|
||||
device = adapter.connect('E3:AE:CD:AF:28:E2')
|
||||
print(device.char_read("64a70010-f691-4b93-a6f4-0968f5b648f8"))
|
||||
@@ -0,0 +1,37 @@
|
||||
import gatt
|
||||
|
||||
manager = gatt.DeviceManager(adapter_name='hci0')
|
||||
|
||||
class KanoWand(gatt.Device):
|
||||
def services_resolved(self):
|
||||
super().services_resolved()
|
||||
print("Grabbing services")
|
||||
print(self.is_connected())
|
||||
print(self.services)
|
||||
|
||||
for service in self.services:
|
||||
print("FOUND SERVICE")
|
||||
# print(service)
|
||||
# print(service.device)
|
||||
print(service.uuid)
|
||||
print(service.characteristics)
|
||||
for char in service.characteristics:
|
||||
print("FOUND CHARACTERISTIC")
|
||||
print(char.uuid)
|
||||
print(char.read_value())
|
||||
# device_information_service = next(s for s in self.services if s.uuid == '0000180a-0000-1000-8000-00805f9b34fb')
|
||||
#
|
||||
# firmware_version_characteristic = next(
|
||||
# c for c in device_information_service.characteristics
|
||||
# if c.uuid == '00002a26-0000-1000 -8000-00805f9b34fb')
|
||||
#
|
||||
# firmware_version_characteristic.read_value()
|
||||
|
||||
def characteristic_value_updated(self, characteristic, value):
|
||||
print("Firmware version:", value.decode("utf-8"))
|
||||
|
||||
|
||||
device = KanoWand(mac_address='e3:ae:cd:af:28:e2', manager=manager)
|
||||
device.connect()
|
||||
manager.run()
|
||||
manager.stop()
|
||||
@@ -1,28 +0,0 @@
|
||||
from bluetooth.ble import BeaconService
|
||||
|
||||
class Beacon(object):
|
||||
|
||||
def __init__(self, data, address):
|
||||
self._uuid = data[0]
|
||||
self._major = data[1]
|
||||
self._minor = data[2]
|
||||
self._power = data[3]
|
||||
self._rssi = data[4]
|
||||
self._address = address
|
||||
|
||||
def __str__(self):
|
||||
ret = "Beacon: address:{ADDR} uuid:{UUID} major:{MAJOR}" \
|
||||
" minor:{MINOR} txpower:{POWER} rssi:{RSSI}" \
|
||||
.format(ADDR=self._address, UUID=self._uuid, MAJOR=self._major,
|
||||
MINOR=self._minor, POWER=self._power, RSSI=self._rssi)
|
||||
return ret
|
||||
|
||||
|
||||
service = BeaconService()
|
||||
devices = service.scan(2)
|
||||
|
||||
for address, data in list(devices.items()):
|
||||
b = Beacon(data, address)
|
||||
print(b)
|
||||
|
||||
print("Done.")
|
||||
@@ -0,0 +1,58 @@
|
||||
import asyncio
|
||||
from bleak import discover
|
||||
|
||||
from pprint import pprint
|
||||
|
||||
|
||||
async def run():
|
||||
devices = await discover()
|
||||
for d in devices:
|
||||
print(d)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(run())
|
||||
|
||||
import asyncio
|
||||
from bleak import BleakClient
|
||||
|
||||
address = "e3:ae:cd:af:28:e2"
|
||||
MODEL_NBR_UUID = "64a7000f-f691-4b93-a6f4-0968f5b648f8"
|
||||
|
||||
async def run(address, loop):
|
||||
async with BleakClient(address, loop=loop) as client:
|
||||
# await client.connect()
|
||||
print(await client.is_connected())
|
||||
print(await client.get_services())
|
||||
services = await client.get_services()
|
||||
pprint(services.descriptors)
|
||||
pprint(services.characteristics)
|
||||
pprint(services.services)
|
||||
# print(services.descriptors)
|
||||
# for key, val in services.descriptors.items():
|
||||
# print(f"{key} + {val}")
|
||||
#
|
||||
# print(services.characteristics)
|
||||
# for key, val in services.characteristics.items():
|
||||
# print(f"{key} + {val}")
|
||||
|
||||
print(services)
|
||||
for x in services:
|
||||
print(x)
|
||||
for characteristic in x.characteristics:
|
||||
print("")
|
||||
print(characteristic)
|
||||
print(characteristic.properties)
|
||||
for descriptor in characteristic.descriptors:
|
||||
print(descriptor)
|
||||
print(x.description)
|
||||
# for i in range(10):
|
||||
# x = await client.read_gatt_descriptor(i)
|
||||
# print(x)
|
||||
|
||||
model_number = await client.read_gatt_char()
|
||||
print(model_number)
|
||||
print("Model Number: {0}".format("".join(map(chr, model_number))))
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(run(address, loop))
|
||||
Reference in New Issue
Block a user