微信蓝牙设备开发教程之设备授权(3)

@V型知识库 原创  已推出最新微信硬件jsapi学习笔记 地址 http://www.vxzsk.com/282.html

设备授权:新接口,厂商不需要提供deviceid

为了简化第三方厂商的生产流程,降低接入微信的门槛,公众平台提供了新的设备授权接口。新接口中,deviceid由微信生成,作为设备在微信平台的唯一标识,第三方不需要在固件中烧入deviceid。deviceid和设备二维码在上一节已经讲过。

设备授权流程如下:


设备功能服务器设置:


这是微信硬件设备服务器设置,如果硬件设备类型返回到H5界面,可关闭此项功能。

连接硬件后交互数据的返回有两种,一种就是硬件通过蓝牙返回到服务器端,也就是上图中的URL地址上,另一种就是直接返回到操作的H5界面上,如果用户操作H5界面硬件无响应数据,那么极有可能微信通过硬件设备把数据返回到我们配置的URL上了。可要求硬件设备厂商修改硬件芯片程序,把程序改成返回H5界面类型。

java授权代码:

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);
    }
 
}

用户把上述代码复制到自己工程里,执行main方法即可。

在main方法中读者要填充几个参数,其中67行的access_token参数可在http://www.vxzsk.com/28.html地址参考。

mac地址询问硬件设备提供商。

返回结果

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

1
2
3
4
5
6
7
8
9
10
11
{ "resp" :[
     {
     "base_info" :
     {
     "device_type" : "your_devcie_type" ,
     "device_id" : "id"
     },
     "errcode" : 0 ,
     "errmsg" : "ok"
     }
]}

错误的Json返回示例:{"errcode":42001,"errmsg":""}

具体params中各个参数含义,请读者自行查阅微信官方文档。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值