[Huawei Developers] John Wo:
1. Background

Today's online games offer stunning effects and exhilarating gameplay, and user expectations for gaming have skyrocketed. Here, we'll talk about how large-screen games can be controlled on a mobile phone or tablet, via Nearby Service's Bluetooth- and Wi-Fi-based solution.

2. Applicable Scenarios

With Nearby Service, users are able to wirelessly transmit real-time data from the sensor on a mobile phone or tablet to a large-screen device, to enjoy gaming that is vast in scale, but remarkably easy to control. This controller function can be downloaded, simply by scanning the QR code displayed on the home page for the large-screen game, after which all gaming operations can be transmitted. Now we'll show you how transmission works for phone gyroscope data.

This solution enables users to benefit from a more powerful and cost-effective game controller, which does not require additional mobile data to be consumed, or even an Internet connection, meaning that it remains reliable at all times. In addition, it empowers the controller to discover, connect, and exchange data with nearby large-screen gaming devices, facilitating effortless multiplayer gaming.

3. Implementation Process

As shown below, the discoverer corresponds to the game controller, while the broadcaster corresponds to the large-screen gaming device. The implementation process is similar: broadcast and scanning -> connection setup -> data transmission -> disconnection. In the broadcast and scanning phase, Bluetooth low energy (BLE) broadcasting technology is used to discover devices. In the data transmission phase, high-speed transmission between devices can be implemented with ease, backed by the traditional P2P protocol.

4. Key Integration Steps and Code

(1) Broadcast.

<p style="line-height: 1.5em;">public void doStartBroadcast(View view) {
Context context = getApplicationContext();
mDiscoveryEngine = Nearby.getDiscoveryEngine(context);
BroadcastOption.Builder advBuilder = new BroadcastOption.Builder();
advBuilder.setPolicy(Policy.POLICY_STAR);
mDiscoveryEngine.startBroadcasting(myNameStr, myServiceId, mConnCb, advBuilder.build());
}
</p>

(2) Scan.

<p style="line-height: 1.5em;">public void doStartScan(View view) {
ScanOption.Builder discBuilder = new ScanOption.Builder();
discBuilder.setPolicy(Policy.POLICY_STAR);
mDiscoveryEngine.startScan(myServiceId, mDiscCb, discBuilder.build());
}
</p>

(3) Request a connection.

<p style="line-height: 1.5em;">private void connect(View view) {
mDiscoveryEngine.requestConnect(myNameStr, mEndpointId, mConnCb)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(SensorActivity.this,"connect Success",Toast.LENGTH_SHORT).show();
Log.d(TAG, "connect Success:");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Toast.makeText(SensorActivity.this,"connect Failure",Toast.LENGTH_SHORT).show();
Log.d(TAG, "connect Failure");
}
});
}
</p>

(4) Send and receive data.

Controller: Send the real-time data obtained by the phone gyroscope.

<p style="line-height: 1.5em;">private void sendData(Data bytesData){ Nearby.getTransferEngine(getApplicationContext()).sendData(mEndpointId, bytesData);}
</p>

Large-screen games: Receive data.

<p style="line-height: 1.5em;">private DataCallback mDataCb =
new DataCallback() {
@Override
public void onReceived(String string, Data data) {
Log.d(TAG, "onReceived, Data.Type = " + data.getType());
Log.d(TAG, "onReceived, string ======== " + string);
switch (data.getType()) {
case Data.Type.BYTES:
String str = new String(data.asBytes(), UTF_8);
receiveMessage(data);
break;
default:
Log.i(TAG, "the other Unknown data type, please check the uploaded file.");
return;
}
}

@Override
public void onTransferUpdate(String string, TransferStateUpdate update) {
Log.d(TAG, "onTransferUpdate.Status=======" + update.getStatus());
return;
}
}
};
</p>

<p style="line-height: 1.5em;">private void receiveMessage(Data data) {
msgStr = new String(data.asBytes(), UTF_8);

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
String dateStr = dateFormat.format(new Date());
tvData.append("\n" + dateStr + msgStr);
}
</p>

(5) Disconnect.

<p style="line-height: 1.5em;">private void disconnect(){
mDiscoveryEngine.disconnect(mEndpointId);
}
</p>

5. Effects

Due to hardware limitations, we used two mobile phones for the test, with one serving as the controller to send real-time data for phone gyroscope, and the other as the large-screen gaming device to receive the data. After the data is received, it is calculated to obtain the game controller operations.

When the distance between the two devices is about three meters, the comparison between the transmitted and received data is as follows:

When two devices are close to each other (for example, approximately three or four meters apart), the latency for data transmission is generally less than one second, which can be regarded as real-time data. Even when the distance is changing, the service remains user-friendly.

Note: Currently, when there are no obstacles obstructing the Bluetooth signal from the two devices, data transmission remains smooth within seven meters. The Bluetooth connection will likely be inconsistent when the distance exceeds eight meters, and non-existent beyond ten meters.

6. Precautions

(1) In this example, you would need to enter the names of both devices for the connection. In the broadcast and scanning phase, you can display the list of discovered devices and allow users to select the devices to be connected.

(2) The example above makes use of the point-to-point connection policy, which supports a 1-to-N or star-shaped connection topology. You can also use the point-to-point connection policy that supports an M-to-N or cluster-shaped connection topology, and policy that supports a 1-to-1 connection topology. For details about the application scenarios of these three policies, please refer to Nearby Service-Broadcast and Scanning.

(3) The data transmitted in this example is in byte sequence (BYTES), in which the data size cannot exceed 32 KB. You can also use two other data types, as needed: FILE and STREAM.

(4) In this example, unprocessed phone gyroscope data is sent. You can control the game operations by calculating the data.

(5) In this example, there is some amount of latency when data is sent and received. The latency is for reference only, and should not be regarded as the standard of performance for Nearby Service.