[Window with BLE]Data receive

김세빈·2021년 12월 13일
0

BLE

목록 보기
1/1

0. 준비

bleak 라이브러리[python]
https://bleak.readthedocs.io/en/latest/index.html

1. 시작

컴퓨터 블루투스 버전 확인

장치 관리자 -> bluetooth -> 컴퓨터 bluetooth device ->오른쪽 클릭 후 속성 -> 고급 -> 펌웨어 버전 확인

장치 관리자 ->bluetooth -> 컴퓨터 bluetooth device
페어링된 장비도 뜨는 듯


lmp확인

버전 : 4.0 이상 확인

https://support.microsoft.com/ko-kr/windows/%EB%82%B4-pc-bluetooth-%EB%B2%84%EC%A0%84%EC%9D%80-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80%EC%9A%94-f5d4cff7-c00d-337b-a642-d2d23b082793

2. scann

scan 확인

https://bleak.readthedocs.io/en/latest/scanning.html
scann 예제

import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

asyncio.run(main())

3. ble connect

paring

  1. 직접 페어링

설정 -> 장치 -> bluetooth -> bluetooth 또는 기타 장비 추가

를 통해서 디바이스 페어링
페어링 이후에는 밑에 장치 표시됨

  1. 페어링 함수

connect

https://bleak.readthedocs.io/en/latest/usage.html
connect 예제

import asyncio
from bleak import BleakClient

# 장비 address 작성 
address = "--:--:--:--:--:--"

async def main(address):
    client = BleakClient(address)
    try:
        await client.connect() 
        print("connect to " + client.address)
    except Exception as e:
        print(e)
    finally:
        await client.disconnect()

asyncio.run(main(address))

error: connect 되었다가 안되었다가 하는 오류

해결방법 : bluetooth 동굴 이용

노트북/컴퓨터 기본 내장 bluetooth칩일시 끊기는 현상
->블루투스 동굴 이용해서 해보세요 잘 됩니다

4. 데이터 받기

notify enable 설정을 해야지 전송 데이터가 쭉 들어옴

notify and read

import asyncio
from bleak import BleakClient

address = "--:--:--:--:--:--"

#charateristic uuid
C_uuid = "00001524-1212-efde-1523-785feabcd123"

def callback(sender:int , data:bytearray):
    pass

async def main(address):
    client = BleakClient(address)
    try:
        #connect
        await client.connect() 
        print("connect to " + client.address)

        #notify
        await client.start_notify(C_uuid,callback)

        #read
        while(True):
            data = await client.read_gatt_char(C_uuid)
            print(bytes(data))

      
        
    except Exception as e:
        print(e)
        pass

    finally:
        await client.disconnect()

asyncio.run(main(address))
profile
의식의흐름 밸로그

0개의 댓글