Bluetooth APP based on E4A


foreword

After two days of exploration, I finally realized a simple Bluetooth APP. The Bluetooth module uses HC-05, and the APP is developed with Yi Android. The codes are all in Chinese, and I can get started quickly. If you want to make a low-power Bluetooth APP, you can refer to this BLE Bluetooth APP development


1. Preliminary preparation

1. Install the E4A development tool download
link . If you need to change the APP function, please download it. The software is not large, only more than 300 megabytes.
2. Bluetooth module HC-05
Before using the APP, use the mobile phone Bluetooth to complete the pairing.
3. USB to serial port module
Used to supply power to the Bluetooth module and output data to the serial port assistant display

2. Video demonstration

Simple Bluetooth Demo Video

3. Graphic explanation

APP interface:
Click the "Start Search" button to add the nearby Bluetooth to the list box, click the list box to connect to the corresponding Bluetooth, note: only the paired Bluetooth can be successfully connected. Clicking the "Connected" button will disconnect and the button title will change to Not Connected.
insert image description here
Serial port assistant interface:
the baud rate is set to 9600, the serial port assistant receives data from the mobile APP, and can also send data to the APP.
insert image description here
Physical picture:
use the USB to serial port module to connect with the Bluetooth module, connection method: RX–>TX TX–>RX
insert image description here

4. Bluetooth APP source code

变量 设备地址 为 文本型

事件 主窗口.创建完毕()
	'安卓6.0以上的系统需要动态申请权限,否则app可能无法正常运行
	如果 权限操作1.取系统版本号()>=23 则
		权限操作1.申请全部权限()
	结束 如果	
	初始化蓝牙()

结束 事件

事件 权限操作1.申请完毕(权限数组 为 文本型(),申请结果 为 整数型())
	
	'可以把需要特殊权限的代码写在此处,当申请权限成功后再执行相关代码
	
结束 事件

过程 初始化蓝牙()
	如果 蓝牙1.是否存在() = 假 则
		信息框("信息","本机找不到蓝牙设备,程序将退出!","确定")
		结束程序()
	结束 如果
	如果 蓝牙1.是否已开启() = 假 则
		蓝牙1.开启蓝牙()
	否则	
		蓝牙1.置工作模式(2)
	结束 如果		
结束 过程

事件 蓝牙1.蓝牙设置完毕(设置结果 为 整数型)
	
	如果 设置结果 = 1 则
		蓝牙1.置工作模式(2)
	结束 如果	
	
结束 事件

事件 图片框_返回.被单击()
	
	返回桌面()
	
结束 事件

事件 退出标签.被单击()
	
	蓝牙1.断开连接() '退出处理,释放资源,结束内部处理线程
	结束程序()
	
结束 事件
	
事件 搜索按钮.被单击()
	
	如果 搜索按钮.标题 = "开始搜索" 则
		位置传感器1.开始监测() '在新版的安卓系统中,必须开启GPS定位才能搜索到蓝牙设备
	    列表框1.清空项目()
      	蓝牙1.搜索设备() '搜索设备
        时钟1.时钟周期 = 10*1000   '10S
        搜索按钮.标题 = "停止搜索"
    否则        
        蓝牙1.停止搜索()
        时钟1.时钟周期 = 0
        搜索按钮.标题 = "开始搜索"
        结束 如果
	
结束 事件
	
事件 时钟1.周期事件()

    蓝牙1.停止搜索()
    搜索按钮.标题 = "开始搜索"
    时钟1.时钟周期 = 0

结束 事件

事件 蓝牙1.发现设备(设备名称 为 文本型,设备地址 为 文本型,是否已配对 为 逻辑型)
	
	列表框1.添加项目("名称:" & 设备名称 & "\n地址:" & 设备地址)
    列表框1.置项目标记(列表框1.取项目数()-1,设备地址)  
	
结束 事件

事件 列表框1.表项被单击(项目索引 为 整数型)	
	
	设备地址 = 列表框1.取项目标记(项目索引)
	蓝牙1.连接设备(设备地址)'连接设备
	弹出提示("正在连接" & 设备地址)
	
结束 事件

事件 蓝牙1.连接完毕(连接结果 为 逻辑型,设备名称 为 文本型,设备地址 为 文本型,连接模式 为 整数型)
	
	如果 连接结果 = 真 则
		头部标签.标题 = "已连接:" & 设备名称
	    连接状态.标题 = "已连接"
	    连接状态.背景颜色 = 绿色
		弹出提示("连接成功")
	否则
		弹出提示("连接失败")
		连接状态.标题 = "未连接"
		连接状态.背景颜色 = 红色
		头部标签.标题 = "简易蓝牙" 
	结束 如果
	
结束 事件

事件 蓝牙1.收到数据(数据 为 字节型(),设备名称 为 文本型,设备地址 为 文本型)
	
	接收框.内容 = 接收框.内容 & "\n收<-" & 字节到文本(数据,"GBK") 
	'接收框.内容 = 接收框.内容 & 字节到文本(数据,"GBK")  
	
	接收框.置光标位置(取文本长度(接收框.内容))
  
'=====================以下是接收单片机发送过来的16进制HEX数据的方法=======================	
'	变量 计次 为 整数型
'	判断循环首 计次 < 取数组成员数(数据)
'		接收框.内容 = 接收框.内容 & "  " & 到十六进制(数据(计次)) 
'		计次 = 计次 + 1
'	判断循环尾	
结束 事件

事件 发送按钮.被单击()
	
	'如果 发送框.内容 <> "" 则
	如果 取文本长度(发送框.内容) > 0  则
		接收框.内容 = 接收框.内容 & "\n发->" & 发送框.内容
		如果 连接状态.标题 = "已连接" 则
         	蓝牙1.发送数据(文本到字节(发送框.内容,"GBK")) '发送消息
        结束 如果
	
	否则
		'信息框("信息","消息内容不能为空!","确定")
	结束 如果	
	接收框.置光标位置(取文本长度(接收框.内容))
	
结束 事件

事件 连接状态.被单击()
	
	 如果 连接状态.背景颜色 = 绿色 则
        蓝牙1.断开连接()
	   连接状态.标题 = "正在断开"
	   
    否则
        连接状态.标题 = "正在连接"
	    蓝牙1.连接设备(设备地址) 
		弹出提示("正在连接" & 设备地址)
        结束 如果
	
结束 事件

事件 蓝牙1.连接断开()

    连接状态.标题 = "未连接"
	连接状态.背景颜色 = 红色
	头部标签.标题 = "简易蓝牙"
     弹出提示("已断开")
结束 事件

事件 清空按钮.被单击()
	
	接收框.内容 = 空
	
结束 事件

事件 按钮上.被单击()
	
    如果 连接状态.标题 = "已连接" 则
        蓝牙1.发送数据(文本到字节("上","GBK")) '发送消息
    结束 如果
    接收框.内容 = 接收框.内容 & "\n发->" & "上"
	接收框.置光标位置(取文本长度(接收框.内容))
	
结束 事件


事件 按钮下.被单击()
	
	如果 连接状态.标题 = "已连接" 则
        蓝牙1.发送数据(文本到字节("下","GBK")) '发送消息
    结束 如果
    接收框.内容 = 接收框.内容 & "\n发->" & "下"
   
    接收框.置光标位置(取文本长度(接收框.内容))
结束 事件


事件 按钮左.被单击()
	
	如果 连接状态.标题 = "已连接" 则
          蓝牙1.发送数据(文本到字节("左","GBK")) '发送消息
    结束 如果
    接收框.内容 = 接收框.内容 & "\n发->" & "左"
    接收框.置光标位置(取文本长度(接收框.内容))
	
结束 事件

事件 按钮右.被单击()
	
    如果 连接状态.标题 = "已连接" 则
          蓝牙1.发送数据(文本到字节("右","GBK")) '发送消息
    结束 如果
    接收框.内容 = 接收框.内容 & "\n发->" & "右"
    接收框.置光标位置(取文本长度(接收框.内容))
	
结束 事件

5. Interface layout

insert image description here

Six, source code download

Those who need the source code can download it by themselves. Download link
Download operation:
insert image description here

7. E4A software experience

1. Cultivate the habit of saving at hand, the software is easy to crash
2. In order to prevent the software from crashing due to pressing the TAB key, block the TAB key in the setting options, and then restart
3. In the "File" tab of the menu bar, "Open Routine" "There are many learning routines, which can be used as a reference
4. In the "Manual" in the toolbar, you can view the introduction of the software and the syntax of programming.
5. When the Bluetooth APP crashes or the device cannot be found, turn on the location authority of the APP
. USB debugging mode, and then use USB to connect to the computer. In this way, the software will be automatically installed on the mobile phone after running the code, and the software will be uninstalled from the mobile phone when it stops running.
7. Bluetooth cannot be turned on in the emulator, and the emulator and mobile phone can only be connected to one


Summarize

In the future, the self-made Bluetooth APP can be used to realize the remote control function of the single-chip microcomputer. Although it is relatively simple to use E4A to develop an APP, it can only be used by amateurs like us for entertainment.

Guess you like

Origin blog.csdn.net/NICHUN12345/article/details/124711683