nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart)

本文主要是介绍nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       nRF52832技术交流群:680723714

       nRF52832-Bluefruit52核心板详细介绍:

https://blog.csdn.net/solar_Lan/article/details/88688451

       github仓库地址:https://github.com/Afantor/Bluefruit52_Arduino.git

       

       Arduino例程目前分为6大部分:Central、DualRoles、Display、Hardware、Peripheral、Project。

一、Central:主设备与其他设备通信例程

二、DualRoles:从设备与主设备通信例程

本篇讲解蓝牙组网一拖8实验,本实验展示了以一个nRF52382为中心,其他nRF52832模块为外围设备,进行级联通信控制的过程。示例中演示串口打印连接状态和接收到数据,通过数据收发控制板载LED灯。主机中心为一个主模式设备,检测按键发送控制LED的字符。

下面直接讲解代码:

Client Services客户端服务

由于Central角色访问外围设备上的GATT服务器,我们首先需要使用BLEClientUart帮助程序类声明客户端bleuart实例。 如果还使用BLEClientDis,我们也可以方便地读取设备信息。

BLEClientDis  clientDis;
BLEClientUart clientUart;

在我们配置客户端服务之前,必须至少调用Bluefruit.begin(),以获得中央模式支持的并发连接数。 由于在这种情况下我们不会将nRF52作为外设运行,因此我们将外设计数设置为0:

// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
Bluefruit.begin(0, 1);

之后,必须通过调用begin()函数初始化客户端服务,并且可以从helper类设置任何要使用的回调:

// Configure DIS client
clientDis.begin();// Init BLE Central Uart Serivce
clientUart.begin();
clientUart.setRxCallback(bleuart_rx_callback);

Scanner扫描器

让我们启动广告扫描仪找到一个外围设备。

我们将使用setRxCallback()连接扫描结果回调。

每当扫描仪发现广告数据时,它将被传递给该回调处理程序,我们可以检查那里的广告数据,并且只连接到广播bleuart服务的外围设备。

注意:如果外围设备有多个服务且广告包中的UUID列表中未包含bleuart,您可以选择使用其他检查,例如匹配MAC地址,名称检查,使用“其他服务”等。

找到我们希望与之通信的外围设备后,请调用Bluefruit.Central.connect()与其建立连接:

void setup()
{// Other set up ...../* Start Central Scanning* - Enable auto scan if disconnected* - Interval = 100 ms, window = 80 ms* - Don't use active scan* - Start(timeout) with timeout = 0 will scan forever (until connected)*/Bluefruit.Scanner.setRxCallback(scan_callback);Bluefruit.Scanner.restartOnDisconnect(true);Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 msBluefruit.Scanner.useActiveScan(false);Bluefruit.Scanner.start(0);                   // // 0 = Don't stop scanning after n seconds
}/*** Callback invoked when scanner pick up an advertising data* @param report Structural advertising data*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{// Check if advertising contain BleUart serviceif ( Bluefruit.Scanner.checkReportForService(report, clientUart) ){Serial.print("BLE UART service detected. Connecting ... ");// Connect to device with bleuart service in advertisingBluefruit.Central.connect(report);}
}

Central Role中心角色

通常需要设置中央模式设备的连接回调,该连接回调在与外围设备建立/断开连接时触发。 或者,可以使用connected()轮询连接状态,但回调有助于显着简化代码:

// Callbacks for Central
Bluefruit.Central.setConnectCallback(connect_callback);
Bluefruit.Central.setDisconnectCallback(disconnect_callback);

在连接回调中,我们将尝试通过浏览外围设备的GATT表来发现bleuart服务。 这将有助于确定特征的句柄值(例如TXD,RXD等)。 这完全由BLEClientUart的.discover()完成。 找到服务后,启用TXD特性的CCCD以允许外设发送数据,我们准备在设备之间来回发送数据:

void connect_callback(uint16_t conn_handle)
{Serial.println("Connected");Serial.print("Dicovering DIS ... ");if ( clientDis.discover(conn_handle) ){Serial.println("Found it");char buffer[32+1];// read and print out Manufacturermemset(buffer, 0, sizeof(buffer));if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ){Serial.print("Manufacturer: ");Serial.println(buffer);}// read and print out Model Numbermemset(buffer, 0, sizeof(buffer));if ( clientDis.getModel(buffer, sizeof(buffer)) ){Serial.print("Model: ");Serial.println(buffer);}Serial.println();}  Serial.print("Discovering BLE Uart Service ... ");if ( clientUart.discover(conn_handle) ){Serial.println("Found it");Serial.println("Enable TXD's notify");clientUart.enableTXD();Serial.println("Ready to receive from peripheral");}else{Serial.println("Found NONE");// disconect since we couldn't find bleuart serviceBluefruit.Central.disconnect(conn_handle);}  
}

Full Sample Code完整的例程代码:

/*********************************************************************This is an example for our nRF52 based Bluefruit LE modulesPick one up today in the adafruit shop!Adafruit invests time and resources providing this open source code,please support Adafruit and open-source hardware by purchasingproducts from Adafruit!MIT license, check LICENSE for more informationAll text above, and the splash screen below must be included inany redistribution
*********************************************************************//** This sketch demonstrate the central API(). A additional bluefruit* that has bleuart as peripheral is required for the demo.*/
#include <bluefruit.h>BLEClientBas  clientBas;  // battery client
BLEClientDis  clientDis;  // device information client
BLEClientUart clientUart; // bleuart clientvoid setup()
{Serial.begin(115200);while ( !Serial ) delay(10);   // for nrf52840 with native usbSerial.println("Bluefruit52 Central BLEUART Example");Serial.println("-----------------------------------\n");// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1// SRAM usage required by SoftDevice will increase dramatically with number of connectionsBluefruit.begin(0, 1);Bluefruit.setName("Bluefruit52 Central");// Configure Battyer clientclientBas.begin();  // Configure DIS clientclientDis.begin();// Init BLE Central Uart SerivceclientUart.begin();clientUart.setRxCallback(bleuart_rx_callback);// Increase Blink rate to different from PrPh advertising modeBluefruit.setConnLedInterval(250);// Callbacks for CentralBluefruit.Central.setConnectCallback(connect_callback);Bluefruit.Central.setDisconnectCallback(disconnect_callback);/* Start Central Scanning* - Enable auto scan if disconnected* - Interval = 100 ms, window = 80 ms* - Don't use active scan* - Start(timeout) with timeout = 0 will scan forever (until connected)*/Bluefruit.Scanner.setRxCallback(scan_callback);Bluefruit.Scanner.restartOnDisconnect(true);Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 msBluefruit.Scanner.useActiveScan(false);Bluefruit.Scanner.start(0);                   // // 0 = Don't stop scanning after n seconds
}/*** Callback invoked when scanner pick up an advertising data* @param report Structural advertising data*/
void scan_callback(ble_gap_evt_adv_report_t* report)
{// Check if advertising contain BleUart serviceif ( Bluefruit.Scanner.checkReportForService(report, clientUart) ){Serial.print("BLE UART service detected. Connecting ... ");// Connect to device with bleuart service in advertisingBluefruit.Central.connect(report);}else{      // For Softdevice v6: after received a report, scanner will be paused// We need to call Scanner resume() to continue scanningBluefruit.Scanner.resume();}
}/*** Callback invoked when an connection is established* @param conn_handle*/
void connect_callback(uint16_t conn_handle)
{Serial.println("Connected");Serial.print("Dicovering Device Information ... ");if ( clientDis.discover(conn_handle) ){Serial.println("Found it");char buffer[32+1];// read and print out Manufacturermemset(buffer, 0, sizeof(buffer));if ( clientDis.getManufacturer(buffer, sizeof(buffer)) ){Serial.print("Manufacturer: ");Serial.println(buffer);}// read and print out Model Numbermemset(buffer, 0, sizeof(buffer));if ( clientDis.getModel(buffer, sizeof(buffer)) ){Serial.print("Model: ");Serial.println(buffer);}Serial.println();}else{Serial.println("Found NONE");}Serial.print("Dicovering Battery ... ");if ( clientBas.discover(conn_handle) ){Serial.println("Found it");Serial.print("Battery level: ");Serial.print(clientBas.read());Serial.println("%");}else{Serial.println("Found NONE");}Serial.print("Discovering BLE Uart Service ... ");if ( clientUart.discover(conn_handle) ){Serial.println("Found it");Serial.println("Enable TXD's notify");clientUart.enableTXD();Serial.println("Ready to receive from peripheral");}else{Serial.println("Found NONE");// disconnect since we couldn't find bleuart serviceBluefruit.disconnect(conn_handle);}  
}/*** Callback invoked when a connection is dropped* @param conn_handle* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{(void) conn_handle;(void) reason;Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}/*** Callback invoked when uart received data* @param uart_svc Reference object to the service where the data * arrived. In this example it is clientUart*/
void bleuart_rx_callback(BLEClientUart& uart_svc)
{Serial.print("[RX]: ");while ( uart_svc.available() ){Serial.print( (char) uart_svc.read() );}Serial.println();
}void loop()
{if ( Bluefruit.Central.connected() ){// Not discovered yetif ( clientUart.discovered() ){// Discovered means in working state// Get Serial input and send to Peripheralif ( Serial.available() ){delay(2); // delay a bit for all characters to arrivechar str[20+1] = { 0 };Serial.readBytes(str, 20);clientUart.print( str );}}}
}

从上面分析的代码流程,我们很容易理解蓝牙组网连接的流程。本例程代码是主机端的,从机端将在下一篇博文。从最后的实验现象我们可以看出,蓝牙组网中主机只能存在一个设备,从机可以是多个设备,从机是主从模式存在的,所以此蓝牙网络是一种级联的形式,中间连接的从设备既充当主机又是中继站。

这篇关于nRF52832-Bluefruit52学习之Arduino开发(3)-- 蓝牙组网一拖8主机模式(central_bleuart)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/621467

相关文章

VSCode开发中有哪些好用的插件和快捷键

《VSCode开发中有哪些好用的插件和快捷键》作为全球最受欢迎的编程工具,VSCode的快捷键体系是提升开发效率的核心密码,:本文主要介绍VSCode开发中有哪些好用的插件和快捷键的相关资料,文中... 目录前言1、vscode插件1.1 Live-server1.2 Auto Rename Tag1.3

Agent开发核心技术解析以及现代Agent架构设计

《Agent开发核心技术解析以及现代Agent架构设计》在人工智能领域,Agent并非一个全新的概念,但在大模型时代,它被赋予了全新的生命力,简单来说,Agent是一个能够自主感知环境、理解任务、制定... 目录一、回归本源:到底什么是Agent?二、核心链路拆解:Agent的"大脑"与"四肢"1. 规划模

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Go语言实现桥接模式

《Go语言实现桥接模式》桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,本文就来介绍一下了Go语言实现桥接模式,感兴趣的可以了解一下... 目录简介核心概念为什么使用桥接模式?应用场景案例分析步骤一:定义实现接口步骤二:创建具体实现类步骤三:定义抽象类步骤四:创建扩展抽象类步

Python+wxPython开发一个文件属性比对工具

《Python+wxPython开发一个文件属性比对工具》在日常的文件管理工作中,我们经常会遇到同一个文件存在多个版本,或者需要验证备份文件与源文件是否一致,下面我们就来看看如何使用wxPython模... 目录引言项目背景与需求应用场景核心需求运行结果技术选型程序设计界面布局核心功能模块关键代码解析文件大

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++中的解释器模式实例详解

《C++中的解释器模式实例详解》这篇文章总结了C++标准库中的算法分类,还介绍了sort和stable_sort的区别,以及remove和erase的结合使用,结合实例代码给大家介绍的非常详细,感兴趣... 目录1、非修改序列算法1.1 find 和 find_if1.2 count 和 count_if1

Redis中群集三种模式的实现

《Redis中群集三种模式的实现》Redis群集有三种模式,分别是主从同步/复制、哨兵模式、Cluster,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1. Redis三种模式概述2、Redis 主从复制2.1 主从复制的作用2.2 主从复制流程2

深入理解MySQL流模式

《深入理解MySQL流模式》MySQL的Binlog流模式是一种实时读取二进制日志的技术,允许下游系统几乎无延迟地获取数据库变更事件,适用于需要极低延迟复制的场景,感兴趣的可以了解一下... 目录核心概念一句话总结1. 背景知识:什么是 Binlog?2. 传统方式 vs. 流模式传统文件方式 (非流式)流

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D