目录

微信硬件设备授权

2017年11月05日 08:50 | 13704次浏览 作者原创 版权保护

我们在上一篇 介绍了 微信硬件获取设备ID和二维码,之所以上一篇强调介绍不要急于扫描二维码绑定设备,是因为设备并未授权,只有授权成功的设备才可以绑定并成功连接

一、设备授权接口

http://iot.weixin.qq.com/wiki/new/index.html 微信硬件平台地址,然后如下图所示,点击文档的左侧的设备授权新接口,是的你没看错,咱们用设备授权新接口, 另外还有个设备授权接口,此接口本作者没用过,既然官方文档强调新接口,那么就用新的。

进入设备授权新接口说明,哈哈,上一节还在纠结获取设备deviceId和设备二维码的接口在哪里,没想到再设备授权新接口说明文档里面,既然上一节已经说了,在这里不在累述,咱们直接看设备授权新接口说明。


利用deviceid更新设备属性

第三方公众账号将device id及其属性信息提交公众平台进行授权。

接口调用请求说明

http请求方式: POST
https://api.weixin.qq.com/device/authorize_device?access_token=ACCESS_TOKEN

POST数据说明

POST数据示例如下:
{
    "device_num":"1",
    "device_list":[
    {
    "id":"dev1",
    "mac":"123456789ABC",
    "connect_protocol":"3",
    "auth_key":"",
    "close_strategy":"1",
    "conn_strategy":"1",
    "crypt_method":"0",
    "auth_ver":"1",
    "manu_mac_pos":"-1",
    "ser_mac_pos":"-2",
    "ble_simple_protocol": "0"
}
],
"op_type":"1"
}

、java设备授权示例代码

package com.wepayweb.weixin.util.device;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/****
 * 
 * @author www.vxzsk.com V型知识库
 *
 */
public class DeviceAuthTest {
    /**
     * V型知识库
     * 模拟post方法
     */
     public static String sendPost(String requrl,String param){
         URL url;
          String sTotalString="";  
        try {
            url = new URL(requrl);
             URLConnection connection = url.openConnection(); 
              
             connection.setRequestProperty("accept", "*/*");
             connection.setRequestProperty("connection", "Keep-Alive");
             connection.setRequestProperty("Content-Type", "text/xml");
            // connection.setRequestProperty("Content-Length", body.getBytes().length+"");
             connection.setRequestProperty("User-Agent",
                     "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
              
              
                connection.setDoOutput(true);  
                OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");  
                out.write(param); // 向页面传递数据。post的关键所在!  
                out.flush();  
                out.close();  
                // 一旦发送成功,用以下方法就可以得到服务器的回应:  
                String sCurrentLine;  
               
                sCurrentLine = "";  
                sTotalString = "";  
                InputStream l_urlStream;  
                l_urlStream = connection.getInputStream();  
                // 传说中的三层包装阿!  
                BufferedReader l_reader = new BufferedReader(new InputStreamReader(  
                        l_urlStream));  
                while ((sCurrentLine = l_reader.readLine()) != null) {  
                    sTotalString += sCurrentLine + "\r\n";  
           
                }  
                 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
            
            System.out.println(sTotalString);  
            return sTotalString;
     }
 
    public static void main(String[] args) {
        String access_token="";//更换为自己的access_token
        String deviceId="";//调用生成二维码接口返回的设备id
        String mac="";//设备mac地址,可询问提供硬件设备的厂商人员
        String params="{\"device_num\":\"1\",\"device_list\":[{"
                   +"\"id\":\""+deviceId+"\","
                   +"\"mac\":\""+mac+"\","
                    +"\"connect_protocol\":\"3\","
                    +"\"auth_key\":\"\","
                    +"\"close_strategy\":\"1\","
                    +"\"conn_strategy\":\"1\","
                    +"\"crypt_method\":\"0\","
                    +"\"auth_ver\":\"0\","
                    +"\"manu_mac_pos\":\"-1\","
                    +"\"ser_mac_pos\":\"-2\","
                    +"\"ble_simple_protocol\": \"0\""
                    + "}],"
                    +"\"op_type\":\"1\""
                   + "}";
                   
              String s=DeviceAuthTest.sendPost("https://api.weixin.qq.com/device/authorize_device?access_token="+access_token, params);
                  System.out.println("返回:"+s);
    }
 
}

deviceId设备id的获取请参考https://www.vxzsk.com/288.html

main方法中的mac地址格式为xxyyaabb1234,中间是没有“:”的。

connect_protocol设置为3,也就是ble蓝牙设备

auth_key 通信加密,在这里设置为空,意思就是不用此加密方式

close_strategy 断开策略,目前支持: 1:退出公众号页面时即断开连接 2:退出公众号之后保持连接不断开

crypt_method auth加密方法,目前支持两种取值: 0:不加密 1:AES加密(CBC模式,PKCS7填充方式)

auth_ver auth version,设备和微信进行auth时,会根据该版本号来确认auth buf和auth key的格式(各version对应的auth buf及key的具体格式可以参看“客户端蓝牙外设协议”),该字段目前支持取值: 0:不加密的version 1:version 1  我们在这里设置0  不加密

返回结果

成功:json方式返回授权成功的设备

{"resp":[
    {
    "base_info":
    {
    "device_type":"your_devcie_type",
    "device_id":"id"
    },
    "errcode":0,
    "errmsg":"ok"
    }
]}

、授权成功后,扫描二维码绑定设备效果如下(前提是打开手机蓝牙)

型号二维码扫描后的效果

点击下一步,会扫描附近的蓝牙设备,并显示设备的名称


唯一二维码(一机一码)扫描后的效果

点击绑定设备 然后进入公众账号,进入公众账号之后,我们在微信的头部会看到"未连接设备"或"已连接1个设备"

如果没有出现已连接1个设备或者未连接设备 可能是您授权的二维码和设备mac地址不匹配,还有一种情况就是手机蓝牙不支持ble模式,在绑定设备后,记得打开设备。



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

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