본문 바로가기

아두이노우노 R4

Nano 33 IoT BLE Scanner 코드

반응형

 

 

BLE Scanner 구현

 

아래 예제 코드는 Central device (Client)가 자신에게 정보를 줄 server를 찾기위해 scan하는 예제다.

 

  • setup()문에서는 시리얼모니터 출력을 위한 Serial.begin()과 BLE 통신을 위한 BLE.begin()문이 있다.
  • BLE.scan()이 호출되면 scan을 시작한다.
  • BLE.available()는 인근에 advertising 중인 peripheral device를 발견했는지 확인해준다.
  • loop()문에 정의된 print문들에 따라 여러 정보를 출력하게 된다. 이 예제에서는 주변 기기를 발견하더라도 따로 연결하지는 않고 그저 스캔만 주기적으로 계속 수행한다.
  • 검출한  BLE 디바이스 기기의 MAC 주소와 이름 그리고 UUID를 알아낼 수 있다.

 

코드

 

/*
  Scan

  This example scans for Bluetooth® Low Energy peripherals and prints out their advertising details:
  address, local name, advertised service UUID's.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  Serial.println("Bluetooth® Low Energy Central scan");

  // start scanning for peripheral
  BLE.scan();
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral
    Serial.println("Discovered a peripheral");
    Serial.println("-----------------------");

    // print address
    Serial.print("Address: ");
    Serial.println(peripheral.address());

    // print the local name, if present
    if (peripheral.hasLocalName()) {
      Serial.print("Local Name: ");
      Serial.println(peripheral.localName());
    }

    // print the advertised service UUIDs, if present
    if (peripheral.hasAdvertisedServiceUuid()) {
      Serial.print("Service UUIDs: ");
      for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
        Serial.print(peripheral.advertisedServiceUuid(i));
        Serial.print(" ");
      }
      Serial.println();
    }

    // print the RSSI
    Serial.print("RSSI: ");
    Serial.println(peripheral.rssi());

    Serial.println();
  }
}

 

 

코드 설명

 

  • Central device (Client)가 자신에게 정보를 줄 server를 찾기위해 scan하는 예제다.
  • setup()문에서는 시리얼모니터 출력을 위한 Serial.begin()과 BLE 통신을 위한 BLE.begin()문이 있다. 최대한 기존 arduino API와 형식을 비슷하게 맞춰주기 위한 노력이 보인다.
  • BLE.scan()이 호출되면 scan을 시작한다.
  • BLE.available()는 인근에 advertising 중인 peripheral device를 발견했는지 확인해준다. loop()문에 정의된 print문들에 따라 여러 정보를 출력하게 된다.

이 예제에서는 주변 기기를 발견하더라도 따로 연결하지는 않고 그저 스캔만 주기적으로 계속 수행한다. 스캔을 할 수 있다, 검출한 기기의 MAC 주소와 이름 그리고 UUID를 알아낼 수 있다. 

 

 

실행 결과

 

Bluetooth® Low Energy Central scan
Discovered a peripheral
-----------------------
Address: 17:12:4b:c0:60:60
RSSI: -51

Discovered a peripheral
-----------------------
Address: 06:0e:3c:e2:3f:b1
Service UUIDs: fe78 
RSSI: -39

Discovered a peripheral
-----------------------
Address: f3:7f:09:ad:6c:e2
Local Name: PuppyDoc
Service UUIDs: 180f fffa 090a 7070 
RSSI: -72

 

 

참고 문서

BLE 개발 관련 내용 정리

 

 

 

 

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

당신의 성공을 위해 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩