Samsung Electronics logo

Discovering AllShare Devices

Table of Contents


Get Joined Device

To discover AllShare devices, an application can use the DeviceFinder instance by invoking getDeviceFinder() in ServiceProvider.

 
/**
* Get DeviceFinder instance by invoking getDeviceFinder().
*/
//Define success callback for creating ServiceProvider
var deviceFinder = null;
var sCB = function(serviceProvider) {
    // Framework Service is connected successfully.
    deviceFinder = serviceProvider.getDeviceFinder();
};

// Define error callback for creating ServiceProvider.		
var eCB = function(e, state){
	//Connecting to framework service has failed.
	console.log("createServiceProvider error: " + e.message + ", state: "+ state);
};

webapis.allshare.serviceconnector.createServiceProvider(sCB, eCB);
			

DeviceFinder provides four types of methods to get the Device instances from the AllShare Framework service. The methods are explained in the following table.

Interface Method/Attribute Description
DeviceFinder Device? getDevice(DeviceType deviceType, DeviceId id) Provides a device with a specified device ID and device type.
DeviceArray getDeviceList(DeviceType deviceType) Provides a list of discovered devices with specified device types.
DeviceArray getDeviceListByDomain(DeviceType deviceType, DeviceDomain domain) Provides a list of discovered devices with specified device types within specified network domains.
DeviceArray getDeviceListByNIC(DeviceType deviceType, DOMString nic) Provides a list of discovered devices with specified types within specified network interface controllers (NICs).

Through the DeviceFinder instance, the AllShare application can discover six types of devices in the network; each type of device is listed in the table below:

Device Type Enumeration Description
AVPLAYER Audio and video player device.
IMAGEVIEWER Image viewer device.
FILERECEIVER File receiver device.
MEDIAPROVIDER Media provider device.
MEDIARECEIVER Media receiver device.
TVCONTROLLER TV controller device.

The sample code for getting joined devices is as follows:

 
/**
* Get joined devices.
*/
var deviceFinder = null;
function getProviders(){
	if(deviceFinder == null){
		return;
	} 

	//Get media provider, AV player, image viewer devices
	try{
		var mediaproviders = deviceFinder.getDeviceList(“MEDIAPROVIDER”);
		var avplayers = deviceFinder.getDeviceList(“AVPLAYER”);
		var imgviewers = deviceFinder.getDeviceList(“IMAGEVIEWER”);
	}catch(e){
		console.log("error: " + e);
	}
}
	
var sCB = function(serviceProvider){
	//Get DeviceFinder instance
	deviceFinder = serviceProvider.getDeviceFinder();

	// Get joined devices after 3 sec later.
	setTimeout(getProviders, 3000);			
};
		
var eCB = function(e, state){
	console.log("createServiceProvider error: " + e + ", state: "+ state);
};

webapis.allshare.serviceconnector.createServiceProvider(sCB, eCB);
				

The sample code for other device discovery APIs is as follows:

 
var serviceProvider; //It is assumed that you have been obtained a serviceProvider object.
var deviceFinder = serviceProvider.getDeviceFinder();

// Find the device by specifying DeviceType and DeviceId.
var targetDev1 = deviceFinder.getDevice('AVPLAYER', deviceID); // deviceID indicates the specific device.
// targetDev1 is an AVPlayer device object whose ID is equal to deviceID.

// Find the device by specifying DeviceType and DeviceDomain
var targetDev2 = deviceFinder.getDeviceListByDomain('AVPLAYER', 'LOCAL_NETWORK');
// targetDev2 is a list of AVPlayer devices in the local network.

// Find the device by specifying DeviceType and NIC
var targetDev3 = deviceFinder.getDeviceListByNIC('AVPLAYER', 'Wireless LAN');
//targetDev3 is a list of AVPlayer devices available in the local wireless LAN network.
				

Monitor Device Join

An AllShare application can get device list change events by registering DeviceDiscoveryCallback callback by invoking addDeviceDiscoveryListener(). Then, the ondeviceadded() or ondeviceremoved() callback function is invoked when an AllShare device is added to or removed from the network.

The sample code for getting joined devices is as follows:

 
/**
* Monitoring devices when they join/leave the AllShare service.
*/
var deviceFinder = null;
var sCB = function(serviceProvider) {
	// Get DeviceFinder instance
	deviceFinder = serviceProvider.getDeviceFinder();
    
	// Define device discovery listener 
var listenerCB = function(){
	ondeviceadded = function(device) {
		// The device disappeared in the network.
		// Do something since the device list has changed.
	};
	ondeviceremoved = function(device) {
		// The device newly discovered in the network.
		// Do something since the device list has changed.
	};
};

    // Set listener to monitor device list change event.
	var listenerKey = deviceFinder.addDeviceDiscoveryListener(listenerCB);	
}; 	
		
var eCB = function(e, state){
	console.log("createServiceProvider error: " + e + ", state: "+ state);
};

webapis.allshare.serviceconnector.createServiceProvider(sCB, eCB);


				

The following table shows APIs that are used to connect and disconnect.

Interface Method/Attribute Description
DeviceFinder long addDeviceDiscoveryListener(DeviceDiscoveryCallback deviceDiscoveryCallback) Register device discovery event listener.
void removeDeviceDiscoveryListener(long deviceDiscoveryListener) Clear device discovery event listener.

Get Device Information

After getting the device list, an application can get information for each device using Device. The AllShare Framework provides the following device information.

Attribute Description Return Value
id A string value represents the unique ID of the device, for example: “uuid: [a set of string]“ e.g. “uuid:08f0d180-0002-1000-8823-00245491c0ab”
deviceDomain A string value defined in DeviceDomain enumeration. Device.DeviceDomain enumeration
deviceType A string value defined in DeviceType enumeration. Device.DeviceType enumeration
iconArray A sequence of Icon objects. A list of Icon objects.
ipAddress

A string value specifies the device IPv4 address.

IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, for example, 172.16.254.1. Each part represents a group of 8 bits (octet) of the address.

e.g. “192.168.1.100”
modelName A string value specifies the device model name. e.g. “Samsung Smart TV”
name A string value specifies the device name. e.g. “[TV]Smart TV”
nic A string value specifies the NIC (network interface controller) name of the device. Network adaptor name string
 
/**
* Print all attributes of a Device object.
*/
var deviceFinder = null;
function getProviders() {
	if (deviceFinder == null) {
		return;
	} 

	// Get AV player devices
	try {
		var avplayers = deviceFinder.getDeviceList('AVPLAYER');
		
		if (avplayers.length > 0) {
		var id = avplayers[0].id; // The unique ID of the device, e.g. “uuid:08f0d180-0002-1000-8823-00245491c0ab”
		var domain = avplayers[0].deviceDomain;  // The device domain, e.g. “LOCAL_NETWORK”
		var type = avplayers[0].deviceType; // The device type, e.g. “AVPLAYER”
		var icons =  avplayers[0].iconArray; // A sequence of Icon object.
		var ip = avplayers[0].ipAddress; // IPv4 address.
		var modelName = avplayers[0].modelName; // Device model name.
		var name = avplayers[0].name; // Device name.
		var nic = avplayers[0].nic; // Network interface controller.
		}
	
	}catch (e) {

		console.log("error: " + e);
	}
}
	
var sCB = function(serviceProvider) {

	// Get DeviceFinder instance
	deviceFinder = serviceProvider.getDeviceFinder();
	
	// Get joined devices.
	getProviders();			
};
		
var eCB = function(e, state){
	console.log("createServiceProvider error: " + e + ", state: "+ state);
};

webapis.allshare.serviceconnector.createServiceProvider(sCB, eCB);