I’m using an Arduino Nano 33 BLE Sense!!!
#include <ArduinoBLE.h>
const int BUTTON_PIN = 2;
void setup()
{
pinMode( BUTTON_PIN, INPUT );
BLE.begin();
}
void loop()
{
const int DATA_SIZE = 1;
uint8_t data[DATA_SIZE];
static int oldButtonState = LOW;
int buttonState = digitalRead( BUTTON_PIN );
if ( oldButtonState != buttonState )
{
oldButtonState = buttonState;
data[0] = ( uint8_t ) buttonState;
BLE.stopAdvertise();
BLE.setManufacturerData( data, DATA_SIZE );
BLE.advertise();
}
}
Teacher,
I have a question so I have a message.
In the source code above
Is there a way to read and store’BLE.setManufacturerData() 'in Central?
I really appreciate it…
Here is my Peripheral code:
//-------------------------------------------------------------------------------
#include <ArduinoBLE.h>
//나노33BLE 라이브러리
#include <Arduino_HTS221.h>
//온도,습도 라이브러리
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
std::string _uuid;
int _major;
int _minor;
int _tx;
const int ledPin = LED_BUILTIN;
// 내장 LED핀을 정의한다.
//-------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); //시리얼 통신 시작
pinMode(ledPin, OUTPUT); //내장LED OUTPUT 지정
pinMode(D2, OUTPUT); // D2 LED OUTPUT 지정
pinMode(D3, OUTPUT); // D3 LED OUTPUT 지정
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
if (!HTS.begin()) {
Serial.println("Failed to initialize humidity temperature sensor!");
while (1);
}
BLE.setLocalName("이동렬TEST"); // 광고할 peripheral UUID의 로컬네임을 지정한다.
//------------------------------------------------------------------------------------------------------
}
void begin(std::string uuid, int major, int minor, int tx){
_uuid=uuid;
_major=major;
_minor=minor;
_tx=tx;
}
void loop() {
BLE.poll();
float temperatureC = HTS.readTemperature();//32비트(4바이트) 정보를 저장한다
float humidity = HTS.readHumidity();//32비트(4바이트) 정보를 저장한다
digitalWrite(ledPin, HIGH);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
delay(250);
if (ledPin, HIGH)
{
Serial.println(" ★ 나와 특성을 광고했음 ★ ");
Serial.print("온도 = ");
Serial.print(temperatureC);
Serial.print(" °C, ");
Serial.print("습도 = ");
Serial.print(humidity);
Serial.println(" %");
}
if (ledPin, HIGH)
{
begin("c336aa38054bb0483b0ae75027061982",HTS.readTemperature(),HTS.readHumidity(),197);
byte uuidByte[16],majByte[2],minByte[2];
convertStringToByte(_uuid,uuidByte);
byte data[25]={
0X4C,0x00,
0x02,0x15,
uuidByte[0],
uuidByte[1],
uuidByte[2],
uuidByte[3],
uuidByte[4],
uuidByte[5],
uuidByte[6],
uuidByte[7],
uuidByte[8],
uuidByte[9],
uuidByte[10],
uuidByte[11],
uuidByte[12],
uuidByte[13],
uuidByte[14],
uuidByte[15],
0,
_major,
0,
_minor,
_tx
};
BLE.stopAdvertise();
BLE.setManufacturerData( data, 25 );
BLE.advertise();
}
}
void convertStringToByte(std::string uuid, byte bytes[]){
char uuidChar[uuid.length()+1];
strcpy(uuidChar,uuid.c_str());
int pos=0;
for (unsigned int i = 0; i<sizeof(uuidChar);i += 2){
char hexValue[2];
hexValue[0]=uuidChar[i];
hexValue[1]=uuidChar[i+1];
int intValue=strtol(hexValue,0, 16);
bytes[pos]=(char)intValue;
pos++;
}
}
Peripheral’s BLE.setManufacturerData(data, 25); I want to scan, save and read in Central… Please help sir.!!!