HUAWEI Cast Engine facilitates fast, stable, and low-latency collaboration between mobile phones and external large screens, delivering a seamless, cross-device collaborative experience, and a responsive mobile phone–centric ecosystem.

What You Will Create

In this codelab, you will create a demo project, integrate the HUAWEI Cast Engine SDK into the demo project, and complete the overall process setup of the HUAWEI Cast Engine service.

What You Will Learn

Hardware Requirements

Software Requirements

Required Knowledge

HUAWEI Cast Engine integration requires the following preparations:

For details, see HUAWEI Cast EngineApp Development .

1.Integrate the HUAWEI Cast Engine SDK.

Obtain the AAR dependency package according to the Cast Engine SDK. Save the downloaded AAR package to the libs directory of the third-party app project, and add dependencies in the build.gradle file.

dependencies { //Adds dependencies. implementation fileTree(include: ['*.aar'], dir: 'libs') ... }

Click the sync button, as shown in the following figure.

After the synchronization, the screen shown in the following figure is displayed.

2.Add permissions to the AndroidManifest.xml file.

Add permissions to the AndroidManifest.xml file of the app.

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

3.Initialize the service.

Initialize PlayerClient in MyService, process Huawei Cast Engine service callback messages, and listen to local configuration changes.

SinkTesterService.java

public class SinkTesterService extends Service{ private PlayerClient mPlayerClient; @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate called()."); ... mCallbackHandler = new CallbackHandler(getMainLooper()); mPlayerClient = PlayerClient.getInstance(); mPlayerClient.registerCallback(mCallback); mPlayerClient.init(mContext); } }

Process the Cast Engine callback messages.
When the EVENT_ID_SERVICE_BIND_SUCCESS message is received, the service is successfully started. In this message, you can set the screen projection capability, device information, and authentication mode of the display device.
When the EVENT_ID_PIN_CODE_SHOW message is received, it indicates that a device is requesting for a connection. The display device prompts the user to handle the connection request. If the connection is allowed, the PIN is obtained and displayed through the message, so that the user can enter the PIN on their mobile phone to set up the connection.
After the EVENT_ID_CONNECT_REQ message is received, which carries the DisplayInfo object, the display device shows the projection screen. You can use the displayInfo.getProjectionDevice() method to obtain the phone's device information object. Preserve the object, as it will be used for later projection.
After the EVENT_ID_PAUSED is received, the display device can set the surface and play the video stream.
SinkTesterService.java

public class SinkTesterService extends Service{ ... private IEventListener mCallback = new IEventListener.Stub() { public boolean onEvent(Event event) { int eventId = event.getEventId(); Log.e(TAG, "eventId: " + eventId); Message msg = mCallbackHandler.obtainMessage(); msg.what = eventId; msg.obj = event; msg.sendToTarget(); return true; } public boolean onDisplayEvent(int eventId, DisplayInfo displayInfo) { Log.e(TAG, "handleDisplayEvent: " + eventId); Message msg = mCallbackHandler.obtainMessage(); msg.what = eventId; msg.obj = displayInfo; msg.sendToTarget(); return true; } }; ... private class CallbackHandler extends Handler { public CallbackHandler(Looper mainLooper) { super(mainLooper); } @Override public void handleMessage(Message msg) { if (msg == null) return; Log.d(TAG, "msg " + msg.what); switch (msg.what) { case Constant.EVENT_ID_SERVICE_BIND_SUCCESS: if (mPlayerClient != null){ int framerate = 30; HiSightCapability capability = new HiSightCapability(1920, 1080, 1920, 1080); capability.setVideoFps(framerate); mPlayerClient.setCapability(capability); String deviceName = mBluetoothAdapter.getName(); DeviceInfo deviceInfo = new DeviceInfo(deviceName, DeviceInfo.TYPE_TV); mPlayerClient.setDiscoverable(mIsDiscoverable, deviceInfo); boolean needPassword = SharedPreferenceUtil.getAuthMode(mContext); String password = SharedPreferenceUtil.getPassword(mContext); boolean isNewPassword = false; AuthInfo authInfo = null; if (needPassword) { Log.d(TAG, "password: $" + password + "$"); authInfo = new AuthInfo(AuthInfo.AUTH_MODE_PWD, password, isNewPassword); } else { authInfo = new AuthInfo(AuthInfo.AUTH_MODE_GENERIC); } mPlayerClient.setAuthMode(authInfo); } else { Log.e(TAG, "mPlayerClient is null."); } break; case Constant.EVENT_ID_CONNECT_REQ: DisplayInfo displayInfo = (DisplayInfo) msg.obj; if (displayInfo != null) { mProjectionDevice = displayInfo.getProjectionDevice(); startPlayActivity(); } else { Log.e(TAG, "displayInfo is null."); } break; case Constant.EVENT_ID_PIN_CODE_SHOW: DisplayInfo displayInfo = (DisplayInfo) msg.obj; if ((displayInfo != null) && (mProjectionDevice = displayInfo.getProjectionDevice()) != null) { String pinCode = displayInfo.getPinCode(); String deviceName = mProjectionDevice.getDeviceName(); startAlertActivity(pinCode, deviceName); } else { Log.e(TAG, "displayInfo is null."); } break; case Constant.EVENT_ID_PAUSED: mPlayerClient.setHiSightSurface(mHiView.getHolder().getSurface()); mPlayerClient.play(new TrackControl(mProjectionDevice.getDeviceId())); break; } } } ... }

Cast the mobile phone's screen to the display device. For details, please refer to the HUAWEI Cast EngineApp Development .
You have successfully completed this Codelab and learned:

For more information, please click the following link:

Related documents

Download source code

Code copied