# Samsung SmartView Android SDK

See: Description

Packages
Package | Description
--- | ---
com.koushikdutta.async |
com.samsung.multiscreen | Contains classes for using the MultiScreen SDK Android API

# Samsung SmartView Adroid SDK

This API is the Android component of the SmartView SDK.
For more information, see Samsung Developers.

## Download library

- Download the SmartViewSDK Android Sender SDK.

## Include in your Android project

Copy `/dist/android-smartview-sdk-2.x.x.jar` to your libs folder and link as normal.
Supports Eclipse or Android Studio projects.

## Javadocs

API reference docs are online and included in `/dist/docs/`.

# API Usage

The API supports three main features:

- Discover: Discover a compatible Samsung SmartTV on your network
- Connect: Connect and launch applications on the SmartTV
- Communicate: Communicate with a SmartTV application and other mobile devices.

## Discover

You can discover a compatible Samsung SmartTV on your network using the Search class. The workflow is:

1. Start the Discovery Process
2. Listen for events indicating services added/removed.
3. Stop the Discovery Process

Generally, while the discovery process is running, you should display a list of discovered services to your user, and allow them to select one. You can get a list of the discovered services at any time, even after the discovery process is stopped.

Example:

        // Get an instance of Search
        Search search = Service.search(getContext());

        // Add a listener for the service found event
        search.setOnServiceFoundListener(
            new OnServiceFoundListener() {

                @Override
                public void onFound(Service service) {
                    Log.d(LOGTAG, "Search.onFound() service: " + service.toString());

                    // Add service to a displayed list where your user can select one.
                    // For display, we recommend that you show: service.getName()
                }
            }
        );

        // Add a listener for the service lost event
        search.setOnServiceLostListener(
            new OnServiceLostListener() {

                @Override
                public void onLost(Service service) {
                    Log.d(LOGTAG, "Search.onLost() service: " + service.toString());

                    // Remove this service from the displayed list.
                }
            }
        );

        // Start the discovery process
        search.start();

        // Do something while we wait for services to be discovered.
        ...

        // Stop the discovery process after some amount of time, preferably once the user
        // has selected a service to work with.
        search.stop();

## Connect

Once a device or “service” has been discovered, you can interact with the service to get additional information about the device, or start, stop, install, and retrieve information about applications. Both installed applications and cloud applications can be launched.

Example:

        // Example uri
        Uri url = Uri.parse("http://dev-multiscreen-examples.s3-website-us-west-1.amazonaws.com/examples/helloworld/tv/");

        // Example channel id
        // Note: We recommend that you use a reverse domain style id for your
        // channel to prevent collisions.
        String channelId = "com.samsung.multiscreen.helloworld";

        // Get an instance of Application.
        Application application = service.createApplication(url, channelId);

        // Listen for the connect event
        application.setOnConnectListener(new OnConnectListener() {

            @Override
            public void onConnect(Client client) {
                Log.d(LOGTAG, "Application.onConnect() client: " + client.toString());
            }
        });

        // Connect and launch the application.
        // When you connect to a service, the specified application will
        // be launched automatically.
        application.connect(new Result<Client>() {

            @Override
            public void onSuccess(Client client) {
                Log.d(LOGTAG, "Application connect onSuccess() client: " + client.toString());

                // The application is launched, and is ready to accept
                // messages.
            }

            @Override
            public void onError(Error error) {
                Log.d(LOGTAG, "Application connect onError() error: " + error.toString());

                // Uh oh. Handle the error.
            }
        );

## Communicate

After connecting and launching the application on the TV, otherwise known as the host, the client can now publish both text and binary messages to any and/or all connected clients.

### Publishing Messages

To send or publish a message, the client must supply:

1. Message Event: Application defined event describing the message
2. Message Data: A string or object representing the message data

Example:

        // Note: The TV application is designated as the "HOST"
        String event = "say";
        String messageData = "Hello World!";

        // Send a message to the TV application only, by default.
        application.publish(event, messageData);

        // Send a message to the TV application, explicitly.
        application.publish(event, messageData, Message.TARGET_HOST);

        // Send a "broadcast" message to all connected clients EXCEPT yourself.
        application.publish(event, messageData, Message.TARGET_BROADCAST);

        // Send a message to all clients INCLUDING yourself
        application.publish(event, messageData, Message.TARGET_ALL);

        // Send a message to a specific client
        String clientId = "123467"; // Assuming that this is a valid id of a connected client
        Client client = channel.getClients().get(clientId);
        application.publish(event, messageData, client);

        // Send a message to a list of clients
        List<Client> clients = ... // You can create any combination list of clients
        application.publish(event, messageData, clients);

        // Send a binary message to the TV application only, by default.
        byte[] payload = {0x00, 0x01, 0x02, 0x03};
        application.publish(event, messageData, payload);

### Receiving Messages

To receive a message on the connected channel, the client must simply add a message listener.

Example:

        // The event that this client is interested in receiving published messages.
        String event = "say";

        // Listen for a message by event
        application.addOnMessageListener(event, new OnMessageListener() {

            @Override
            public void onMessage(Message message) {
                Log.d(LOGTAG, "Application.onMessage() message: " + message.toString());

                // Got a message with some data or a binary payload.
            }
        });

## Advanced Options

Advanced options are not required to be called by default, but may allow the developer flexibility in the design and implementation of the application.

### disconnect

By default, the specified application will stop automatically when this is the last client to disconnect. To disable this behavior, call `disconnect(false, ...)`.

Example:

        // Disconnect from the application. The application will continue to run on the TV, even
        // if this was the last connected client.
        application.disconnect(false, new Result<Client>() {

            @Override
            public void onSuccess(Client client) {
                Log.d(LOGTAG, "Application disconnect onSuccess() client: " + client.toString());

                // The application will continue to run on the TV.
            }

            @Override
            public void onError(Error error) {
                Log.d(LOGTAG, "Application disconnect onError() error: " + error.toString());
            }
        });

### install

To install an application, you must create an application with the specified application id issued by Samsung when the application is submitted. See the App Submission pages at the Samsung Developer Forum for more information on the app submission process.

Example:

        // Example app id
        String appId = "111299000796";

        // Example channel id
        String channelId = "com.samsung.multiscreen.helloworld";

        // Get an instance of Application. Then connect to the service
        Application application = service.createApplication(appId, channelId);

        // Install the application on the TV. Note: This will only bring up the installation page on the TV.
        // The user will still have to acknowledge by selecting "install" using the TV remote.
        application.install(new Result<Boolean>() {

            @Override
            public void onSuccess(Boolean result) {
                Log.d(LOGTAG, "Application install onSuccess() result: " + result.toString());

                // Application installed
            }

            @Override
            public void onError(Error error) {
                Log.d(LOGTAG, "Application install onError() error: " + error.toString());

                // Uh oh. Handle the error.
            }
        });

### WoW (Wake on Wireless LAN)

#### Latest SmartViewSDK include Enhaned WOW feature

SDK 2.4.0 makes your app enable WOW function automatically without NO source code change.

1. If your app have been connected once,

   - SDK manage the TV device list for WOW feature
   - SDK handles below Basic UI scenario

2. Basic UI scenario

   - Turn the TV off (put to standby).
   - Appears marked as “TV_NAME(standby)” within 7 seconds.
   - Select the TV.
   - The TV power on
   - The TV WebApp(or DMP) should start loading on the TV.
   - The video should start playing on the TV.

#### Implement manually lower version of SDK 2.4.0

1.How to get MAC Address:

    Service service.getDeviceInfo(new Result<Device>() {
        @Override
        public void onSuccess(Device device) {
               if (device != null)
                   String wifiMac = device.getWifiMac();

               SharedPreferences.Editor editor = getSharedPreferences("WoWMacs", 0).edit();

               editor.putString(name, wifiMac);

               editor.commit();

        }

        @Override
        public void onError(com.samsung.multiscreen.Error error) {
        }
    });

2.Wake up TV:

    String mac = getSharedPreferences("WoWMacs", 0).getString(service.getName(), 0);

    Service.WakeOnWirelessLan(mac);

3.Wake up TV and connect:

    //For connection is used default timeout: Service.DEFAULT_WOW_TIMEOUT_VALUE = 60000

    String mac = getSharedPreferences("WoWMacs", 0).getString(service.getName(), 0);

    String uri = getSharedPreferences("WoWIps", 0).getString(mac, 0);

    Service.WakeOnWirelessAndConnect(mac, uri, new Result<Service>() {
        @Override
        public void onSuccess(Service service) {
            if (service != null) {
                Application application = service.createApplication("YOUR_APP_ID","YOUR_CHANNEL_ID");

                application.connect(new Result<Client>() {

                    @Override
                    public void onSuccess(Client client) {
                        if (client != null) {
                            Log.d("log", "onSuccess: " + client.toString());
                        }
                    }

                    @Override
                    public void onError(com.samsung.multiscreen.Error error) {
                        Log.d("log", "onError: " + error.getMessage());
                    }
                });
            }
        }

        @Override
        public void onError(com.samsung.multiscreen.Error error) {
            Log.d("log", "onError: " + error.getMessage());
        }

    });

    //Connect using custom timeout:

    int timeout = 120000;

    Service.WakeOnWirelessAndConnect(mac, uri, timeout, new Result<Service>()){

    ....

    }

## License

Copyright © 2017 Samsung Electronics

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.