📜 ⬆️ ⬇️

Samsung Chord SDK for Android P2P games development

Good Friday, Habr!

Making games is a laborious process, but there is no magic and mystery in it. It does not differ from the creation of any application, consists of the same steps, and it is possible to divide it into blocks, modules and steps. Creating a network game can increase the time and cost of development several times. But application networking is an independent module that is developed and tested separately from the game itself. We will talk about this module today. We present you the Samsung Chord SDK - a free tool for simple and fast creation of applications for the exchange of files and messages in real time between devices on the same network.



Samsung invests a lot of power in the development of Android. Visit the resource for developers - there you will find many useful tools, and one of them is the Samsung Mobile SDK , which we already wrote about earlier . In the following publications we will tell you more about each of the other tools.
')


Samsung Chord SDK (prod. As rus. “Code”, eng. [Kd]; trans. Bundle, chord, string, chord) allows you to develop applications for local information exchange without using a cloud or without knowing the details of the network.

Chord Benefits:
â—Ź Easy to detect and establish connection with devices nearby;
â—Ź real-time interaction with multiple devices;
â—Ź fast peer-to-peer connection without server;
â—Ź a decentralized network where members can connect and disconnect at any time on the fly.

Samsung Chord SDK v1.0 was released on April 25, 2013, it supports Android 4.0 (> = API level 14) - both arm and x86 architectures. And in September 2013, Chord appeared in the Samsung Mobile SDK: the code was substantially reworked (unfortunately, backward compatibility was not preserved), support for encrypted channels was added. It will be very helpful to read the FAQ .

Let's take a look at how the Samsung Chord SDK works:



Device 1-N devices are connected to the network in one of the following ways: Wi-Fi, AndroidAP, or Wi-Fi Direct (description below). An application is installed on the devices, which contains a service that interacts with the Chord and the “activity” of the application. In turn, “game” sends or receives messages from “activity”. Each of the “device” is a node. The transfer of messages is possible both for a specific node and a channel. The channel can be public or private (in more detail here ).

To demonstrate how easy it is to integrate Chord into any gaming application, take the ready-made Snake game from the Android SDK samples: the project is available on github .

First you need to install the Samsung Mobile SDK via Eclipse update. Let's create the project from examples of Snake. Next, you need to copy the files from the “% android_path% / samsungmobilesdk / Samsung Mobile SDK / 1.0.3 / Libs / chord” folder of our project libs and connect them to the Build path.

The following is a simple code that does not need comments:

... @Override public void onCreate(Bundle savedInstanceState) { ... mChordManager = new SchordManager(getActivity()); List<Integer> infList = mChordManager.getAvailableInterfaceTypes(); if(infList.isEmpty()){ return; } int interfaceType = SchordManager.INTERFACE_TYPE_WIFI; try { mChordManager.start(interfaceType, mManagerListener); mSelectedInterface = interfaceType; Log.d(TAG, TAGClass +" start(" + getInterfaceName(interfaceType) + ")"); } catch (IllegalArgumentException e) { Log.d(TAG, TAGClass +" Fail to start -" + e.getMessage()); } catch (InvalidInterfaceException e) { Log.d(TAG, TAGClass +" There is no such a connection."); } catch (Exception e) { Log.d(TAG, TAGClass +" Fail to start -" + e.getMessage()); } } … SchordManager.StatusListener mManagerListener = new SchordManager.StatusListener() { @Override public void onStarted(String nodeName, int reason) { if (reason == STARTED_BY_USER) { SchordChannel channel = mChordManager.joinChannel(CHORD_HELLO_TEST_CHANNEL, mChannelListener); } else if (reason == STARTED_BY_RECONNECTION) { // Re-start by network re-connection. } } @Override public void onStopped(int reason) { if (STOPPED_BY_USER == reason) { // Success to stop by calling stop() method } else if (NETWORK_DISCONNECTED == reason) { // Stopped by network disconnected } } }; private SchordChannel.StatusListener mChannelListener = new SchordChannel.StatusListener() { @Override public void onNodeJoined(String fromNode, String fromChannel) { //    ,    } @Override public void onDataReceived(String node, String channel, String payloadType, byte[][] payload) { String message = new String(payload[0]); //   } … //     }; 

You can send messages from other clients as follows (to all public channel nodes):
  byte[][] payload = new byte[1][]; payload[0] = ”Hello, chord”.getBytes(); SchordChannel channel = mChordManager.getJoinedChannel(CHORD_HELLO_TEST_CHANNEL); channel.sendDataToAll(CHORD_SAMPLE_MESSAGE_TYPE, payload); 


Chord allows you to create channels (abstract) through which interaction takes place. Channels can be both public and private.

Communication can be established via Wi-Fi using the following methods:

â—Ź INTERFACE_TYPE_WIFI - the usual connection of all devices to one common Wi-Fi access point;
â—Ź INTERFACE_TYPE_WIFIAP - AndroidAP, the access point is one of the Android devices;
● INTERFACE_TYPE_WIFIP2P — A typical Wi-Fi Direct connection.

You can ask the user to choose the connection method or programmatically select the most suitable, for example, like this:

  int interfaceType = 0; for (int interfaceValue : mChordManager.getAvailableInterfaceTypes()) { if (interfaceValue == SchordManager.INTERFACE_TYPE_WIFI) { interfaceType = SchordManager.INTERFACE_TYPE_WIFI; break; } else if (interfaceValue == SchordManager.INTERFACE_TYPE_WIFI_AP) { interfaceType = SchordManager.INTERFACE_TYPE_WIFI_AP; break; } else if (interfaceValue == SchordManager.INTERFACE_TYPE_WIFI_P2P) { interfaceType = SchordManager.INTERFACE_TYPE_WIFI_P2P; break; } } 


All the code for connecting and creating simple game mechanics can be found in this commit , which provides the most necessary integration with the Chord SDK. Consider the key points:

1. Added a page for player status;
2. each manages his snake;
3. conflict is considered only with its tail, other players are ignored;
4. the snake sees and has only “its own” apples;
5. The speed of the snake remains constant;
6. The one with the snake longer wins.

A small demonstration:



and screenshots:



Also take a look at an example from the Chord development team, available on the project page . It has everything you need, and you can also use it as a debugging tool.

Peering overlay networks require a lot of resources, and therefore the question of performance arises. Take a look at this demo:



Hopefully, we have already convinced you that Chord is a rather interesting tool. What else can you do with it and why is it Samsung? It's simple: Samsung occupies more than 47% of the Android market (according to the open signal ), so who else, if not our company, should take care of the ecosystem. More good apps - more satisfied users. You, as a developer, can get more new users and more game sessions if you add your application using Samsung Chord SDK to a special section “Play games” in the pre-installed application “Group Play”, which can be found in all modern Samsung phones on Android . Documentation can be found here .

If you have any questions or suggestions, feel free to write to e-mail: dev.cis@partner.samsung.com.

As we have already written , Apps4All Hackathon will be held on December 13-14 and, if you have a game of your own design, you can try to make it networked with the help of the Chord SDK and win prizes from Samsung.

Dare and meet on hakatone!

Source: https://habr.com/ru/post/205960/


All Articles