๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ | Python/ํŒŒ์ด์ฌ ์ดˆ๊ฐ„๋‹จ ํ”„๋กœ์ ํŠธ

[Python] BLE ํ†ต์‹  ํ•˜๊ธฐ with bleak (MacOS) - 1) ํƒ์ƒ‰, ์—ฐ๊ฒฐ

by KASSID 2023. 5. 24.

๋ชฉ์ฐจ

    728x90

    ํŒŒ์ด์ฌ์˜ ๋ชจ๋“ˆ ์ค‘ bleak์„ ์ด์šฉํ•˜์—ฌ Mac์—์„œ BLE ํ†ต์‹ ์„ ํ•ด๋ณด์ž!

     

    1. BLE๋ž€ ๋ฌด์—‡์ธ๊ฐ€

    BLE๋Š” Bluetooth Low Energy์˜ ์•ฝ์ž๋กœ ๋ธ”๋ฃจํˆฌ์Šค์™€ ๊ฐ™์ง€๋งŒ ๋” ๋น ๋ฅด๊ณ  ๊ฐ€๋ณ๊ฒŒ ํ†ต์‹ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

    ๋•Œ๋ฌธ์— ์Šค๋งˆํŠธ ์›Œ์น˜, ํƒœ๊ทธ, ์ž‘์€ ์žฅ์น˜๋“ค์— ์ฃผ๋กœ ์ด์šฉํ•œ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์‹ฌ์ „๋„, ๋ฉ”์‹œ์ง€ ํ†ต์‹  ๋“ฑ ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    2. bleak ๋ชจ๋“ˆ ์ด์šฉํ•˜๊ธฐ

    bleak๋Š” ํŒŒ์ด์ฌ์—์„œ ๋ธ”๋ฃจํˆฌ์Šค LE๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋“ˆ์ด๋‹ค. ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅด ํ†ตํ•ด์„œ MacOS, Windows, Linux ๋“ฑ์—์„œ๋„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์žฅ์น˜์— ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ์•„๋ž˜์—์„œ๋Š” bleak์„ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋“ค์ด๋‹ค.

    --- ์‹ค์Šตํ™˜๊ฒฝ
    python 3.9.6
    bleak 0.20.2

     

    ์ˆ˜ํ–‰ ๋‹จ๊ณ„ ์š”์•ฝ

    1. Bluetooth ์žฅ๋น„ On

    2. bleak ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ

    3. ์ฃผ๋ณ€์—์„œ BLE์žฅ์น˜ ๊ฒ€์ƒ‰ ๋ฐ ๋ชฉ๋ก ๋‚˜ํƒ€๋‚ด๊ธฐ

    4. ์›ํ•˜๋Š” ๊ธฐ๊ธฐ์˜ ์ฃผ์†Œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅ์น˜์— ์—ฐ๊ฒฐ

     

     

    1) ๋ชจ๋“ˆ import

    import bleak

    1. pip install bleak์„ ํ†ตํ•ด ๋ชจ๋“ˆ ์„ค์น˜ (๊ฐ€์ƒํ™˜๊ฒฝ ์„ธํŒ… ๋ฐ pip ์„ค์น˜ ๋“ฑ์€ ์„œ์ˆ ์—์„œ ์ƒ๋žต)

    2. import bleak์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

     

     

    2) ์žฅ์น˜ ๊ฒ€์ƒ‰ํ•˜๊ธฐ

    import asyncio # ๋น„๋™๊ธฐ ๋ชจ๋“ˆ๊ฐ€์ ธ์˜ค๊ธฐ
    from bleak import BleakScanner # bleak์˜ Scanner๊ฐ€์ ธ์˜ค๊ธฐ
    
    async  def  main (): # ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ์ •์˜
        devices = await BleakScanner.discover() # ๋น„๋™๊ธฐ ์ˆ˜ํ–‰์ž„์„ ์•Œ๋ฆผ
        for device in devices: 
            print (device) 
    
    asyncio.run(main()) # main()ํ•จ์ˆ˜๊ฐ€ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ˆ˜ํ–‰๋  ๊ฒƒ์ž„์„ ์•Œ๋ฆผ(ํ•„์ˆ˜)

    - Asyncio ๋ž€?

    ๋น„๋™๊ธฐ ์‹คํ–‰์„ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

    ๋ธ”๋ฃจํˆฌ์Šค ๊ธฐ๊ธฐ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์€ ์‘๋‹ต ์‹œ๊ฐ„์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์—†๊ณ  ๋™์•ˆ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ผ์‹œ์ •์ง€ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—

    Asyncio ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์žฅ์น˜ ๊ฒ€์ƒ‰ ์ˆ˜ํ–‰๊ณผ ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•ด์•ผํ•œ๋‹ค.

     

     

    discover()๋Š” ํ˜„์žฌ ์ฃผ๋ณ€์˜ BLE ์žฅ์น˜๋“ค์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

     

    ์ˆ˜ํ–‰๊ฒฐ๊ณผ

    ๊ฐ ๋””๋ฐ”์ด์Šค์— ๋‹ด๊ธด ๋ณ€์ˆ˜๋“ค์€

    .name .address .details๊ฐ€ ์žˆ๋‹ค.

     

    ์˜ค๋Š˜ ํ™œ์šฉํ•  ํ‚คํŠธ๋Š”

    F0B8A2B0-CD98-DAC7-FD9B-E2FCC1BF4200 ์ด๋ผ๋Š” UUID ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

     

    ์—ฌ๊ธฐ์„œ MAC์ฃผ์†Œ๊ฐ€ ์•„๋‹Œ UUID ์ฃผ์†Œ๋งŒ ๋ฐ˜ํ™˜ํ•œ ์ด์œ ๋Š” MacOS์˜ ๋ณด์•ˆ์ƒ์˜ ์ด์œ ์ด๋‹ค.

    ๋ฌผ๋ก  ์ด๋ฏธ MAC์ฃผ์†Œ๋ฅผ ์•Œ๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,

    ํƒ์ƒ‰์„ ํ•˜์—ฌ ๋ฐ˜ํ™˜๋œ ๊ฒƒ์„ ์ด์šฉํ•  ๊ฒฝ์šฐ UUID๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

     

     

    3) ์žฅ์น˜์— ์—ฐ๊ฒฐํ•˜๊ธฐ

    BLE์žฅ์น˜์— ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” MAC ํ˜น์€ UUID ์ฃผ์†Œ๋ฅผ ์ด์šฉํ•˜์—ฌ BleakClient์— ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

    import asyncio 
    from bleak import BleakClient, BleakScanner 
    
    async  def  main (): 
        address = "F0B8A2B0-CD98-DAC7-FD9B-E2FCC1BF4200"  # Kit UUID 
    
        async  with BleakClient(address) as client: 
            print (client.is_connected) # True ๋˜๋Š” False ์ถœ๋ ฅ
    
    asyncio.run(main())

     

    ์ˆ˜ํ–‰๊ฒฐ๊ณผ

     

    BleckClient()์— ์ฃผ์†Œ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     

    ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์œ„์˜ ํƒ์ƒ‰๊ณผ ์žฅ์น˜ ์—ฐ๊ฒฐ์„ ๋ณ‘ํ•ฉ ๋ฐ ์‘์šฉํ•œ ๊ฒƒ์ด๋‹ค.

    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!!')
            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!')

     

    ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ

     

     

     

    ๋Œ“๊ธ€