开发者工具
Android开发工具有很多,开发者可根据自己的喜好进行选择。在此,我们推荐开发者使用Eclipse作为自己的开发工具,本套开发指南也是针对Eclipse开发环境下进行编写的。
SDK结构
百度导航HUD SDK只有一个jar包,BaiduHUDSDK_2.0.jar,但其依赖于百度开放平台鉴权SDK:lbsCoreSDK.jar,开发者需要一起下载。
工程搭建
在新建的android工程里的libs文件夹添加BaiduHUDSDK_2.0.jar和lbsCoreSDK.jar文件,作为工程引用的外部jar包即可。
连接服务
在使用SDK前,应该使用socket连接到百度导航服务,并通过返回值确定是否连接成功:
//首先实现连接管理监听接口 private OnConnectCallback mConnectCallback = new OnConnectCallback() { @Override public void onReConnected() { Log.e(BNRemoteConstants.TAG, "reConnect to server success"); mMainHandler.post(new Runnable() { @Override public void run() { mSimpleRGLayout.setVisibility(View.VISIBLE); mRemainInfoLayout.setVisibility(View.VISIBLE); mNavilogTv.setText("重新连接到百度导航"); } }); } @Override public void onConnected() { Log.e(BNRemoteConstants.TAG, "connect to server success"); mMainHandler.post(new Runnable() { @Override public void run() { mProgressDialog.dismiss(); mSimpleRGLayout.setVisibility(View.VISIBLE); mRemainInfoLayout.setVisibility(View.VISIBLE); mNavilogTv.setText("成功连接到百度导航"); } }); } @Override public void onClose(int arg0, String arg1) { Log.e(BNRemoteConstants.TAG, "MainActivity.................onClose() disconnect, reason = " + arg1); final String reason = arg1; mMainHandler.post(new Runnable() { @Override public void run() { mProgressDialog.dismiss(); mSimpleRGLayout.setVisibility(View.GONE); mRemainInfoLayout.setVisibility(View.GONE); mNavilogTv.setText("连接断开, " + reason); } }); } @Override public void onAuth(BNRGAuthSuccess arg0) { if (arg0 != null) { Log.d(BNRemoteConstants.TAG, "auth success, serverVer = " + arg0.getServerVersion()); final String serverVer = arg0.getServerVersion(); mMainHandler.post(new Runnable() { @Override public void run() { mNavilogTv.setText("认证成功, 服务器版本: " + serverVer); } }); } } @Override public void onStartLBSAuth() { } @Override public void onEndLBSAuth(int result, String reason) { // TODO Auto-generated method stub if (result == 0) { mConnectBtn.setClickable(true); mCloseBtn.setClickable(false); } } }; //其次实现诱导信息回调接口 private OnRGInfoEventCallback mRGEventCallback = new OnRGInfoEventCallback(){ @Override public void onAssistant(BNRGAssistant arg0) { Log.d(BNRemoteConstants.TAG, "onAssistant......distance = " + arg0.getAssistantDistance() + ", type = " + arg0.getAssistantType()); } @Override public void onCruiseEnd(BNRGCruiseEnd arg0) { Log.d(BNRemoteConstants.TAG, "cruise end"); } @Override public void onCruiseStart(BNRGCruiseStart arg0) { Log.d(BNRemoteConstants.TAG, "cruise start"); } @Override public void onCurrentRoad(BNRGCurrentRoad arg0) { Log.d(BNRemoteConstants.TAG, "onCurrentRoad...........curRoadName = " + arg0.getCurrentRoadName()); } @Override public void onGPSLost(BNRGGPSLost arg0) { Log.d(BNRemoteConstants.TAG, "onGPSLost...."); } @Override public void onGPSNormal(BNRGGPSNormal arg0) { Log.d(BNRemoteConstants.TAG, "onGPSNormal...."); } @Override public void onManeuver(BNRGManeuver arg0) { Log.d(BNRemoteConstants.TAG, "onManeuver...........name = " + arg0.getManeuverName() + ", distance = " + arg0.getManeuverDistance() + ",nextRoadName = " + arg0.getNextRoadName()); } @Override public void onNaviEnd(BNRGNaviEnd arg0) { Log.d(BNRemoteConstants.TAG, "onNaviEnd..........."); } @Override public void onNaviStart(BNRGNaviStart arg0) { Log.d(BNRemoteConstants.TAG, "onNaviStart..........."); } @Override public void onNextRoad(BNRGNextRoad arg0) { Log.d(BNRemoteConstants.TAG, "onNextRoad...........nextRoadName = " + arg0.getNextRoadName()); } @Override public void onRemainInfo(BNRGRemainInfo arg0) { Log.d(BNRemoteConstants.TAG, "onRemainInfo.............distance = " + arg0.getRemainDistance() + ", time = " + arg0.getRemainTime()); } @Override public void onRoutePlanYawComplete(BNRGRPYawComplete arg0) { Log.d(BNRemoteConstants.TAG, "onRoutePlanYawComplete............"); } @Override public void onRoutePlanYawing(BNRGRPYawing arg0) { Log.d(BNRemoteConstants.TAG, "onRoutePlanYawing............"); } @Override public void onServiceArea(BNRGServiceArea arg0) { Log.d(BNRemoteConstants.TAG, "onServiceArea............name = " + arg0.getServiceAreaName() + ", distance = " + arg0.getServiceAreaDistance()); } @Override public void onEnlargeRoad(BNEnlargeRoad enlargeRoad) { Log.d(BNRemoteConstants.MODULE_TAG, "onEnlargeRoad......enlargeRoad = " + enlargeRoad); final BNEnlargeRoad enlargeInfo = enlargeRoad; mMainHandler.post(new Runnable() { @Override public void run() { handleEnlargeRoad(enlargeInfo); } }); } @Override public void onCarFreeStatus(BNRGCarFreeStatus arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onCarFreeStatus...... "); } @Override public void onCarInfo(BNRGCarInfo arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onCarInfo...... "); } @Override public void onCarTunelInfo(BNRGCarTunelInfo arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onCarTunelInfo...... "); } @Override public void onCurShapeIndexUpdate(BNRGCurShapeIndexUpdate arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onCurShapeIndexUpdate...... "); } @Override public void onDestInfo(BNRGDestInfo arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onDestInfo...... "); } @Override public void onNearByCamera(BNRGNearByCameraInfo arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onNearByCamera...... "); } @Override public void onRouteInfo(BNRGRouteInfo arg0) { // TODO Auto-generated method stub Log.d(BNRemoteConstants.MODULE_TAG, "onRouteInfo...... "); } }; // 初始化HUD SDK BNRemoteVistor.getInstance().init(getApplicationContext(), getPackageName(), getAppVersionName(MainActivity.this, getPackageName()), mRGEventCallback, mConnectCallback); //连接到导航服务 BNRemoteVistor.getInstance().open();//本机之间通信调用这个接口即可建立连接,如果在不同设备之间互联,请先调用setServerIPAddr()接口设置导航服务IP地址
断开服务
在退出时,只需要调用以下接口即可
@Override public void onDestroy() { if (BNRemoteVistor.getInstance().isConnect()) { BNRemoteVistor.getInstance().close(HUDSDkEventCallback.OnConnectCallback.CLOSE_NORMAL, "User Exit"); } BNRemoteVistor.getInstance().unInit(); super.onDestroy(); }