# Rokid ## 准备工作 ### 配置文件 配置文件 `init.json` 以 `JSON` 格式存储,通过 `Key-Value` 配置系统功能。 > 示例: ```json { "launchPackageName":"com.rokid.sample", "wifiSsid":"", "wifiPwd":"" } ``` 通过 `adb push` 至目录 `/sdcard/init.json` ### 调试连接 ```shell adb shell setprop persist.vendor.adb 1 ``` ## Glasses行业版SDK ### 0.背景 * 该插件仅适用于Glasses行业版OS。 ### 1.开发接入 #### 1.1配置 1. 导入 sprite_dcg_sdk.aar ### 1.系统服务 行业版SDK提供服务列表: * 语音指令服务 `IInstructService`,应用可监听系统固定语音指令 * 离线TTS服务 `ITTSService` * 系统功能服务 `ISystemFuncService`, 提供系统设置功能 * 媒体服务 `ISpriteMediaService`,提供拍照、录像等功能 #### 1.1 异步获取服务 * 异步获取语音指令服务:ServiceManager.getInstructService(Context context, IGetServiceCallback callback) * 异步获取TTS服务:ServiceManager.getTTSService(Context context, IGetServiceCallback callback) * 异步获取系统功能服务:ServiceManager.getSystemFuncService(Context context, IGetServiceCallback callback) * 异步获取媒体服务:ServiceManager.getSpriteMediaService(Context context, IGetServiceCallback callback) > 调用异步接口时,如果服务没有bind,会自动bind ```java ServiceManager.getTTSService(this) { it.playTtsMsg("让每个人享受科技,Leave Nobody Behind!") } ``` #### 1.1 同步获取服务 * bind 服务:ServiceManager. * 同步获取服务:ServiceManager.getService(Class serviceClass) > 调用同步接口时,必须提前bind,如果服务没有bind,或者bind还没完成时,将返回 `null` ```java ServiceManager.getService(ITTSService::class.java)?.playTtsMsg("让每个人享受科技,Leave Nobody Behind!") ``` ### 2. 语音指令服务 `IInstructService` #### 2.1 可用语音指令 ```java const int INSTRUCT_WAKEUP = 1; // 若琪、乐琪 const int INSTRUCT_VOLUME_UP = 2; // 声音大一点 const int INSTRUCT_VOLUME_DOWN = 3; // 声音小一点 const int INSTRUCT_LIGHT_UP = 4; // 亮一点 const int INSTRUCT_LIGHT_DOWN = 5; // 暗一点 const int INSTRUCT_TAKE_PHOTO = 6; // 拍照 const int INSTRUCT_START_AUDIO_RECORD = 7; // 录音 const int INSTRUCT_STOP_AUDIO_RECORD = 8; // 结束录音 const int INSTRUCT_START_VIDEO_RECORD = 9; // 录像 const int INSTRUCT_STOP_VIDEO_RECORD = 10; // 结束录像 const int INSTRUCT_QUIT = 11; // 退出 ``` #### 2.2 注册/反注册语音指令监听 ```java interface IInstructListener { void onInstructReceived(int instruct); } void registerListener(IInstructListener listener); void unregisterListener(IInstructListener listener); ``` 例如: ```kotlin private val mInstructListener = object : IInstructListener.Stub() { // 因为通过binder调用服务,这里必须是Stub对象 override fun onInstructReceived(instruct: Int) { Log.d("Instruct", "on receive instruct: $instruct") // 注意: 服务的回调都不在主线程,如果要操作UI,请自己切换到主线程 // textView.text = "收到语音指令: $instruct" } } override fun onResume() { super.onResume() ServiceManager.getInstructService(this) { it.registerListener(mInstructListener) } } override fun onPause() { super.onPause() ServiceManager.getInstructService(this) { it.unregisterListener(mInstructListener) } } ``` ### 3. TTS服务 `ITTSService` #### 3.1 播放TTS ```java void playTtsMsg(String ttsMsg); ``` ### 4. 系统基础功能服务 `ISystemFuncService` #### 4.1 获取/设置系统音量,音量值范围 `[0, 15]` ```java int getVolumeSpecified(); void setVolumeSpecified(int value); ``` #### 4.2 获取/设置屏幕亮度,亮度值范围 `[0, 15]` ```java int getBrightnessSpecified(); void setBrightnessSpecified(int value); ``` #### 4.3 获取设备序列号 ```java String getSn(); ``` #### 4.4 获取/设置自动休眠时间(单位:毫秒) ```java long getScreenAutoSleepTime(); void setScreenAutoSleepTime(long ms); ``` #### 4.5 唤醒设备 ```java void wakeUp(); ``` #### 4.6 设备休眠 ```java void goToSleep(); ``` #### 4.7 设备重启 ```java void reboot(); ``` #### 4.8 设备关机 ```java void shutdown(); ``` ### 5. 多媒体服务 `ISpriteMediaService` #### 5.1 多媒体Listener ```java interface ISpriteMediaListener { void onTakePicture(int errorCode, String path); // 拍照回调 void onVideoRecord(int errorCode, String path); // 录像回调 void onAudioRecord(int errorCode, String path); // 录音回调 } // 错误码 interface ISpriteMediaService { const int ERROR_NONE = 0; const int ERROR_CAMERA_EXCEPTION = 1; const int ERROR_P_SENSOR_NEAR = 2; } ``` #### 5.2 设置监听 ```java void setMediaListener(ISpriteMediaListener listener); ``` #### 5.3 设置拍照分辨率 ```java void setPictureSize(int width, int height); /* 可用分辨率 [4032x3024, 4000x3000, 4032x2268, 3264x2448, 3200x2400, 2268x3024, 2876x2156, 2688x2016, 2582x1936, 2400x1800, 1800x2400, 2560x1440, 2400x1350, 2048x1536, 2016x1512, 1920x1080, 1600x1200, 1440x1080, 1280x720, 720x1280, 1024x768, 800x600, 648x648, 854x480, 800x480, 640x480, 480x640, 352x288, 320x240, 320x180, 176x144] */ ``` #### 5.4 设置录像分辨率 ```java void setVideoSize(int width, int height); ``` #### 5.5 拍照(结果通过Lisener获取) ```java void takePicture(); ``` #### 5.6 录像(结果通过Lisener获取) ```java void startVideoRecord(); // 开始录像 void stopVideoRecord(); // 停止录像 boolean isVideoRecording(); // 是否正在录像 ``` #### 5.7 录音(结果通过Lisener获取) ```java void startAudioRecord(); void stopAudioRecord(); boolean isAudioRecording(); ``` ### 6. 自定义按键 #### 6.1 触控板标准按键 标准按键可通过onKeyDown、onKeyUp监听。可用键值如下: * 点击:KeyEvent.KEYCODE_ENTER * 前滑:KeyEvent.KEYCODE_DPAD_DOWN、KeyEvent.KEYCODE_DPAD_RIGHT * 后滑:KeyEvent.KEYCODE_DPAD_UP、KeyEvent.KEYCODE_DPAD_LEFT * 双击:KeyEvent.KEYCODE_BACK #### 6.2 特殊按键 特殊按键需通过广播注册监听,可监听事件: * 长按触摸板:SysKeyAction.TOUCH_BAR_LONG_PRESS * 点击拍照键:SysKeyAction.SPRITE_BUTTON_CLICK * 按下/弹起拍照键:SysKeyAction.SPRITE_BUTTON_DOWN、SysKeyAction.SPRITE_BUTTON_UP * 长按拍照键:SysKeyAction.SPRITE_BUTTON_LONG_PRESS 例如监听拍照键和触摸板长按事件: ```java private final BroadcastReceiver mSysKeyReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (SysKeyAction.SPRITE_BUTTON_CLICK.equals(action)) { } else if (SysKeyAction.TOUCH_BAR_LONG_PRESS.equals(action)) { } } }; private void registerSysKeyReceiver() { try { IntentFilter filter = new IntentFilter(); filter.addAction(SysKeyAction.SPRITE_BUTTON_CLICK); filter.addAction(SysKeyAction.TOUCH_BAR_LONG_PRESS); registerReceiver(mSysKeyReceiver, filter) } catch (Exception e) { e.printStackTrace(); } } ```