微信采集beacon数据

服务端实现代码:

/**
 * Created by lim on 2016/11/25.
 */
public class wxBeaconsController {
    private String strAppId = "123";
    private String strAppSecret = "123";

    /**
     * 发送post请求
     * @param strURL
     * @param params
     * @return
     */
    public String post(String strURL, String params) {
        try {
            URL url = new URL(strURL);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("POST"); // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            OutputStreamWriter out = new OutputStreamWriter(
                    connection.getOutputStream(), "UTF-8"); // utf-8编码
            out.append(params);
            out.flush();
            out.close();
            // 读取响应
            int length = (int) connection.getContentLength();// 获取长度
            InputStream is = connection.getInputStream();
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                String result = new String(data, "UTF-8"); // utf-8编码
                System.out.println(result);
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error"; // 自定义错误信息
    }


    /**
     * 采集Config设置
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "wxconfig.do", method = RequestMethod.POST)
    @ResponseBody
    public Map getWXConfigPara(HttpServletRequest request, HttpSession session) {
        Map jsonMap = new HashMap();
        String strUrl = StringUtils.trim(request.getParameter("url"));
        long lTime = 0;
        String strGetData = null;
        //时间戳
        if(session.getAttribute("timestamp") != null){
            lTime = (long)session.getAttribute("timestamp");
        }else {
            lTime = (new Date()).getTime();
        }

        //应用编号
        String sAppId = null;
        if(session.getAttribute("appId") != null){
            sAppId = (String)session.getAttribute("appId");
        }else {
            sAppId = strAppId;
        }

        String sNonceStr = null;
        if(session.getAttribute("nonceStr") != null){
            sNonceStr = (String)session.getAttribute("nonceStr");
        }else {
            UUID uuid = UUID.randomUUID();
            sNonceStr = uuid.toString();
        }

        String sSignature = null;
        if(session.getAttribute("signature") != null){
            sSignature = (String)session.getAttribute("signature");
        }

        String sAccToken = null;
        if(session.getAttribute("access_token") != null){
            sAccToken = (String)session.getAttribute("access_token");
        }

        long lCur = (new Date()).getTime();
        if((lCur -lTime)< 7199 && sAppId !=null && sNonceStr != null && sSignature != null){
            jsonMap.put("appId", sAppId);
            jsonMap.put("nonceStr", sNonceStr);
            jsonMap.put("timestamp", lTime);
            jsonMap.put("signature", sSignature);
        }else{
            jsonMap.put("appId", sAppId);
            jsonMap.put("nonceStr", sNonceStr);
            jsonMap.put("timestamp", lCur);
            String sUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + strAppId + "&secret=" + strAppSecret;
            //获取accToken
            HttpClientUtils httpClientUtils = new HttpClientUtils();
            strGetData = httpClientUtils.sendGetData(sUrl);
            JSONObject jsWXToken = new JSONObject(strGetData);
            if (strGetData.indexOf("access_token") > -1) {
                sAccToken = (String) jsWXToken.get("access_token");
                sUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + sAccToken + "&type=jsapi";
                strGetData = httpClientUtils.sendGetData(sUrl);
                JSONObject jsWXTicket = new JSONObject(strGetData);
                if (strGetData.indexOf("ticket") > -1) {
                    String strTicket = (String) jsWXTicket.get("ticket");
                    if (!strTicket.equals("")) {
                        String strSignSource = "jsapi_ticket=" + strTicket + "&noncestr=" + sNonceStr
                                + "×tamp=" + String.valueOf(lCur) + "&url=" + strUrl;
                        sSignature = new SHA1().getDigestOfString(strSignSource.getBytes()).toLowerCase();
                        jsonMap.put("signature", sSignature);
                    }
                }
            }

        }

        List listDevice = new ArrayList<>();
        DeviceListBean deviceListBean = new DeviceListBean();
        deviceListBean.setMajor(10091);
        deviceListBean.setMinor(49419);
        deviceListBean.setUuid("FDA50693-A4E2-4FB1-AFCF-C6EB07647825");
        listDevice.add(deviceListBean);

        if(sAccToken != null && sAppId != null && sNonceStr != null && sSignature != null){
            session.setAttribute("appId", sAppId);
            session.setAttribute("nonceStr", sNonceStr);
            session.setAttribute("timestamp", lTime);
            session.setAttribute("signature", sSignature);
            session.setAttribute("access_token", sAccToken);
            //查询并删除所有分组
            String sUrl = "https://api.weixin.qq.com/shakearound/device/group/getlist?access_token=" + sAccToken;
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("begin", 0);
            jsonObj.put("count", 100);
            strGetData = post(sUrl, jsonObj.toString());
            DelGBean delGBean =JsonUtils.json2Obj(strGetData, DelGBean.class);
            DelGroupBean delGroupBean = delGBean.getData();
            if(delGroupBean.getTotal_count() > 0){
                for (WXGroupBean wxGroupBean: delGroupBean.getGroups()) {
                    JSONObject jsonParam = new JSONObject();
                    //移除设备
                    sUrl = "https://api.weixin.qq.com/shakearound/device/group/deletedevice?access_token=" + sAccToken;
                    jsonParam.put("group_id", wxGroupBean.getGroup_id());
                    jsonParam.put("device_identifiers", listDevice);
                    strGetData = post(sUrl, jsonParam.toString());
                    //删除分组
                    jsonParam.remove("device_identifiers");
                    sUrl = "https://api.weixin.qq.com/shakearound/device/group/delete?access_token=" + sAccToken;
                    strGetData = post(sUrl, jsonParam.toString());
                }
                //新增分组
                sUrl = "https://api.weixin.qq.com/shakearound/device/group/add?access_token=" + sAccToken;
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("group_name", "test");
                strGetData = post(sUrl, jsonObject.toString());
                GroupBean wxGroupBean = JsonUtils.json2Obj(strGetData, GroupBean.class);
                if(strGetData.indexOf("group_id") > -1){
                    if(wxGroupBean != null){
                        //添加设备
                        JSONObject jsonGroup = new JSONObject();
                        jsonGroup.put("group_id", wxGroupBean.getData().getGroup_id());
                        jsonGroup.put("device_identifiers", listDevice);
                        sUrl = "https://api.weixin.qq.com/shakearound/device/group/adddevice?access_token="  + sAccToken;
                        strGetData = post(sUrl, jsonGroup.toString());

                        jsonGroup.remove("device_identifiers");
                        jsonGroup.put("begin", 0);
                        jsonGroup.put("count", 100);
                        sUrl = "https://api.weixin.qq.com/shakearound/device/group/getdetail?access_token=" + sAccToken;
                        strGetData = post(sUrl, jsonGroup.toString());
                        System.out.println("-----");
                        System.out.println(strGetData);
                        System.out.println("-----");


                    }
                }

            }
        }
        return jsonMap;
    }
}

前端HTML5页面实现代码




	
	     
	    
	 
	Insert title here
	
	
	


	
	
	
	
	

ps

wx.onSearchBeacons 无响应可能原因

      1、iBeacon 未工作

      2、iBeacon 在微信端未激活

      3、设备未分组

      4、代码错误

      5、生成签名时的url和实际的url不一致

转载于:https://my.oschina.net/u/2337820/blog/796917

你可能感兴趣的:(微信采集beacon数据)