基于Paho Android Service 实现MQTT协议通信Android客户端
目录
- 1 MQTT & mosquitto及相关代理库和客户端库
- 1.1 MQTT
- 1.2 mosquitto
- 1.3 Phho Android Service
- 2 MQTT服务器搭建
- 2.1 Windows
- 2.2 Mac
- 2.3 Linux(Ubuntu18.04)
- 2.3.1 通过安装包管理工具安装
- 2.3.2 通过源码安装
- 3 Mosquitto的简单使用
- 3.1 mosquitto命令及配置简介
- 3.2 mosquitto-passwd命令
- 3.3 mosquitto_sub和mosquitto_pub使用
- 4 Paho Android Service & Mosquitto
- 5 引用
1 MQTT & mosquitto及相关代理库和客户端库
1.1 MQTT
MQTT是流行的数据通信或消息传递协议之一,它正广泛用于端对端(M2M)通信,或者在“物联网(LoT)”的计算机网络趋势。
MQTT(Message Queue Transport Telemetry,消息队列传输遥测)是一种具有发布 - 订阅模式的消息传递协议,该协议具有“轻量级”特性,这对于物联网架构非常重要,因为它主要涉及低功耗设备,
例如传感器节点,微控制器等。
MQTT协议的许多用途之一是从嵌入式设备发送传感器数据。
有时我们希望将这些数据发送到我们的智能手机,这可以帮助我们远程监控一些重要的事情。
1.2 mosquitto
Eclipse Mosquitto
是一个开源(EPL/ EDL许可)消息代理库,它实现了MQTT协议版本3.1和3.1.1,像mosquitto这样的消息代理库是有很多的,如ActiveMQ
Apollo
、HiveMQ
、Mosca
、Mosquitto
、RabbitMQ
、EMQ
,但是这个库确实MQTT协议制定作者开发的,而且开源。
Mosquitto是一个轻量级的库,适用于从低功耗单板计算机到完整服务器的所有设备。
Mosquitto
项目还提供了用于实现MQTT客户端的C库,以及非常流行的mosquitto_pub
和mosquitto_sub
命令行MQTT客户端。
MQTT
目前最新协议版本为3.1.1(截至到2018年12月3日),MQTT5
正在开发计划中,接口相对原来版本可能有所改变,计划在mosquitto
2.0发布,从mosquitto
1.6开始接口会开始改变。所以在协议兼容方面,想严格兼容3.1.1版本可以选择mosquitto
1.5版本。
1.3 Phho Android Service
Paho Android Service是一个用Java编写的MQTT客户端库,用于在Android上开发应用程序。
特点:
- 支持MQTT3.1/3.1.1
- 自动重连机制
- 离线缓冲机制
- 支持Websocket协议方式
- 支持标准的TCP方式
- 支持安全传输层协议SSL/TLS
- 支持
临终遗嘱
(LWT) - 消息持久性
2 MQTT服务器搭建
这一章主要详解Linux下的MQTT服务器的安装和配置,采用mosquitto代理库。
2.1 Windows
编译好的二进制文件下载链接: mosquitto-1.5.4-install-windows-x64.exe
安装后可以参考readme-windows.txt
。
2.2 Mac
在Mac系统中,可以通过os x 缺少的套件管理工具Homebrew来安装。先安装好Homebrew
,然后就可以直接用Homebrew来安装mosquitto。
# Mac安装mosquitto命令brew install mosquitto
2.3 Linux(Ubuntu18.04)
2.3.1 通过安装包管理工具安装
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppasudo apt-get update
2.3.2 通过源码安装
首先确保依赖的openssl和websocket已经安装好。
# openssl 安装$ sudo apt-get install openssl $ sudo apt-get install libssl-dev# libwebsockets 安装$ wget https://github.com/warmcat/libwebsockets/archive/v3.1.0.tar.gz$ tar -zxvf libwebsockets-3.1.0.tar.gz$ cd libwebsockets-3.1.0$ mkdir build$ cd build$ cmake ..$ make$ sudo make install# mosquitto编译安装# 配置好libwebsockets的头文件和库文件路径到环境变量中# ~/.bashrc# /usr/local/include# /usr/local/lib/$ wget https://mosquitto.org/files/source/mosquitto-1.5.4.tar.gz$ tar -zxvf mosquitto-1.5.4.tar.gz$ cd mosquitto$ mkdir build$ cd build$ cmake ..$ make $ sudo make install# 生成可执行文件/$your_target_dir/mosquitto/sbin/mosquitto/$your_target_dir/mosquitto/bin/mosquitto_pub/$your_target_dir/mosquitto/bin/mosquitto_sub/$your_target_dir/mosquitto/bin/mosquitto_passwd
3 Mosquitto的简单使用
3.1 mosquitto命令及配置简介
首先简单的介绍Mosquitto的用法,通过mosquitto自带的-h
帮助选项:
$ mosquitto -hmosquitto version 1.5.4mosquitto is an MQTT v3.1.1 broker.Usage: mosquitto [-c config_file] [-d] [-h] [-p port] # 指定配置文件 -c : specify the broker config file. # 是否启动在后台运行 -d : put the broker into the background after starting. -h : display this help. # 指定监听端口,默认端口为1883,不建议与-c选项一起使用 -p : start the broker listening on the specified port. Not recommended in conjunction with the -c option. # 打开调试模式,使能所有的log级别打印 -v : verbose mode - enable all logging types. This overrides any logging options given in the config file.See http://mosquitto.org/ for more information.
mosquitto的配置文件在/etc/mosquitto/mosquitto.conf中,主要内容为:
https://mosquitto.org/man/mosquitto-conf-5.html
通用设置为:
# 设置客户端是否需要通过用户名和密码来连接allow_anonymous [ true | false ]# 将内存中数据保存到磁盘中的等待时间,如果设置为0,则等到mosquitto退出或者收到SIGUSR1信号才会保存到磁盘。默认设置为1800秒autosave_interval seconds# 如果为true,mosquitto将计算订阅更改的数量,保留的消息和排队的消息,如果总数超过autosave_interval,则内存数据库将保存到磁盘。如果为false,mosquitto会将autosave_interval视为以秒为单位的时间将内存数据保存到磁盘autosave_on_changes [ true | false ]# 客户端id的前缀限制,只有id前缀为该值才能连接clientid_prefixes dms-# 如果设置为true,则日志将包括客户端连接和断开连接时的条目。如果设置为false,则不会显示这些条目。connection_messages [ true | false ]# 设置额外的配置文件目录include_dir dir# 设置将日志消息发送到特定目标(stdout/stderr/syslog/topic)log_dest destinations# 设置是否将时间戳加入到每条日志记录中,默认为truelog_timestamp [ true | false ]# 设置消息保存到日志的类型(debug, error, warning, notice, information, subscribe, unsubscribe, websockets, none, all) 默认值为error, warning, notice and information。log_type types# 消息大小限制,默认接收符合MQTT的消息类型,最大载荷为255Mbmessage_size_limit limit# QoS 1 or 2消息类型保存到队列中最大数目,设置为0,没有最大限制,不推荐,默认值为100max_queued_messages
监听基本设置为:
# 设置绑定IP,如只监听本地或者指定ip上的连接# bind_address localhostbind_address address# 当侦听器使用websockets协议时,也可以提供http数据。将http_dir设置为包含您要提供的文件的目录。如果未指定此选项,则无法进行正常的http连接。http_dir directory# 侦听指定端口上的传入网络连接。第二个可选参数允许将侦听器绑定到特定的ip地址/主机名。如果使用此变量并且既未使用全局bind_address也未使用端口选项,则不会启动默认侦听器。绑定地址/主机选项允许通过传递IP地址或主机名将此侦听器绑定到特定IP地址。对于websockets侦听器,只能在此处传递IP地址。listener port [bind address/host]# 设置当前监听器总的客户端连接数,设置为-1无限制,但是会收到系统的其他限制max_connections count# 指定监听端口,默认值为1883port port number# 设置监听器接收的协议,可以是mqtt、websockets,websockets默认在编译的时候就关闭了。protocol value...
基于SSL/TSL认证设置:
cafile file pathcapath directory pathcertfile file pathciphers cipher:listcrlfile file pathkeyfile file pathrequire_certificate [ true | false ]tls_version versionuse_identity_as_username [ true | false ]use_subject_as_username [ true | false ]...
桥接配置:
address address[:port] [address[:port]], addresses address[:port] [address[:port]]bridge_attempt_unsubscribe [ true | false ]bridge_protocol_version versioncleansession [ true | false ]connection namekeepalive_interval secondsidle_timeout secondslocal_clientid idlocal_password passwordlocal_username usernamenotifications [ true | false ]notifications_local_only [ true | false ]notification_topic topicremote_clientid idremote_password value......
SSL/TSL支持:
bridge_cafile file pathbridge_capath file pathbridge_certfile file path
3.2 mosquitto-passwd命令
该命令管理mosquitto的密码文件。用法:
mosquitto_passwd [ -c | -D ] passwordfile usernamemosquitto_passwd -b passwordfile username passwordmosquitto_passwd -U passwordfile-b 以批处理模式运行。这允许在命令行提供密码,这可以很方便,但应小心使用,因为密码将在命令行和命令历史记录中可见-c 创建一个新的密码文件,如果文件已经存在,则会覆盖。输入命令后,控制台会提示输入新建用户的密码,连续输入两次密码后,则密码文件创建完成-D 从密码文件中删除指定的用户-U 此选项可用于使用哈希密码将带有纯文本密码的密码文件升级/转换为一个密码文件
示例:
# 添加一个用户Ethanmosquitto_passwd -c /etc/mosquitto/passwd Ethan# 删除一个指定用户Ethanmosquitto_passwd -D /etc/mosquitto/passwd Ethan
- 注意用户名一定不要包含
:
(冒号)!;- 让用户名和密码生效,需要修改配置文件allow_anonymous和password_file选项。
# 指定密码文件 password_file /etc/mosquitto/passwd # 禁止匿名登陆 allow_anonymous false
3.3 mosquitto_sub和mosquitto_pub使用
step1: 启动mosquitto:
# 后台运行mosquitto -c /etc/mosquitto/mosquitto.conf -d# 前台运行$ mosquitto -c ./etc/mosquitto/mosquitto.conf1543906982: mosquitto version 1.5.4 starting1543906982: Config loaded from ./etc/mosquitto/mosquitto.conf.1543906982: Opening ipv4 listen socket on port 1883.1543906982: Opening ipv6 listen socket on port 1883.
step2: 订阅主题/dms/test
# ts 在接收到订阅主题消息时加上时间$ mosquitto_sub -v -t /dms/test | ts
step3: 发布主题消息
# -t 指定主题名字,-m指定消息内容$ mosquitto_pub -t /dms/test -m "Hello DMS"# -h 指定代理主机IP -u指定连接代理用户名,-P指定连接代理密码# 指定参数时可用双引号也可不用$ mosquitto_pub -h 10.5.12.232 -t "/dms/test" -m "Hello world" -u "Ethan" -P "pd111111"
step4: 订阅主题收到消息
$ mosquitto_sub -v -t /dms/test | tsDec 04 16:23:58 /dms/test Hello DMS
关于主题的命名和主题过滤这里就不展开了,详见引用链接。
4 Paho Android Service & Mosquitto
Paho Android Service配合Mosquitto就可以实现android客户端及MQTT消息代理服务器了。
5 引用
- 免费的MQTT私有云代理cloudmqtt
- MQTT 主题与主题过滤
- Python版客户端小教程
- Mosquitto Linux下安装、使用和测试
- mqtt简介
- mqtt协议代理服务器实现列表
- mqtt协议客户端实现列表
- 树莓派安裝 Mosquitto轻量级MQTT Broker 教程
- Mosquitto SSL Configuration -MQTT TLS Security
- Introduction to Security and TLS
- Enable Secure Communication with TLS and the Mosquitto Broker
更多相关文章
- Android HLS协议相关记录及部分解析
- android默认debug.keystore的密码
- Android动态显示隐藏密码输入框的内容
- Android使用MQTT协议
- UML详解:解析Android消息处理机制:Handler/Thread/Looper & Messag
- Android仿QQ消息列表ListView滑动删除效果
- Android EventBus 传递消息
- [Android] 监听系统网络连接打开或者关闭的消息
- Android 通过http协议数据交互