使用Arduino开发ESP32(十五):使用Bluetooth(蓝牙)进行串行通信,经典蓝牙,非BLE

Bluetooth Serial Library(蓝牙串口库)

A simple Serial compatible library using ESP32 classical bluetooth (SPP)
一个用到了ESP32的蓝牙()库的简单串口兼容库

如何使用它?
  • Download one bluetooth terminal app in your smartphone

  • 在你的手机上下载蓝牙终端app

    For Android

    For IOS

  • Flash an example sketch to your ESP32

  • 给你的esp32烧写一个例程

  • Scan and pair the device in your smartphone

  • 用你的手机扫描并配对蓝牙

  • Open the bluetooth terminal app

  • 打开app

函数介绍:

Serial.available() :返回串口缓冲区中当前剩余的字符个数。
Serial.print() :发送的是字符,
Serial.write() :发送的字节.

代码:

//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

//这个例程为串口和传统蓝牙架设了一座桥梁
//同时也演示了SerialBT和普通的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("ESP32test"); //蓝牙模块名称
  Serial.println("蓝牙已启动,你可以配对了!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
    Serial.println("由SerialBT打印");
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
    Serial.println("由Serial打印");
  }
  delay(20);
}

测试:
打开app,点击左侧菜单
在这里插入图片描述
点击devices,找到名称点击配对
在这里插入图片描述

发送信息在这里插入图片描述

结果:
在这里插入图片描述

  • 10
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值