关键参考文档:微信硬件平台
一、为设备添加产品
1、在认证过的服务号或订阅号下,在微信公众平台添加“设备功能”插件。公众测试号提供设备功能接口,供开发者测试使用。
2、在公众号平台的“测试号管理”下,选择“设备功能接口”并对其进行设置,进入“产品管理界面”并选择“添加产品”。
3、产品添加成功后,会为每一类型的产品提供一个产品编号,该产品编号在为设备授权时,以product_id字段为每个产品授权,默认分配100个测试产品。
重点参数:
每个产品唯一的二维码:微信硬件平台要求每一个注册设备都有唯一的设备编号,设备开发者可以调用微信 后台接口生成带设备编号的二维码,每个二维码需要与每台设备一一对应。用户购买产 品的同时拿到设备二维码,通过扫描设备二维码就可以在微信上直接绑定该设备
每种型号唯一的二维码:型号二维码是指一个二维码对应某一个类别/型号的产品,不包含某个具体设备的信息,因此在设备生产过程中,不需要二维码与设备进行一一对应。用户使用扫一扫,扫描品类二维码时,微信客户端会通过局域网发现技术搜索该类别/型号的设备,发现后再进行绑定。因此,使用型号二维码的设备必须支持微信AirKiss局域网发现(即AirKiss2.0)
微信配网:通过微信公众号使用AirKiss技术为硬件设备进行连网配置。
蓝牙配网:通过微信公众号使用AirSync技术为硬件设备进行连网配置。
二、获取设备二维码:
型号二维码(每种型号唯一的二维码):在添加产品时,若选择了产品二维码对应类型,在设备管理的产品详情,可以找到对应产品对应的二维码。
一机一码(每个产品唯一的二维码):每授权一个设备,产品对应授权配额将为减少减少一个。通过以下两种方式进行授权
方案一:第三方获取deviceid和设备二维码(由微信公众平台生成设备id,根据产品id和公众账号access_token随机授权一台设备,并生成deviceid),调用微信接口如下
private void getqrcode(String token, String productid) throws Exception{ String initUrl = "http://api.weixin.qq.com/device/getqrcode?access_token=" + token + "&product_id=" + productid; doGet(new URL(initUrl)); }
protected String doGet(URL url) throws Exception{ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); StringBuffer sb = new StringBuffer(); String line = ""; BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) { while((line = reader.readLine()) != null){ sb.append(line); } } reader.close(); connection.disconnect(); return sb.toString(); }返回结果格式如下:
{ "base_resp": { "errcode":0, "errmsg":"ok" }, "deviceid":"deviceid", "qrticket":"qrticket", "devicelicence":"devicelicence" }
其中,deviceid表示每个设备对应的标识,qrticket是该设备对应的二维码链接,通过草料二维码即可生成相应二维码,devicelicence为设备证书,可提供给设备作为凭据。之后需要为设备进行授权,方可在网页中获取设备信息,并为设备配网。
该授权过程同方案2,但post数据格式不同,不需要productid为参数,op_type设为1,post数据格式如下
{ "device_num":"1", "device_list":[ { "id":"deviceid", "mac":"1cda27c12d69", "connect_protocol":"4", "auth_key":"Abcdef1234566543abcdef1234566544", "close_strategy":"2", "conn_strategy":"1", "crypt_method":"0", "auth_ver":"0", "manu_mac_pos":"-1", "ser_mac_pos":"-2", "ble_simple_protocol": "0" } ], "op_type":"1" }方案二:第三方公众账号将deviceid及其属性提交到公众平台,并进行授权,之后再调用微信接口获取对应设备的获取二维码(需要硬件设备提供deviceid及其他属性信息),post调用微信接口如下,其中本文post参数从文件中读取。
授权过程(如果deviceid已经被授权过,则不能重新授权):
private void authorizeDevices(String token, String postFilePath ) throws Exception{ URL url = new URL("http://api.weixin.qq.com/device/authorize_device?access_token=" + token); doPost(url, postFilePath); }
protected void doPost(URL url, String postFilePath) throws Exception{ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setConnectTimeout(5*1000); connection.setReadTimeout(5*1000); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset","utf-8"); connection.setRequestProperty("Content-Disposition","utf-8"); String params = readfile(postFilePath); OutputStream os = connection.getOutputStream(); os.write(params.getBytes()); os.flush(); os.close(); if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK){ InputStream is = connection.getInputStream(); StringBuilder sb = new StringBuilder(); byte[] bytes = new byte[1024]; int i = 0; while ((i = is.read(bytes)) != -1){ sb.append(new String(bytes,0,i,"utf-8")); } is.close(); } os.close(); connection.disconnect(); }
private String readfile(String path) throws Exception{ FileReader fileReader = new FileReader(path); BufferedReader reader = new BufferedReader(fileReader); StringBuffer sBuffer = new StringBuffer(); String line = ""; while((line = reader.readLine()) != null){ sBuffer.append(line); } fileReader.close(); reader.close(); return sBuffer.toString(); }post文件格式如下,一次授权设备数量建议不超过5个。
{ "device_num":"1", "device_list":[ { "id":"test", "mac":"1cda27c12d69", "connect_protocol":"4", "auth_key":"Abcdef1234566543abcdef1234566544", "close_strategy":"2", "conn_strategy":"1", "crypt_method":"1", "auth_ver":"1", "manu_mac_pos":"-1", "ser_mac_pos":"-2", "ble_simple_protocol": "0" } ], "op_type":"0", "product_id": "40332" }微信服务器返回数据格式如下:
{ "resp": [{ "base_info": { "device_type":"gh_3ec41c424", "device_id":"test" }, "errcode":0,"errmsg":"ok" }] }其中device_type表示微信公众号的原始id,device_id表示硬件设备所提供的唯一标识。
根据device_id生成二维码过程(建议获取二维码链接一次不超过5个):
private void create_qrcode(String token, String postFilePath) throws Exception{ URL url = new URL("http://api.weixin.qq.com/device/create_qrcode?access_token=" + token); doPost(url, postFilePath); }发送post数据格式如下:
{ "device_num":"1", "device_id_list":[ "test" ] }返回对应的二维码格式如下:
{ "errcode":0, "errmsg":"ok", "device_num":1, "code_list": [{ "device_id":"test", "ticket":"http://we.qq.com/d/AQA0CYHUzuT6DRPIo9pmjSA9yq2SPqcfv6z5" }] }通过草料二维码即可生成相应的二维码图片。
三、公众号绑定设备
1、使用型号二维码绑定设备:
微信扫码型号二维码:将首先进入网络配置模式,设备需要具备微信配网(Airkiss)或蓝牙配网(AirSync)的功能。微信将通过局域网发现功能区蓝牙发现功能搜索相应设备,并为设备进行网络配置。
2、使用一机一码绑定设备:
扫码绑定即可。
3、微信中查看绑定的设备:
路径:"我"->"设置"->"设备"。
四、在网页中获取设备信息
关键信息:用户openid,参考”微信网页开发“第三部分,两种方式获取openid。
public String getBindDevicesByOpenId(String token, String open_id) throws Exception{ String url = "http://api.weixin.qq.com/device/get_bind_device?access_token=" + token + "&openid=" + open_id; String connect = doGet(new URL(url)); return connect; }获取数据格式如下:
{ "resp_msg": { "ret_code":0, "error_info":"ok" }, "openid":"oNAmB1KA5ykfs09m02N0AhcUk", "device_list": [ { "device_type":"gh_3154bc424", "device_id":"AA25799", "sub_device_list":[] }, { "device_type":"gh_3154bc424", "device_id":"test", "sub_device_list":[] } ] }
1、型号二维码:
使用微信扫一扫型号二维码,按流程即可成功为已进入网络配置模式的设备进行配置网络。
2、一机一码:
在网页中调用JsApi接口为设备配置网络。在jsp网页配置如下:
注意事项:1、注入js文件: