ประกาศบริการ BLE
ทำให้ ESP32 ของคุณมองเห็นได้เพื่อให้โทรศัพท์ค้นพบ
ประกาศบริการ BLE เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Make Your Board Findable
Before a phone can connect, your ESP32 must announce itself. This shouting into the air is called advertising. 📣
What Advertising Sends
Advertising broadcasts tiny packets with your device name and what it offers, so nearby phones can list it without connecting yet.
Start with the Library
The Arduino core gives you a BLE library. Include it once, then everything for advertising and connecting becomes a few clean calls.
#include <BLEDevice.h>
#include <BLEServer.h>Name Your Device
First you initialize BLE and give your board a friendly name. That name is what users will tap in their phone's Bluetooth list.
BLEDevice::init("Smart-Sensor");Create a Server
Your board acts as a BLE server: it holds the data, and the phone is the client that asks for it. Create the server next.
BLEServer *server = BLEDevice::createServer();What a Service Is
A service is a labeled group of related data, like all the values for one sensor. A device can offer several services at once.
Services Need a UUID
Each service is identified by a UUID, a long unique code. The phone uses this exact id to find the right service among many.
BLEService *svc = server->createService("180F");Standard vs Custom UUIDs
Short UUIDs like 180F are official standard ones. For your own data, generate a long custom UUID so it never clashes with others.
Start the Service
Once the service is defined, you must start it. Until you do, the phone cannot actually use any of its data.
svc->start();Begin Advertising
Finally, tell the radio to advertise. Now the board broadcasts its name and service so phones nearby can discover it.
server->getAdvertising()->start();Find It on Your Phone
Open a scanner app like nRF Connect and you should see Smart-Sensor appear. Seeing your name means advertising works.
Quick Check
You wrote a service but the phone shows no data inside it.
Recap
Init BLE, name the board, make a server, add a service with a UUID, start it, then advertise. Now your device is discoverable. ✨
คำถามที่พบบ่อย
บทเรียน “ประกาศบริการ BLE” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ประกาศบริการ BLE” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ประกาศบริการ BLE”
ทำให้ ESP32 ของคุณมองเห็นได้เพื่อให้โทรศัพท์ค้นพบ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ประกาศบริการ BLE” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ