Split connect for sessions

This commit is contained in:
David Wright 2022-09-26 21:23:17 -06:00
parent ae22223f46
commit ea6c4f5e1f

View File

@ -1,26 +1,38 @@
import asyncio 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, address): def __init__(self, profile):
print("initialing") print("Initializing")
self.address = address self.profile = profile
async def connect(self): async def connect(self):
timeout=0.0 timeout=0.0
print("Connecting") print("Connecting")
async with BleakClient(self.address) as droid: self.droid = BleakClient(self.profile)
while not droid.is_connected and timeout < 10: await self.droid.connect()
while not self.droid.is_connected and timeout < 10:
sleep (.1) sleep (.1)
timeout += .1 timeout += .1
print ("Connected!") print ("Connected!")
connectCode = bytearray.fromhex("222001") connectCode = bytearray.fromhex("222001")
await droid.write_gatt_char(0x000d, connectCode, False) await self.droid.write_gatt_char(0x000d, connectCode, False)
await droid.write_gatt_char(0x000d, connectCode, False) await self.droid.write_gatt_char(0x000d, connectCode, False)
print("Locked") print("Locked")
soundBank = bytearray.fromhex("27420f4444001f00") soundBank = bytearray.fromhex("27420f4444001f00")
await droid.write_gatt_char(0x000d, soundBank) await self.droid.write_gatt_char(0x000d, soundBank)
soundSelection = bytearray.fromhex("27420f4444001800") soundSelection = bytearray.fromhex("27420f4444001800")
await droid.write_gatt_char(0x000d, soundSelection) await self.droid.write_gatt_char(0x000d, soundSelection)
async def disconnect(self):
print ("Disconnecting")
try:
soundBank = bytearray.fromhex("27420f4444001f08")
await self.droid.write_gatt_char(0x000d, soundBank)
soundSelection = bytearray.fromhex("27420f4444001808")
await self.droid.write_gatt_char(0x000d, soundSelection)
finally:
await self.droid.disconnect()
print("Disconnected")
def findDroid(candidate, data): def findDroid(candidate, data):
if candidate.name == "DROID": if candidate.name == "DROID":
return True return True
@ -30,6 +42,8 @@ def findDroid(candidate, data):
async def main(): async def main():
myDroid = await BleakScanner.find_device_by_filter(findDroid) myDroid = await BleakScanner.find_device_by_filter(findDroid)
print (myDroid) print (myDroid)
arms = droid(myDroid) arms = Droid(myDroid)
await arms.connect() await arms.connect()
sleep (3)
await arms.disconnect()
asyncio.run(main()) asyncio.run(main())