ESP32蓝牙接收并截取指定格式的字符串
typedef unsigned char u8;
# include "BluetoothSerial.h"
# include <string.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;
char receive_data[ 40 ] , WIFI_ZH[ 20 ] , WIFI_MM[ 20 ] ;
void LanYa_scan ( void ) {
u8 shuju_flag= 0 , num= 0 , i= 1 , j= 0 , shuju= 0 , JL_sj= 0 ;
char Data;
while ( SerialBT. available ( ) ) {
Data= SerialBT. read ( ) ;
if ( Data== '[' ) {
shuju_flag= 1 , num= 0 ;
memset ( receive_data, 0 , sizeof ( receive_data) ) ;
memset ( WIFI_ZH, 0 , sizeof ( WIFI_ZH) ) ;
memset ( WIFI_MM, 0 , sizeof ( WIFI_MM) ) ;
}
if ( Data== ']' )
{
shuju= 1 ;
shuju_flag= 0 ;
}
if ( shuju_flag== 1 )
{
receive_data[ num] = Data;
num++ ;
}
}
if ( shuju== 1 ) {
Serial. println ( "WIFI_ZH:" ) ;
for ( i= 1 ; i< num; i++ ) {
if ( receive_data[ i] == ',' ) {
JL_sj= 1 ;
Serial. println ( ) ;
Serial. println ( "WIFI_MM:" ) ;
}
if ( JL_sj== 0 ) {
WIFI_ZH[ i- 1 ] = receive_data[ i] ;
Serial. print ( WIFI_ZH[ i- 1 ] ) ;
}
if ( JL_sj== 1 ) {
if ( i< num- 1 ) {
WIFI_MM[ i- j] = receive_data[ i+ 1 ] ;
Serial. print ( WIFI_MM[ i- j] ) ;
}
}
if ( i== num- 1 ) {
JL_sj= 0 ;
}
}
shuju= 0 ;
Serial. println ( ) ;
}
}
void setup ( ) {
Serial. begin ( 115200 ) ;
SerialBT. begin ( "ESP32test" ) ;
Serial. println ( "蓝牙已启动,你可以配对了!" ) ;
}
void loop ( ) {
LanYa_scan ( ) ;
delay ( 10 ) ;
}