BlackBerry的联网(HTTP/SOCKET)方式分为五种:
第一种: BES(Blackberry enterprise Server)
该方式是通过假设企业级Blackberry服务器,来负责客户端与服务器交互,通过使用BES的MDS service来进行网络连接,负责数据的请求与加密等服务。
所以blackberry规范中约定如果使用BES服务,那么url必须要被重写,例如
http://www.myserver.com 原生的地址要重写为 http://www.myserver.com;deviceside=false
第二种:BIS(Blackberry internet Service)
该服务是Blackberry自己的互联网服务,并开放给第三方合作伙伴。比如说中国移动与加拿大的RIM联合合作推出了电子邮件的推送服务,都是由BIS来承载的。
同样,如果您的企业要使用该服务的前提是必须要与RIM公司进行合作,在这里我们称为“入盟”。
使用BIS服务的网络url也需要被重写:
http://www.myservice.com;deviceside=false;connectionUID="本地uid";ConnectionType=mds-public
本地uid需要从servicebook读取,之后会讲解servicebook的原理和解析方式。
第三种:WAP2
如果要连接WAP2.0网关,你需要在连接时指定service record ,以及UID,后面会有代码解读。
网络连接的url被重写为 www.myservice.com;deviceside=true;ConnectionUID="UID"
第四种:WIFI
直接在url后面加入参数 interface=wifi
第五种:DIRECT TCP
需要在url后面跟参数 device=true
好了,说完了这五种连接方式,再说下servicebook,网上不少人叫它傻逼(SB),呵呵,原名“服务预订”,我没有很深刻的理解这个概念,总之我觉得就是联网选择的服务参数表,你选择什么样的网络服务,就需要在servicebook中选择合适的参数,比如说UID。
读取servicebook代码:
/** * 读servicebook */ public void readServiceBook() { ServiceBook sb = ServiceBook.getSB(); ServiceRecord[] records = sb.getRecords(); for (int i = 0; i < records.length; i++) { ServiceRecord myRecord = records[i]; String cid, uid; if (myRecord.isValid() && !myRecord.isDisabled()) { cid = myRecord.getCid().toLowerCase(); uid = myRecord.getUid().toLowerCase(); // BIS if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") != -1) { bisUid = myRecord.getUid(); } // WIFI if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") != -1) { wifiUid = myRecord.getUid(); } // Wap2.0 if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 && uid.indexOf("mms") == -1) { wap2Uid = myRecord.getUid(); } } } }
那么发一下我的代码,供大家参考:
/** * FileName: AccessPoint.java * All rights Reserved, Designed By 21winmess * Copyright: Copyright(C) 2011-2012 * Company 21winmess DaLian LTD. * @author: Gordon Bi * @version V2.0 * Createdate: 2012-8-3 下午4:33:09 * * Modification History: * Date Author Version Discription * ----------------------------------------------------------------------------------- * 2012-8-3 Qute for BlackBerry * Why & What is modified: <修改原因描述> */ package org.blz.net; import org.blz.rms.RmsFacade; import org.blz.rms.RmsSeed; import net.rim.device.api.servicebook.ServiceBook; import net.rim.device.api.servicebook.ServiceRecord; import net.rim.device.api.system.CoverageInfo; import net.rim.device.api.system.WLANInfo; /** * @ClassName: AccessPoint * @Description:接入点能力 * @author: GordonBi * @date: 2012-8-3 下午4:33:09 * */ public class APN { public static int WIFI = 1; public static int BIS = 2; public static int WAP = 3; public static int DIRECT_TCP = 4; public static int AUTO_SELECT = 5; public static int currentAPN = -1;// 当前接入点方式 public static boolean isManualSelectAPN = false;// 是否手动选择APN 默认 public String bisUid = null; public String wifiUid = null; public String wap2Uid = null; public String currentSocketUrl = ""; /** * 根据当前APN,重写socket url地址 * @param url * @return */ public String rewriteSocketUrl(String url) { switch (currentAPN) { case 1: currentSocketUrl = getWIFIURL(url); break; case 2: currentSocketUrl = getBISURL(url, bisUid); break; case 3: currentSocketUrl = getWAP2URL(url, wap2Uid); break; case 4: currentSocketUrl = getDirectTCP(url); break; } return currentSocketUrl; } /** * 手动选择APN联网方式 * * @param conncetionMethod * @return */ public void manualSelectAPN(int conncetionMethod) { switch (conncetionMethod) { case 1: currentAPN = WIFI; break; case 2: currentAPN = BIS; break; case 3: currentAPN = WAP; break; case 4: currentAPN = DIRECT_TCP; break; } RmsFacade.setBoolean(RmsSeed.IS_MANUAL_SELECT_APN, true); RmsFacade.setInt(RmsSeed.APN_SETTING, currentAPN);// 设置当前接入点 isManualSelectAPN = true; } /** * 自动检测APN,选择最优联网方式 * * @return */ public void autoSelectAPN() { if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) { currentAPN = DIRECT_TCP; } if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) { currentAPN = BIS; } if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) { if (wap2Uid != null && !"".equals(wap2Uid)) { currentAPN = WAP; } } if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { currentAPN = WIFI; } RmsFacade.setInt(RmsSeed.APN_SETTING, currentAPN);// 设置当前接入点 RmsFacade.setBoolean(RmsSeed.IS_MANUAL_SELECT_APN, false); isManualSelectAPN = false; } /** * 读servicebook */ public void readServiceBook() { ServiceBook sb = ServiceBook.getSB(); ServiceRecord[] records = sb.getRecords(); for (int i = 0; i < records.length; i++) { ServiceRecord myRecord = records[i]; String cid, uid; if (myRecord.isValid() && !myRecord.isDisabled()) { cid = myRecord.getCid().toLowerCase(); uid = myRecord.getUid().toLowerCase(); // BIS if (cid.indexOf("ippp") != -1 && uid.indexOf("gpmds") != -1) { bisUid = myRecord.getUid(); } // WIFI if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") != -1) { wifiUid = myRecord.getUid(); } // Wap2.0 if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1 && uid.indexOf("mms") == -1) { wap2Uid = myRecord.getUid(); } } } } // ================build url by different connection // method=================// public static String getWIFIURL(String url) { if (url != null && !"".equals(url)) { return url + ";interface=wifi"; } else { return ""; } } public static String getBISURL(String url, final String bisUid) { if (url != null && !"".equals(url) && bisUid != null && !"".equals(bisUid)) { return url + ";deviceside=false;connectionUID=" + bisUid + ";ConnectionType=mds-public"; } else { return ""; } } public static String getWAP2URL(String url, final String wap2Uid) { if (url != null && !"".equals(url) && wap2Uid != null && !"".equals(wap2Uid)) { return url + ";deviceside=true" + ";ConnectionUID=" + wap2Uid; } else { return ""; } } public static String getDirectTCP(String url) { if (url != null && !"".equals(url)) { return url + ";deviceside=true"; } else { return ""; } } // ===============================================================// // ======================单例模式===========================// private static APN mSingleInstance = null; private static Object mSyncObject = new Object(); public static APN getSingleInstance() { synchronized (mSyncObject) { if (mSingleInstance == null) mSingleInstance = new APN(); } return mSingleInstance; } private APN() { readServiceBook(); // autoSelectAPN(); } public static String getAPNAlice(int apnIndex){ String apnAlice = ""; switch (apnIndex) { case 1: apnAlice = "WIFI"; break; case 2: apnAlice = "BIS-B"; break; case 3: apnAlice = "WAP"; break; case 4: apnAlice = "DRTCP"; break; } return apnAlice; } }