微信硬件蓝牙设备开发之设备解绑(13)

文章来源 http://www.vxzsk.com/158.html

微信绑定设备后,有时候用户需求的变化并不需要这个设备了,那么此时如何解绑设备呢,其实微信解绑设备有两种方式,一种方式就是在微信app本身内部解绑设备,另一种就是开发者调用微信解绑接口解除绑定。

第一种方式,微信app内部解绑

1,打开微信app,登录,然后切换到小人栏目,我们可以看到设置连接

2,点击设置连接,我们在切换的界面中可以看到设备按钮

3,点击设备按钮,我们可以看到自己的微信绑定了多少硬件设备

4,点击想要解绑的设备

在界面中,点击界面底部删除设备按钮,就能解绑设备啦。

第二种,调用微信公众平台解绑接口解绑设备

1)、咱们首先看看微信官方文档是如何说明关于解绑设备的。

针对不同类型的设备,可能会有不同的绑定/解绑策略,为了让第三方厂商灵活地根据自身产品定制绑定/解绑的策略,通过新的接口,微信把设备绑定/解绑处理的决定权交给第三方厂商。

目前绑定/解绑有2种体验:

1. 用户通过第三方H5直连第三方后台绑定/解绑设备:在第三方后台处理完成后,若绑定/解绑成功,调用API把绑定/解绑的结果推送给公众平台。

2. 第三方强制绑定/解绑:第三方调用API主要用于客服场景,帮助用户解决无法绑定/解绑的问题。

接口调用请求说明

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

POST数据说明

1
2
3
4
5
{
     "ticket" "TICKET" ,
     "device_id" "DEVICEID" ,
     "openid" " OPENID"
}

返回结果

成功的Json返回结果:

1
{base_resp:{ "errcode" : 0, "errmsg" : "ok" }}

失败的Json返回示例:

1
{base_resp:{ "errcode" : -1, "errmsg" : "system error" }}

2)、编写java示例代码

1,获取access_token

1
2
3
4
5
6
7
//获取token V型知识库 www.vxzsk.com
         String appid= "你公众号基本设置里的应用id" ; //应用ID
         String appSecret= "你公众号基本设置里的应用密钥" ; //(应用密钥)
         String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +appid+ "&secret=" +appSecret+ "" ;
         String backData=Test8.sendGet(url,  "utf-8" 10000 );
         JSONObject jsonObject = JSONObject.fromObject(backData);
         String access_token=jsonObject.getString( "access_token" ); //调用接口凭证

2,获取ticket方法

1
2
3
4
5
6
7
8
9
10
11
12
//V型知识库www.vxzsk.com 
public  final  static  String getticket_url= "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" ;//接口凭据 
public  static  String getJsapiTicket(String access_token){
        String jsonData=Test8.sendGet(getticket_url+access_token+ "&type=jsapi" "utf-8" 30000 );
        JSONObject jsonObj = JSONObject.fromObject(jsonData);
        String errcode = jsonObj.getString( "errcode" );
        String ticket =  null ;
        if (errcode.equals( "0" )){
            ticket = jsonObj.getString( "ticket" );
        }
        return  ticket;
    }

3,sendGet方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public  static  String sendGet(String url, String charset,  int  timeout)
       {
         String result =  "" ;
         try
         {
           URL u =  new  URL(url);
           try
           {
             URLConnection conn = u.openConnection();
             conn.connect();
             conn.setConnectTimeout(timeout);
             BufferedReader in =  new  BufferedReader( new  InputStreamReader(conn.getInputStream(), charset));
             String line= "" ;
             while  ((line = in.readLine()) !=  null )
             {
              
               result = result + line;
             }
             in.close();
           catch  (IOException e) {
             return  result;
           }
         }
         catch  (MalformedURLException e)
         {
           return  result;
         }
        
         return  result;
       }

4,sendPost方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//V型知识库 www.vxzsk.com
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;
      }
    

5,以上四步已经准备好了调用解绑接口的参数,我们还差两个参数device_id 和微信用户的openid,请用户自行替换,在这里不在累述,关于deviceid,请参考:http://www.vxzsk.com/87.html

解绑设备接口代码

1
2
3
4
5
6
7
8
//解绑设备
         String ticket=Test8.getJsapiTicket(access_token); //解绑操作凭证
         String device_id= "" ; //设备id
         String openid= "" ; //用户对应的openid
         String reurl = "https://api.weixin.qq.com/device/unbind?access_token=" +access_token;//解绑接口地址
         String params= "{ \"ticket\": \"" +ticket+ "\", \"device_id\": \"" +device_id+ "\", \"openid\": \"" +openid+ "\"}" ;
         String jsonData = Test8.sendPost(reurl, params);
         System.out.println( "解绑返回的数据:" +jsonData);

6,完整java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStreamWriter;
import  java.net.MalformedURLException;
import  java.net.URL;
import  java.net.URLConnection;
import  net.sf.json.JSONObject;
/**
  * *V型知识库 www.vxzsk.com
  */
public  class  Test8 {
     public  final  static  String getticket_url= "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" ;//接口凭据
     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;
      }
     
     /***
      * 模拟get请求
      * @param url
      * @param charset
      * @param timeout
      * @return
      */
      public  static  String sendGet(String url, String charset,  int  timeout)
       {
         String result =  "" ;
         try
         {
           URL u =  new  URL(url);
           try
           {
             URLConnection conn = u.openConnection();
             conn.connect();
             conn.setConnectTimeout(timeout);
             BufferedReader in =  new  BufferedReader( new  InputStreamReader(conn.getInputStream(), charset));
             String line= "" ;
             while  ((line = in.readLine()) !=  null )
             {
              
               result = result + line;
             }
             in.close();
           catch  (IOException e) {
             return  result;
           }
         }
         catch  (MalformedURLException e)
         {
           return  result;
         }
        
         return  result;
       }
      
      /***
     * 获取调用微信jsapi接口凭据
     * @param access_token
     * @return
     * linfanhe
     */
    public  static  String getJsapiTicket(String access_token){
        String jsonData=Test8.sendGet(getticket_url+access_token+ "&type=jsapi" "utf-8" 30000 );
        JSONObject jsonObj = JSONObject.fromObject(jsonData);
        String errcode = jsonObj.getString( "errcode" );
        String ticket =  null ;
        if (errcode.equals( "0" )){
            ticket = jsonObj.getString( "ticket" );
        }
        return  ticket;
    }
 
 
     /**
      * @param args
      */
     public  static  void  main(String[] args) {
         //获取token
         String appid= "你公众号基本设置里的应用id" ; //应用ID
         String appSecret= "你公众号基本设置里的应用密钥" ; //(应用密钥)
         String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +appid+ "&secret=" +appSecret+ "" ;
         String backData=Test8.sendGet(url,  "utf-8" 10000 );
         JSONObject jsonObject = JSONObject.fromObject(backData);
         String access_token=jsonObject.getString( "access_token" ); //调用接口凭证
         //解绑设备
         String ticket=Test8.getJsapiTicket(access_token); //解绑操作凭证
         String device_id= "" ; //设备id
         String openid= "" ; //用户对应的openid
         String reurl = "https://api.weixin.qq.com/device/unbind?access_token=" +access_token;//解绑接口地址
         String params= "{ \"ticket\": \"" +ticket+ "\", \"device_id\": \"" +device_id+ "\", \"openid\": \"" +openid+ "\"}" ;
         String jsonData = Test8.sendPost(reurl, params);
         System.out.println( "解绑返回的数据:" +jsonData);
 
     }
 
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值