> For the complete documentation index, see [llms.txt](https://docs.petoi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.petoi.com/thai/biboard/demo-applications/10.classic-bluetooth-serial-port-spp.md).

# 10.Classic Bluetooth serial port SPP

โค้ดตัวอย่างส่วนใหญ่แสดงให้เห็นถึงการส่งต่อข้อมูลร่วมกันระหว่างพอร์ตอนุกรม Bluetooth และพอร์ตอนุกรม ซึ่งได้มาจากการตัวอย่าง อย่างเป็นทางการของ ESP32 ซึ่งเรียบง่ายและเข้าใจได้ง่าย คำอธิบายจึงอธิบายแนวคิดที่ปรากฏในโค้ดเป็นหลัก

### 1. Bluetooth protocol

ในปัจจุบัน โปรโตคอล Bluetooth หลักแบ่งออกเป็นสองประเภท ได้แก่ Bluetooth แบบดั้งเดิม (HS/BR/EDR) ตาม RFCOMM และ Bluetooth พลังงานต่ำ (BLE) ตาม GATT

Bluetooth แบบดั้งเดิมนั้นเร็วกว่าและมีโปรโตคอลแอปพลิเคชันเฉพาะมากมาย เช่น A2DP ที่เน้นเสียง, พอร์ตอนุกรม Bluetooth SPP เป็นต้น อย่างไรก็ตาม การใช้พลังงานสูง และการเข้าถึงอุปกรณ์ Apple ต้องใช้ชิป MFi (Made For iOS) และการรับรอง

Bluetooth Low Energy (BLE) สามารถกำหนดโปรไฟล์ GATT ต่างๆ ได้ด้วยตัวเอง และยังมีโปรไฟล์ที่ใช้กันทั่วไป (เช่น ข้อมูลอุปกรณ์ แบตเตอรี่ ฯลฯ) มีการใช้พลังงานต่ำและใช้กันอย่างแพร่หลาย สามารถใช้กับอุปกรณ์ Apple ข้อเสียคือช้ากว่าบลูทูธแบบเดิม บลูทูธพลังงานต่ำส่วนใหญ่จะใช้กับอุปกรณ์ที่มีปริมาณข้อมูลน้อยแต่ไวต่อการใช้พลังงาน เช่น สร้อยข้อมือ/นาฬิกาอัจฉริยะ/บีคอน

### 2. Classic Bluetooth serial port (SPP)

การสาธิตนี้ใช้โปรโตคอล SPP ตามบลูทูธแบบดั้งเดิม ซึ่งมาพร้อมกับโปรโตคอลพอร์ตอนุกรมทั้งหมด เมื่อคอมพิวเตอร์หรือโทรศัพท์ Android เชื่อมต่อและจับคู่ หมายเลขพอร์ตอนุกรมจะถูกสร้างขึ้นโดยอัตโนมัติในระบบสำหรับการสื่อสาร และประสบการณ์การใช้งานจะไม่แตกต่างจากพอร์ตอนุกรมแบบมีสายทั่วไปมากนัก

```cpp
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("BTSPP_Test");          //Bluetooth device name
  Serial.println("The device started,");
  Serial.println("Now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}
```

พอร์ตอนุกรมบลูทูธพลังงานต่ำจะแสดงให้เห็นในบทถัดไป โดยพื้นฐานแล้ว เป็นโปรไฟล์ที่กำหนดค่าด้วยพอร์ตอนุกรมและต้องการ การสนับสนุนของโฮสต์ซอฟต์แวร์


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.petoi.com/thai/biboard/demo-applications/10.classic-bluetooth-serial-port-spp.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
