0Pricing
Arduino & IoT Academy · 课时

读取并写入特征

通过 BLE 属性交换数值

读取并写入特征 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Where the Data Lives

A service is a folder, but the real values live inside characteristics. These are the slots a phone actually reads and writes. 📦

Characteristics Have UUIDs Too

Like services, every characteristic carries its own UUID. The phone uses it to target one exact value, such as temperature.

Create One

You build a characteristic on a service and declare what it allows: reading, writing, or both. Properties decide who can do what.

auto c = svc->createCharacteristic(uuid,
  BLECharacteristic::PROPERTY_READ);

Properties Set Permissions

Use PROPERTY_READ for values the phone fetches, and PROPERTY_WRITE for ones it sends back. Combine them when both directions matter.

Set a Value to Read

Your sketch stores the current data with setValue. When the phone reads, it gets exactly whatever you placed there last.

c->setValue("23.5");

The Phone Writes Back

For a writable characteristic, the phone can push a value to your board. Your code reads it with getValue to learn what arrived.

std::string in = c->getValue();

React with a Callback

To respond the instant a write happens, attach a callback. Its onWrite method fires automatically whenever the phone sends data.

Notify Instead of Polling

With notify, your board pushes new values the moment they change. The phone gets fresh data without asking again and again.

c->setValue(temp);
c->notify();

Values Are Just Bytes

BLE sends raw bytes, not numbers. Send text like 23.5 or pack the number yourself, then unpack it on the phone side.

Keep Packets Small

A default BLE write fits about twenty bytes. Keep each characteristic focused on one small value rather than a giant blob.

Read, Write, Notify

Those three jobs cover almost every project. Read shares state, write accepts commands, and notify streams live updates.

Quick Check

You want the phone to get new readings the instant they change.

Recap

Characteristics hold the data: set properties for read, write, or notify, store values, and let callbacks react to incoming writes. 🚀

常见问题解答

「读取并写入特征」课时是免费的吗?

是的 — 「读取并写入特征」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「读取并写入特征」这节课中我会学到什么?

通过 BLE 属性交换数值 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「读取并写入特征」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 经典蓝牙与 BLE
  2. 广播 BLE 服务
  3. 读取并写入特征
  4. 使用手机应用控制 LED
← 返回 Arduino & IoT Academy