目录

微信硬件蓝牙返回H5和服务器端验证方法

2018年05月31日 11:35 | 4561次浏览 作者原创 版权保护

最近看了下邮箱,看到好多开发者还是有许多问题,尤其是微信硬件设备返回H5和返回服务器端的问题,其实本人认为这是微信给挖的一个大坑,对初开发者实在是有点棘手。请看下面会一一道来。

一、首先看流程图,还是拿前面章节的图来介绍

微信app和蓝牙设备接入通信,通信交互通道有两种,如下图通道路径结构图




黑色箭头 公众号html界面发送apdu命令到设备,设备最终返回数据到厂商服务器,厂商服务器也就是咱们开发者自己工程项目的服务器后端(数据返回到服务器配置url的 servelet的post方法中了)。这也是导致初学者开发者在开发h5界面发送指令后,无论如何在h5前端界面也得不到蓝牙设备返回数据包的原因,这是个坑,大家注意。


红色箭头 也就是我们接下来介绍的通道路径,公众号html界面发送apdu指令到设备,设备最终返回数据到发送html命令的界面,意思就是H5界面发送指令到设备,设备返回响应数据包到H5界面,而不是服务器端。

我相信,大部分开发者想走红色箭头的通道,可是在开发完代码测试的时候无论如何在H5界面接收不到硬件返回的数据,那么就需要开发者自己找原因了,寻找原因的办法如下:

验证硬件设备走的是黑色箭头通道:

1.1 打开微信公众号, 设备功能---》设置


配置URL,Token等,并且状态改为启用

URL的代码为一个普通的sevlet,代码如下:

//V型知识库www.vxzsk.com原创

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wepayweb.weixin.util.AppConst;
import com.wepayweb.weixin.util.SignUtil;

public class WeChatServletDevice extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1043526256516085038L;
	
	private static String token;

	/**
	 * Constructor of the object.
	 */
	public WeChatServletDevice() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

System.out.println("微信的get请求==================");
		
		// 微信加密签名
				String signature = request.getParameter("signature");
				System.out.println("微信加密签名-----------------------"+signature);
				// 时间戳
				String timestamp = request.getParameter("timestamp");
				//System.out.println("时间戳-----------------------"+timestamp);
				// 随机数
				String nonce = request.getParameter("nonce");
				//System.out.println("随机数-----------------------"+nonce);
				// 随机字符串
				String echostr = request.getParameter("echostr");
				System.out.println("随机字符串-----------------------"+echostr);
				//System.out.println("token-----------------------:"+token);
				
				PrintWriter out = response.getWriter();
				// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
				if (SignUtil.checkSignature(token, signature, timestamp, nonce)) {
					out.print(echostr);
					//System.out.println("这是:"+echostr);
				}
				out.close();
				out = null;
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		  
		// 将请求、响应的编码均设置为UTF-8(防止中文乱码)
				request.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				System.out.println("响应设备消息----start");
				// 响应消息
				PrintWriter out = response.getWriter();
				out.print("123");
				out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		token = "RqstuxxxxsgjfujYJKJ7659";
		// Put your code here
	}

}

在sevlet中的post方法中打印输出一句话“响应设备消息”,一旦设备有响应,并且走的是服务器端通道,在服务端就会输出上述一句话。

上述代码中的init()方法中的token需要替换。


二、解决硬件设备返回到H5而不是服务器端的方法

查阅微信硬件官方文档说明得知,微信的返回方式默认为返回到服务器端,所以好多硬件工程师在开发硬件固件的时候并没有修改返回方式,直接默认设置,一旦验证设备返回到服务器端,那么就要和硬件工程师沟通,让他们把返回方式设置成返回H5端(我记得在C中设置type类型为1就是返回H5端,时间久了有这个印象)。



小说《我是全球混乱的源头》
此文章本站原创,地址 https://www.vxzsk.com/1018.html   转载请注明出处!谢谢!

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程