文档开发者中心
我的

基于HarmonyOS控制Hi3861小车之信息通信

image.png

引言 

      在鸿蒙应用实际开发中,时不时的会遇到App与IOT设备间的通信,本节主要详细讲述一下通信关键技术,两者间通过UDP进行通信,这对于每一个HarmonyOS开发者也是需要了解的重点知识。

核心类

DatagramSocket、DatagramPacket、EventHandler

功能介绍

      通过App控制小车运动(前进、后退、左转、右转、停止),它们间控制命令以json格式发送,如:{

"mode": "CarControl",//控制命令分类

"cmd": "forward"//具体命令

}。

开发指南

1、创建UDP协议的发送命令对象

    private UdpManager() {

try {

mGpsDatagramSocket = new DatagramSocket();

} catch (SocketException e) {

e.printStackTrace();

}

}

2、将要发送的数据封装成DatagramPacket对象发送:

DatagramPacket sRequest = new DatagramPacket(mInfoArray, mInfoArray.length,

InetAddress.getByName(getIp()), PORT);

// 开始发送

mGpsDatagramSocket.send(sRequest);

  1. 构造发送的命令:

public void sendMessage(String info) {

Gson gson = new Gson();

WifiCommand messageInfo = new WifiCommand();

messageInfo.setCmd(info);

//控制类型

messageInfo.setMode();

//转换成json

String resultJson = gson.toJson(messageInfo);

// 创建发送命令SendMessageRunnable对象

mSendMessageRunnable = new SendMessageRunnable();

mSendMessageRunnable.setInfoArray(resultJson.getBytes(StandardCharsets.UTF_8));

// 启动发送命令线程

mEventHandler.postTask(mSendMessageRunnable);

if ("stop".equals(info) || "tripod_on".equals(info) || "tripod_off".equals(info)){

HiLog.info(label, "info = " + info);

} else {

// 启动发送Gps请求线程和接收信息线程

startReceive();

startSendGpsMessage();

}

HiLog.info(label, "sendMessage = " + resultJson);

}

附上主要源代码

  1. MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{

private Button iTurnUp,iTurnDown,iTurnLeft,iTurnRight,iTurnRun;

private UdpManager udpManager;

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

initComponent();

// 初始化WiFi控制对象

udpManager = UdpManager.getInstance(this);

}

private void initComponent(){

iTurnUp = (Button) findComponentById(ResourceTable.Id_i_up);

iTurnUp.setClickedListener(this);

iTurnDown = (Button) findComponentById(ResourceTable.Id_i_down);

iTurnDown.setClickedListener(this);

iTurnLeft = (Button) findComponentById(ResourceTable.Id_i_left);

iTurnLeft.setClickedListener(this);

iTurnRight = (Button) findComponentById(ResourceTable.Id_i_right);

iTurnRight.setClickedListener(this);

iTurnRun = (Button) findComponentById(ResourceTable.Id_i_run);

iTurnRun.setClickedListener(this);

}

@Override

public void onActive() {

super.onActive();

}

@Override

public void onForeground(Intent intent) {

super.onForeground(intent);

}

@Override

public void onClick(Component component) {

switch (component.getId()) {

//前进

case ResourceTable.Id_i_up:

udpManager.sendMessage("forward");

break;

//后退

case ResourceTable.Id_i_down:

udpManager.sendMessage("backward");

break;

//左转

case ResourceTable.Id_i_left:

udpManager.sendMessage("left");

break;

//右转

case ResourceTable.Id_i_right:

udpManager.sendMessage("right");

break;

//开始

case ResourceTable.Id_i_run:

udpManager.sendMessage("stop");

break;

}

}

}

  1. UdpManager

/**

* UDP连接类

*/

public class UdpManager {

private static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00134, "UdpManager");

private static final int PORT = 48100;

private static final int GET_MESSAGE = 1;

private static UdpManager sUdpManager;

private static Context sContext;

private UdpReceiveCallback mReceiveInformationCallback;

private ReceiveMessageRunnable mReceiveMessageRunnable;

private SendGpsMessageRunnable mSendGpsMessageRunnable;

private SendMessageRunnable mSendMessageRunnable;

private DatagramSocket mGpsDatagramSocket;

private static String ip = "192.168.0.1";

/**

* 控制是否还需要接收信息控制器

*/

private boolean flag = false;

private final EventHandler mEventHandler = new EventHandler(EventRunner.create()) {

@Override

protected void processEvent(InnerEvent event) {

super.processEvent(event);

if (event.eventId == GET_MESSAGE) {

if (mReceiveInformationCallback != null) {

mReceiveInformationCallback.getMessage(event.object);

}

}

}

};

private final EventHandler mReceiveEventHandler = new EventHandler(EventRunner.create()) {

};

private final EventHandler mSendGpsEventHandler = new EventHandler(EventRunner.create()) {

};

/**

* UdpManager的单例

*

* @return UdpManager单例对象

*/

public static UdpManager getInstance(Context context) {

if (sUdpManager == null) {

sUdpManager = new UdpManager();

sContext = context;

}

return sUdpManager;

}

/**

* 构造函数

*/

private UdpManager() {

// 创建UDP协议的发送命令对象

try {

mGpsDatagramSocket = new DatagramSocket();

} catch (SocketException e) {

e.printStackTrace();

}

}

/**

* 注册接收信息的回调函数

*

* @param callback 接收信息回调函数

*/

public void registerCallback(UdpReceiveCallback callback) {

mReceiveInformationCallback = callback;

}

/**

* 对外提供的发送命令方法

*

* @param info 需要发送的命令

*/

public void sendMessage(String info) {

Gson gson = new Gson();

UdpCommand messageInfo = new UdpCommand();

// 传进来的控制命令

messageInfo.setCmd(info);

//控制类型

messageInfo.setMode();

//转换成json

String resultJson = gson.toJson(messageInfo);

// 创建发送命令SendMessageRunnable对象

mSendMessageRunnable = new SendMessageRunnable();

mSendMessageRunnable.setInfoArray(resultJson.getBytes(StandardCharsets.UTF_8));

// 启动发送命令线程

mEventHandler.postTask(mSendMessageRunnable);

// 启动发送Gps请求线程和接收信息线程

if ("stop".equals(info)) {

HiLog.info(label, "info = " + info);

} else {

// 启动发送Gps请求线程和接收信息线程

startReceive();

startSendGpsMessage();

}

HiLog.info(label, "sendMessage = " + resultJson);

}

public String getIp() {

return ip;

}

public void setIp(String mIp) {

this.ip = mIp;

}

/**

* 内部类,用作发送命令

*/

private class SendMessageRunnable implements Runnable {

private byte[] mInfoArray;

void setInfoArray(byte[] infoArray) {

mInfoArray = infoArray;

}

@Override

public void run() {

HiLog.info(label, "发送线程 = " + Thread.currentThread().getName());

// 发送数据

try {

// 延时发送50毫秒,因为如果不延时会将小车卡死

Thread.sleep(50);

// 将要发送的数据封装成DatagramPacket对象

DatagramPacket sRequest = new DatagramPacket(mInfoArray, mInfoArray.length,

InetAddress.getByName(getIp()), PORT);

// 开始发送

mGpsDatagramSocket.send(sRequest);

} catch (IOException | InterruptedException e) {

e.printStackTrace();

HiLog.info(label, "sendMessage error");

}

}

}

/**

* 内部类,用作接收命令

*/

private class ReceiveMessageRunnable implements Runnable {

@Override

public void run() {

try {

while (flag) {

byte[] buf = new byte[1024];

DatagramPacket receiveDatagramPacket = new DatagramPacket(buf, buf.length);

if (mGpsDatagramSocket != null && !mGpsDatagramSocket.isClosed()) {

HiLog.info(label, "接收线程开始阻塞" + Thread.currentThread().getName());

// 接收返回数据,会阻塞线程

mGpsDatagramSocket.receive(receiveDatagramPacket);

// 将得到的数据转成json

String json = new String(receiveDatagramPacket.getData(), StandardCharsets.UTF_8);

json = json.substring(json.indexOf("{"), json.lastIndexOf("}")+1);

HiLog.info(label, "receiveMessage json = " + json);

// 将对象发送给需要接收返回值的地方

mEventHandler.sendEvent(InnerEvent.get(GET_MESSAGE, json));

}

}

} catch (IOException e) {

e.printStackTrace();

HiLog.error(label, "receiveMessage error");

}

}

}

/**

* 内部类,用作发送请求Gps命令

*/

private class SendGpsMessageRunnable implements Runnable {

@Override

public void run() {

Gson gson = new Gson();

UdpCommand messageInfo = new UdpCommand();

// 传进来的控制命令

messageInfo.setCmd("getinfo");

//控制类型

messageInfo.setMode();

//转换成json

String resultJson = gson.toJson(messageInfo);

byte[] infoArray = resultJson.getBytes(StandardCharsets.UTF_8);

try {

// 将要发送的数据封装成DatagramPacket对象

DatagramPacket sRequest = new DatagramPacket(infoArray, infoArray.length,

InetAddress.getByName(getIp()), PORT);

// 开始发送

mGpsDatagramSocket.send(sRequest);

// 启动获取Gps命令线程

mSendGpsEventHandler.postTask(mSendGpsMessageRunnable, 2000);

HiLog.info(label, "发送gps");

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 启动接收消息

*/

private void startReceive() {

if (!flag) {

flag = true;

// 创建接收命令ReceiveMessageRunnable对象

mReceiveMessageRunnable = new ReceiveMessageRunnable();

// 启动接收命令线程

mReceiveEventHandler.postTask(mReceiveMessageRunnable);

HiLog.info(label, "开启接收线程");

}

}

/**

* 开始获取gps点

*/

private void startSendGpsMessage() {

// 创建发送Gps命令SendGpsMessageRunnable对象

if (mSendGpsMessageRunnable == null) {

mSendGpsMessageRunnable = new SendGpsMessageRunnable();

}

// 启动获取Gps命令线程

mSendGpsEventHandler.postTask(mSendGpsMessageRunnable);

HiLog.info(label, "开启发送gps请求线程");

}

/**

* 停止接收消息和停止获取gps点

*/

public void stopReceive() {

mSendGpsEventHandler.removeTask(mSendGpsMessageRunnable);

mEventHandler.removeTask(mSendMessageRunnable);

HiLog.info(label, "停止接收线程和发送gps请求线程");

}

public void onStop() {

if (mGpsDatagramSocket != null) {

mGpsDatagramSocket.close();

}

if (mReceiveMessageRunnable != null) {

flag = false;

mEventHandler.removeTask(mReceiveMessageRunnable);

}

}

}

  1. UdpCommand

class UdpCommand {

// 控制命令:forward,back,left,right

private String cmd;

// 控制类型

private String mode;

public String getCmd() {

return cmd;

}

void setCmd(String cmd) {

this.cmd = cmd;

}

public String getMode() {

return mode;

}

void setMode() {

this.mode = "CarControl";

}

}

  1. UdpReceiveCallback

/**

* 接收小车返回数据的回调函数

*/

public interface UdpReceiveCallback {

void getMessage(Object value);

}

  1. xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<DirectionalLayout

xmlns:ohos="http://schemas.huawei.com/res/ohos"

ohos:height="match_parent"

ohos:width="match_parent"

ohos:alignment="center"

ohos:orientation="vertical">

<DirectionalLayout

ohos:height="70vp"

ohos:width="match_parent"

ohos:orientation="horizontal"

ohos:layout_alignment="center"

ohos:top_margin="10vp" >

<Button

ohos:id="$+id:i_up"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="60vp"

ohos:text_size="25fp"

ohos:text="前进"/>

<Button

ohos:id="$+id:i_down"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="13vp"

ohos:text_size="25fp"

ohos:text="后退"/>

</DirectionalLayout>

<DirectionalLayout

ohos:height="70vp"

ohos:width="match_parent"

ohos:orientation="horizontal"

ohos:layout_alignment="center"

ohos:top_margin="10vp" >

<Button

ohos:id="$+id:i_run"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="60vp"

ohos:text_size="25fp"

ohos:text="开始"/>

<Button

ohos:id="$+id:i_stop"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="13vp"

ohos:text_size="25fp"

ohos:text="停止"/>

</DirectionalLayout>

<DirectionalLayout

ohos:height="70vp"

ohos:width="match_parent"

ohos:orientation="horizontal"

ohos:top_margin="10vp" >

<Button

ohos:id="$+id:i_left"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="60vp"

ohos:text_size="25fp"

ohos:text="左转"/>

<Button

ohos:id="$+id:i_right"

ohos:height="50vp"

ohos:width="120vp"

ohos:background_element="#FF9F9F9F"

ohos:left_margin="13vp"

ohos:text_size="25fp"

ohos:text="右转"/>

</DirectionalLayout>

</DirectionalLayout>

点赞
2
收藏
2
回复
1
分享
举报
浏览1475 发布于2021-10-27 02:16未知归属地
全部评论
最多点赞
最新发布
最早发布

厉害了,我小车都没跑起来。

不过通信可以用分布式能力的吧,用Socket UPD是不是有点倒退了?

1楼回复于2021-10-27 03:15 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要发帖子

了解社区公约,与您携手共创和谐专业的开发者社区。