Link Turbo(全网络聚合技术)通过手机端侧网络技术革新,为用户带来聚合高网速、稳定低时延的移动上网体验。当前,Link Turbo提供LocalSocket方案,应用可在不集成任何SDK的前提下,接入网络加速能力。

典型应用场景:

您将会建立什么

通过本篇Codelab,您将创建Demo Project,并将会使用到Link Turbo提供的API。

您将会学到什么

硬件要求

软件要求

需要的知识点

进入http://developer.huawei.com/consumer/cn页面,登录华为开发者联盟,账号必须实名认证,详见登录注册。

基于安全考虑,服务侧对开放的接口做了权限保护处理,使用相关接口需要向华为方申请对应权限。权限申请需提供开发者联盟网站创建应用时生成的appid,且指纹信息已配置。权限申请请联系devConnect@huawei.com,主题标明Link Turbo Kit权限申请。

  1. 开发流程示意图:
  2. 添加appid信息
  3. 在自身应用AndroidManifest.xml中添加开发者联盟创建应用时生成的appid信息,name为"com.huawei.hms.client.appid",value为"appid=***",***用自己注册的appid。

    AndroidManifest.xml

    <meta-data android:name="com.huawei.hms.client.appid" android:value="appid=******" />
  4. 创建LocalSocket客户端并注册
  5. 应用启动,创建LocalSocket客户端,建议与系统LocalSocket服务器的通信通道,然后向系统注册。

    创建LocalSocket客户端

    ApiTest.java

    public Boolean openLocalSocket() { try { Log.d(TAG, "Enter OpenSocket"); // 1.create socket, socketName is resmon clientSocket = new LocalSocket(); clientSocketAddr = new LocalSocketAddress(NAME_SOCKET, LocalSocketAddress.Namespace.ABSTRACT); // 2.connect server clientSocket.connect(clientSocketAddr); Log.i(TAG, "openLocalSocket success"); // 3.new Thread to receive msg from server final BufferedReader readStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); new Thread(new Runnable() { @Override public void run() { try { String respMsg; int readed = 0; while (readed != -1) { char[] revMsg = new char[BUFFER_SIZE]; char[] msgType = new char[2]; // 4.received msg from server, include msg head and body, head len is 42 Bytes readed = readStream.read(revMsg, 0, BUFFER_SIZE); /** * 5. get msgType(MSGTYPE_BEGIN_INDEX = 6, * MSGTYPE_END_INDEX = 7), * revMsg[MSGTYPE_BEGIN_INDEX] == 1 is reqMsg, * revMsg[MSGTYPE_BEGIN_INDEX] == 2 is respMsg */ msgType[0] = revMsg[MSGTYPE_BEGIN_INDEX]; msgType[1] = revMsg[MSGTYPE_END_INDEX]; int bodyBegin = RESP_HEAD_LEN; if (msgType[0] == 1) { bodyBegin = REQ_HEAD_LEN; } /** * 6. get body msg. bodyBegin is REQ_HEAD_LEN=44 or RESP_HEAD_LEN=46 * and translate to json string */ respMsg = String.valueOf(revMsg, bodyBegin, revMsg.length - bodyBegin); respMsg = respMsg.trim(); MainActivity.MyHandler handler = MainActivity.getMyHandler(); Message msg = new Message(); msg.what = MSG_RECEIVE; msg.obj = respMsg; handler.sendMessage(msg); Log.d(TAG, "---------> revMsg is : " + respMsg); } } catch (IOException e) { Log.d(TAG, e.getMessage()); } } }).start(); } catch (IOException e) { Log.d(TAG, e.getMessage()); return false; } return true; }

    注册部分:

    ApiTest.java

    public Boolean registerApp() { try { if (clientSocket == null) { Log.w(TAG, "please open socket first!!!"); return false; } MsgHeader msgHeader = new MsgHeader(); // 1.generate msg head,hdrData is byte[] byte[] hdrData = msgHeader.getNewHeaderMsg(registerMessage.length()); // 2.generate msg head and body, body is as this: byte[] msgByte = registerMessage.getBytes(); byte[] newByte = concatByte(hdrData, msgByte); writeSocket(newByte); Log.d(TAG, "send msg: " + registerMessage); } catch (IOException e) { Log.d(TAG, e.getMessage()); return false; } return true; }
  6. 查询系统可用网络接口等信息
  7. 代码如下:

    ApiTest.java

    public Boolean getAvailableNetInterface() { try { if (clientSocket == null) { Log.e(TAG, "please open socket first"); return false; } MsgHeader msgHeader = new MsgHeader(); byte[] hdrData = msgHeader.getNewHeaderMsg(getAvailableNetInterfaceMessage.length()); Log.d(TAG, "hdrData = " + Arrays.toString(hdrData)); byte[] msgByte = getAvailableNetInterfaceMessage.getBytes(); byte[] newByte = concatByte(hdrData, msgByte); Log.d(TAG, "newByte = " + Arrays.toString(newByte)); writeSocket(newByte); Log.d(TAG, "send msg: " + getAvailableNetInterfaceMessage); } catch (IOException e) { Log.d(TAG, e.getMessage()); return false; } return true; }
  8. 应用结束,解除注册。
  9. 代码如下:

    ApiTest.java

    public Boolean unRegisterApp() { try { if (clientSocket == null) { Log.w(TAG, "please open socket first"); return false; } MsgHeader msgHeader = new MsgHeader(); byte[] hdrData = msgHeader.getNewHeaderMsg(unRegisterMessage.length()); Log.d(TAG, "unRegisterApp hdrData = " + Arrays.toString(hdrData)); byte[] msgByte = unRegisterMessage.getBytes(); byte[] newByte = concatByte(hdrData, msgByte); writeSocket(newByte); Log.d(TAG, "send msg: " + unRegisterMessage); } catch (IOException e) { Log.d(TAG, e.getMessage()); return false; } return true; }

目前我们已经接入了LinkTurbo能力,在点击完界面"OpenSocket"和"registerApp"之后,可通过点击界面按钮的形式验证接口是否成功调用。例如:
点击"getNobileSignalLevel"后,日志如下所示:

您已经完成了本篇Codelab并学到:

关于LinkTurbo更多信息,请阅读以下链接。
相关介绍

下载 source code

已复制代码