Connection working sample

This commit is contained in:
David Wright 2022-09-26 17:25:13 -06:00
parent cce4e30c60
commit ae22223f46

View File

@ -2,15 +2,34 @@ import asyncio
from time import sleep from time import sleep
from bleak import BleakScanner, BleakClient from bleak import BleakScanner, BleakClient
class droid(): class droid():
def __init__(self, ipAdress): def __init__(self, address):
print("initizing") print("initialing")
self.ipAdress = ipAdress self.address = address
self.connection = asyncio.run(self.connect())
async def connect(self): async def connect(self):
print("connecting") timeout=0.0
async with BleakClient(self.ipAdress) as droid: print("Connecting")
await droid.write_gatt_char(0x000d, 222001) async with BleakClient(self.address) as droid:
await droid.write_gatt_char(0x000d, 222001) while not droid.is_connected and timeout < 10:
sleep (.1)
timeout += .1
print ("Connected!")
connectCode = bytearray.fromhex("222001")
await droid.write_gatt_char(0x000d, connectCode, False)
await droid.write_gatt_char(0x000d, connectCode, False)
print("Locked")
soundBank = bytearray.fromhex("27420f4444001f00")
await droid.write_gatt_char(0x000d, soundBank)
soundSelection = bytearray.fromhex("27420f4444001800")
await droid.write_gatt_char(0x000d, soundSelection)
def findDroid(candidate, data):
if candidate.name == "DROID":
return True
else:
return False
async def main():
arms = droid("D4:D7:12:C5:93:C1") myDroid = await BleakScanner.find_device_by_filter(findDroid)
print (myDroid)
arms = droid(myDroid)
await arms.connect()
asyncio.run(main())