๋ชฉ์ฐจ
์ง๋ ํฌ์คํธ์์๋ bleak ๋ชจ๋์ ์ด์ฉํด ์ฃผ๋ณ ๊ธฐ๊ธฐ๋ฅผ ํ์ํ๊ณ ์ฐ๊ฒฐํ๋ ๊ฒ์ ํด๋ณด์๋ค.
์ด๋ฒ์๋ ํ์ฌ Kit์ ์๋น์ค ์ ๋ณด๋ฅผ ์์๋ณด๊ณ ๊ทธ๋ฅผ ํ์ฉํ์ฌ kit๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์๋ณด์!
1. ์ฐ๊ฒฐํ ์ฅ์น์ ์๋น์ค
1) ์ฐ๊ฒฐ๋ ์๋น์ค ํ์ธ
import asyncio
from bleak import BleakClient, BleakScanner
async def main ():
devices = await BleakScanner.discover()
for idx, device in enumerate(devices):
print(f'{idx}\t\t',device)
select = int(input())
address = devices[select].address
async with BleakClient(address) as client:
try:
print("Connecting...", end='')
await client.connect()
print('Connected!!')
# ์๋น์ค ๋ฐ๊ธฐ
services = await client.get_services()
print("Services:", type(services))
for service in services:
print(service)
except Exception as e:
print('error: ', e, end='')
finally:
print('Start disconnect')
await client.disconnect()
print('disconnected')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Done!')
์ํ ๊ฒฐ๊ณผ ์ค ์ผ๋ถ
์ ํํ ์ฅ์น์ ์ ์ ์ฐ๊ฒฐ ํ get_services()๋ฅผ ํตํด ์๋น์ค ์ ๋ณด๋ฅผ ๋ฐํ๋ฐ๋๋ค.
2) chracteristic ์ ๋ณด ์ป๊ธฐ
import asyncio
from bleak import BleakClient, BleakScanner
async def main ():
devices = await BleakScanner.discover()
for idx, device in enumerate(devices):
print(f'{idx}\t\t',device)
select = int(input())
address = devices[select].address
async with BleakClient(address) as client:
try:
print("Connecting...", end='')
await client.connect()
print('Connected!!')
# ์๋น์ค ๋ฐ๊ธฐ
services = await client.get_services()
print("Services:", type(services))
for service in services:
print(service)
print(service.uuid)
print("Characteristic List:")
for characteristic in service.characteristics:
print("\n\t", characteristic)
print("\t", characteristic.uuid)
print("\t", characteristic.description)
print("\t", characteristic.properties)
except Exception as e:
print('error: ', e, end='')
finally:
print('Start disconnect')
await client.disconnect()
print('disconnected')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Done!')
์ํ ๊ฒฐ๊ณผ ์ค ์ผ๋ถ
serive ๋ด๋ถ์ ๋ค์ด์๋ ์ ๋ณด๋ค์ ๋ํด์ ๋ฐํํด๋ณด๋ฉด ์์ ๊ฐ๋ค.
uuid, ์ค๋ช , ์์ฑ ๋ค์ ์ ์ ์๋ค.
์ด ์ค chracteristic UUID์ ์์ฑ์ด ๋ค์ ๋จ๊ณ๋ฅผ ์ํด ํ์ํ ์ ๋ณด๋ค์ด๋ค.
์์ ์ ๋ณด๋ฅผ ํตํด ์ ์ ์๋ ๊ฒ์
6e400003-b5a3-f393-e0a9-e50e24dcca9e ์ด๋ผ๋ UUID๋ฅผ ๊ฐ์ง๊ณ ์๋ ์บ๋ฆญํฐ๋ฆฌ์คํฑ์ด
notify, write ๊ธฐ๋ฅ์ ์ํํ ์ ์๋ค ๋ผ๋ ๊ฒ์ด๋ค.
(( ์ฃผ์ ์์ฑ ))
- read : ์ฝ๊ธฐ (์ฅ์น์ ์๋ ๋ฐ์ดํฐ๋ฅผ)
- write : ์ฐ๊ธฐ (์ฅ์น์๊ฒ ๋ฐ์ดํฐ๋ฅผ) & ์๋ต ๊ธฐ๋ค๋ฆฌ๊ธฐ
- notify : ํด๋ผ์ด์ธํธ ์์ฒญ์ด ์์ด๋ ์ฅ์น์์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ & ์๋ต ๊ธฐ๋ค๋ฆฌ์ง ์์
... (indicate, write-without-response ๋ฑ)
3) ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ (notify)
ํ์์ ํ๋ก์ ํธ์์ ํ์ํ ์์ฑ์ธ notify๋ฅผ ํตํด
์ฅ์น์์ ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋๋ก ํด๋ณด์!
import asyncio
from bleak import BleakClient, BleakScanner
def notify_callback(sender: int, data: bytearray):
print('sender: ', sender, 'data: ', data)
print('\tint list: ', [b for b in data])
async def main ():
devices = await BleakScanner.discover()
for idx, device in enumerate(devices):
print(f'{idx}\t\t',device)
select = int(input())
address = devices[select].address
async with BleakClient(address) as client:
try:
print("Connecting...", end='')
await client.connect()
print('Connected!!')
# ์๋น์ค ๋ฐ๊ธฐ
services = await client.get_services()
print("Services:", type(services))
for service in services:
print(service)
for characteristic in service.characteristics:
if 'notify' in characteristic.properties:
notity_charcteristic_uuid = characteristic.uuid
print("\n\t", characteristic)
print('Activate notify...')
await client.start_notify(characteristic, notify_callback)
except Exception as e:
print('error: ', e, end='')
if client.is_connected:
await asyncio.sleep(2.0) #20์ด ๋์ ์คํธ๋ฆผ
print('try to deactivate notify.')
await client.stop_notify(notity_charcteristic_uuid)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('Done!')
์ํ ๊ฒฐ๊ณผ ์ค ์ผ๋ถ
์์ ๊ฐ์ด ์ฅ์น๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ฌ ์ ์๋ค.
์ถํ์ ๋ค๋ฅธ ์์ฑ๋ค์ ๋ค๋ฅด๋ ๊ธ๋ ํฌ์คํ ํด๋ณผ ์์ ์ด๋ค!
'๐ | Python > ํ์ด์ฌ ์ด๊ฐ๋จ ํ๋ก์ ํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] BLE ํต์ ํ๊ธฐ with bleak (MacOS) - 1) ํ์, ์ฐ๊ฒฐ (0) | 2023.05.24 |
---|---|
Python_์ฌ์๋ฐค๋ฐ๋ค '์' ํ์๋ฅผ ์ธ์ด๋ณด์ (0) | 2022.01.17 |
์น ํฌ๋กค๋ง _ ๋ฆฌ๊ทธ์ค๋ธ๋ ์ ๋ ์ ์ ๊ฒ์๊ธฐ (0) | 2021.12.27 |
์ฃผ์ฌ์ ๋ฒ ํ ๊ฒ์ (0) | 2021.11.03 |
์ซ์์ผ๊ตฌ (0) | 2021.11.03 |
๋๊ธ