public class CastDevice extends Object
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
autoReconnect
Whether automatic
Channel reconnection "on demand" is enabled |
protected Set<CastDeviceCapability> |
capabilities
The
Set of CastDeviceCapabilitys for the cast device |
protected Channel |
channel
The
Channel associated with this CastDevice |
static String |
DEFAULT_MEDIA_RECEIVER_APP_ID
The application ID for the "Default Media Receiver" application
|
protected String |
deviceURL
The "base URL" of the cast device
|
protected String |
displayName
A generated name intended to be suitable to present users
|
protected String |
dnsName
The mDNS name of the cast device
|
protected static ExecutorService |
EXECUTOR
The
ExecutorService that is used for asynchronous operations |
protected String |
friendlyName
The "friendly name" of the cast device
|
protected String |
iconPath
The (device URL) relative path to its icon
|
protected CastEvent.CastEventListenerList |
listeners
The currently registered
CastEvent.CastEventListeners |
protected String |
modelName
The model name of the cast device
|
protected int |
protocolVersion
The protocol version supported by the cast device
|
static String |
SERVICE_TYPE
The DNS-SD service type for cast devices
|
protected String |
serviceName
The
DNS-SD service name |
protected InetSocketAddress |
socketAddress
The IP address and port number of the cast device
|
protected String |
uniqueId
The unique ID of the cast device
|
| Constructor and Description |
|---|
CastDevice(javax.jmdns.JmDNS mDNS,
String dnsName,
boolean autoReconnect)
Creates a new instance by extracting the required information from the
specified
JmDNS instance using the specified DNS name. |
CastDevice(javax.jmdns.ServiceInfo serviceInfo,
boolean autoReconnect)
Creates a new instance by extracting the required information from the
specified
ServiceInfo. |
CastDevice(String dnsName,
InetAddress address,
int port,
String deviceURL,
String serviceName,
String uniqueId,
Set<CastDeviceCapability> capabilities,
String friendlyName,
String modelName,
int protocolVersion,
String iconPath,
boolean autoReconnect)
Creates a new instance using the specified parameters.
|
CastDevice(String dnsName,
InetAddress address,
String deviceURL,
String serviceName,
String uniqueId,
Set<CastDeviceCapability> capabilities,
String friendlyName,
String modelName,
int protocolVersion,
String iconPath,
boolean autoReconnect)
Creates a new instance using the specified parameters and
Channel.STANDARD_DEVICE_PORT. |
CastDevice(String dnsName,
String hostname,
int port,
String deviceURL,
String serviceName,
String uniqueId,
Set<CastDeviceCapability> capabilities,
String friendlyName,
String modelName,
int protocolVersion,
String iconPath,
boolean autoReconnect)
Creates a new instance using the specified parameters.
|
CastDevice(String dnsName,
String hostname,
String deviceURL,
String serviceName,
String uniqueId,
Set<CastDeviceCapability> capabilities,
String friendlyName,
String modelName,
int protocolVersion,
String iconPath,
boolean autoReconnect)
Creates a new instance using the specified parameters and
Channel.STANDARD_DEVICE_PORT. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
addEventListener(CastEvent.CastEventListener listener,
CastEvent.CastEventType... eventTypes)
Registers the specified
CastEvent.CastEventListener for the specified
CastEvent.CastEventTypes. |
protected Channel |
channel()
|
boolean |
connect()
Establishes a connection to the remote cast device on the associated
Channel. |
protected static ExecutorService |
createExecutor() |
void |
disconnect()
|
boolean |
equals(Object obj) |
protected String |
generateDisplayName()
Tries to generate a suitable "display name" from the available device
information.
|
InetAddress |
getAddress() |
Set<CastDeviceCapability> |
getCapabilities() |
String |
getDeviceURL() |
String |
getDisplayName() |
String |
getDNSName() |
String |
getFriendlyName() |
String |
getHostname() |
String |
getIconPath() |
String |
getModelName()
Returns the model name of the device.
|
int |
getPort() |
int |
getProtocolVersion() |
ReceiverStatus |
getReceiverStatus()
Requests a status from the cast device and returns the resulting
ReceiverStatus if one is obtained, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout. |
ReceiverStatus |
getReceiverStatus(long responseTimeout)
Requests a status from the cast device and returns the resulting
ReceiverStatus if one is obtained. |
Application |
getRunningApplication()
This is a convenience method that calls
getReceiverStatus() and
then ReceiverStatus.getRunningApplication(). |
String |
getServiceName() |
InetSocketAddress |
getSocketAddress() |
String |
getUniqueId() |
int |
hashCode() |
boolean |
isApplicationAvailable(String applicationId)
Queries the cast device if the application represented by the specified
application ID is available, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
boolean |
isApplicationRunning(String applicationId)
This is a convenience method that calls
getReceiverStatus() and
then compares the specified application ID with the result of
ReceiverStatus.getRunningApplication(). |
boolean |
isAutoReconnect()
Returns whether or not an attempt will be made to connect to the cast
device on demand.
|
boolean |
isConnected() |
ReceiverStatus |
launch(String applicationId,
boolean synchronous,
long responseTimeout)
Asks the cast device to launch the application represented by the
specified application ID.
|
ReceiverStatus |
launchApplication(String applicationId,
boolean synchronous)
Asks the cast device to launch the application represented by the
specified application ID, using
Channel.DEFAULT_RESPONSE_TIMEOUT
as the timeout value. |
boolean |
removeEventListener(CastEvent.CastEventListener listener)
Unregisters the specified
CastEvent.CastEventListener. |
<T extends Response> |
sendGenericRequest(String sourceId,
String destinationId,
String namespace,
Request request,
Class<T> responseClass)
Sends the specified
Request with the specified namespace using
the specified source and destination IDs and
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
<T extends Response> |
sendGenericRequest(String sourceId,
String destinationId,
String namespace,
Request request,
Class<T> responseClass,
long responseTimeout)
Sends the specified
Request with the specified namespace using
the specified source and destination IDs. |
void |
setMuteState(boolean muteState)
Sets the mute state for the cast device.This method will create a
one-time
Volume instance, so if both mute and volume level should
be changed, it's better to use setVolume(Volume). |
void |
setVolume(Volume volume)
Sets the
Volume for the cast device. |
void |
setVolumeLevel(double level)
Sets the volume level for the cast device to the specified volume level
(the value must be in the range 0-1).
|
Session |
startSession(String sourceId,
Application application)
Establishes a
Session with the specified Application
unless one already exists, in which case the existing Session is
returned. |
Session |
startSession(String sourceId,
Application application,
String userAgent,
VirtualConnectionType connectionType)
Establishes a
Session with the specified Application
unless one already exists, in which case the existing Session is
returned. |
ReceiverStatus |
stopApplication(Application application,
boolean synchronous)
Asks the cast device to stop the specified
Application, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. |
ReceiverStatus |
stopApplication(Application application,
boolean synchronous,
long responseTimeout)
Asks the cast device to stop the specified
Application. |
String |
toString() |
public static final String SERVICE_TYPE
public static final String DEFAULT_MEDIA_RECEIVER_APP_ID
@Nonnull protected static final ExecutorService EXECUTOR
ExecutorService that is used for asynchronous operations@Nonnull protected final CastEvent.CastEventListenerList listeners
CastEvent.CastEventListeners@Nonnull protected final InetSocketAddress socketAddress
@Nonnull protected final Set<CastDeviceCapability> capabilities
Set of CastDeviceCapabilitys for the cast deviceprotected final int protocolVersion
@Nonnull protected final String displayName
@Nonnull protected final Channel channel
Channel associated with this CastDeviceprotected final boolean autoReconnect
Channel reconnection "on demand" is enabledpublic CastDevice(@Nonnull javax.jmdns.JmDNS mDNS, @Nonnull String dnsName, boolean autoReconnect)
JmDNS instance using the specified DNS name.mDNS - the JmDNS instance.dnsName - the DNS name.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.NullPointerException - if mDNS is null.public CastDevice(@Nonnull javax.jmdns.ServiceInfo serviceInfo, boolean autoReconnect)
ServiceInfo.serviceInfo - the ServiceInfo instance to extract the device
information from. It must be fully resolved.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.NullPointerException - if serviceInfo is null.public CastDevice(@Nonnull String dnsName, @Nonnull String hostname, @Nullable String deviceURL, @Nullable String serviceName, @Nullable String uniqueId, @Nullable Set<CastDeviceCapability> capabilities, @Nullable String friendlyName, @Nullable String modelName, int protocolVersion, @Nullable String iconPath, boolean autoReconnect)
Channel.STANDARD_DEVICE_PORT.dnsName - the DNS name used by the cast device.hostname - the hostname/address of the cast device.deviceURL - the "base URL" of the cast device.serviceName - the DNS-SD service name, usually "googlecast".uniqueId - the unique ID of the cast device.capabilities - the Set of CastDeviceCapabilitys that
applies for the cast device.friendlyName - the "friendly name" of the cast device.modelName - the model name of the cast device.protocolVersion - the protocol version supported by the cast device.iconPath - the (device URL) relative path to its icon.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.IllegalArgumentException - If dnsName is blank or if
hostname is null.public CastDevice(@Nonnull String dnsName, @Nonnull String hostname, int port, @Nullable String deviceURL, @Nullable String serviceName, @Nullable String uniqueId, @Nullable Set<CastDeviceCapability> capabilities, @Nullable String friendlyName, @Nullable String modelName, int protocolVersion, @Nullable String iconPath, boolean autoReconnect)
dnsName - the DNS name used by the cast device.hostname - the hostname/address of the cast device.port - the port the cast device is listening to.deviceURL - the "base URL" of the cast device.serviceName - the DNS-SD service name, usually "googlecast".uniqueId - the unique ID of the casst device.capabilities - the Set of CastDeviceCapabilitys that
applies for the cast device.friendlyName - the "friendly name" of the cast device.modelName - the model name of the cast device.protocolVersion - the protocol version supported by the cast device.iconPath - the (device URL) relative path to its icon.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.IllegalArgumentException - If dnsName is blank, if
port is outside the range of valid port values or if
hostname is null.public CastDevice(@Nonnull String dnsName, @Nonnull InetAddress address, @Nullable String deviceURL, @Nullable String serviceName, @Nullable String uniqueId, @Nullable Set<CastDeviceCapability> capabilities, @Nullable String friendlyName, @Nullable String modelName, int protocolVersion, @Nullable String iconPath, boolean autoReconnect)
Channel.STANDARD_DEVICE_PORT.dnsName - the DNS name used by the cast device.address - the IP address of the cast device.deviceURL - the "base URL" of the cast device.serviceName - the DNS-SD service name, usually "googlecast".uniqueId - the unique ID of the casst device.capabilities - the Set of CastDeviceCapabilitys that
applies for the cast device.friendlyName - the "friendly name" of the cast device.modelName - the model name of the cast device.protocolVersion - the protocol version supported by the cast device.iconPath - the (device URL) relative path to its icon.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.IllegalArgumentException - If dnsName is blank, if
port is outside the range of valid port values or if
address is null.public CastDevice(@Nonnull String dnsName, @Nonnull InetAddress address, int port, @Nullable String deviceURL, @Nullable String serviceName, @Nullable String uniqueId, @Nullable Set<CastDeviceCapability> capabilities, @Nullable String friendlyName, @Nullable String modelName, int protocolVersion, @Nullable String iconPath, boolean autoReconnect)
dnsName - the DNS name used by the cast device.address - the IP address of the cast device.port - the port the cast device is listening to.deviceURL - the "base URL" of the cast device.serviceName - the DNS-SD service name, usually "googlecast".uniqueId - the unique ID of the cast device.capabilities - the Set of CastDeviceCapabilitys that
applies for the cast device.friendlyName - the "friendly name" of the cast device.modelName - the model name of the cast device.protocolVersion - the protocol version supported by the cast device.iconPath - the (device URL) relative path to its icon.autoReconnect - true to try to automatically reconnect "on
demand", false to handle connection "manually" by
listening to CONNECTED events.IllegalArgumentException - If dnsName is blank, if
port is outside the range of valid port values or if
address is null.@Nonnull public String getDNSName()
Chromecast-e28835678bc02247abcdef112341278f.@Nullable public InetAddress getAddress()
null, but there's a small chance that it can, if this
CastDevice was created using a hostname that could not be
resolved to an IP address.public String getHostname()
CastDevice was created.public int getPort()
@Nonnull public InetSocketAddress getSocketAddress()
InetSocketAddress.@Nullable public String getServiceName()
DNS-SD service name, usually "googlecast".@Nullable public String getFriendlyName()
@Nullable public String getModelName()
ChromecastChromecast AudioChromecast UltraGoogle HomeGoogle Cast GroupSHIELD Android TV@Nonnull public Set<CastDeviceCapability> getCapabilities()
Set of CastDeviceCapability announced by the
cast device.@Nullable public String getUniqueId()
public int getProtocolVersion()
public String getIconPath()
public String getDisplayName()
@Nonnull protected Channel channel() throws IOException
Channel if it is open or if
autoReconnect is true and reconnect succeeds.
If the Channel isn't open and autoReconnect is
false or reconnection fails, an IOException is thrown.
Channel.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If an error occurs during reconnect.public boolean connect()
throws IOException,
KeyManagementException,
NoSuchAlgorithmException
Channel.true if a connection was established, false if
there was no need.KeyManagementException - If there's a problem with key management
that prevents connection.NoSuchAlgorithmException - If the required cryptographic algorithm
isn't available in the JVM.CastException - If there was an authentication problem with the
cast device.IOException - If an error occurs during the operation.public void disconnect()
throws IOException
Channel and any Sessions
belonging to it. If the Channel is already closed, this is a
no-op.IOException - If an error occurs during the operation.public boolean isConnected()
true if the associated Channel is closed,
false if it's open.public boolean isAutoReconnect()
true if automatic connect is active, false
otherwise if this must be handled manually.@Nullable public ReceiverStatus getReceiverStatus() throws IOException
ReceiverStatus if one is obtained, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout.
This is a blocking call that waits for the response or times out.
ReceiverStatus.IOException - If the response times out or an error occurs during
the operation.@Nullable public ReceiverStatus getReceiverStatus(long responseTimeout) throws IOException
ReceiverStatus if one is obtained.
This is a blocking call that waits for the response or times out.
responseTimeout - the response timeout in milliseconds. If zero or
negative, Channel.DEFAULT_RESPONSE_TIMEOUT will be
used.ReceiverStatus.IOException - If the response times out or an error occurs during
the operation.@Nullable public Application getRunningApplication() throws IOException
getReceiverStatus() and
then ReceiverStatus.getRunningApplication().
This is a blocking call that waits for the response or times out.
Application describing the current running
application, if any, or null.IOException - If the response times out or an error occurs during
the operation.public boolean isApplicationAvailable(String applicationId) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.applicationId - the application ID for which to query availability.true if the application is available, false if
it's not.IOException - If the response times out or if an error occurs
during the operation.public boolean isApplicationRunning(String applicationId) throws IOException
getReceiverStatus() and
then compares the specified application ID with the result of
ReceiverStatus.getRunningApplication().
This is a blocking call that waits for the response or times out.
applicationId - application ID to check if is the "currently running
application".true if application with specified identifier is
"currently running", false otherwise.IOException - If the response times out or an error occurs during
the operation.@Nullable public ReceiverStatus launchApplication(String applicationId, boolean synchronous) throws IOException
Channel.DEFAULT_RESPONSE_TIMEOUT
as the timeout value.applicationId - the application ID for the application to launch.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.ReceiverStatus or null if
synchronous is false.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public ReceiverStatus launch(String applicationId, boolean synchronous, long responseTimeout) throws IOException
applicationId - the application ID for the application to launch.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
Channel.DEFAULT_RESPONSE_TIMEOUT will be used.ReceiverStatus or null if
synchronous is false.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the response times out or an error occurs during
the operation.@Nullable public ReceiverStatus stopApplication(@Nullable Application application, boolean synchronous) throws IOException
Application, using
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value.application - the Application to stop.synchronous - if true, the method will block and wait for a
response which will be returned. If false, the method
will not block and null will always be returned.ReceiverStatus if synchronous is
true and one is returned from the cast device.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If an error occurs during the operation.@Nullable public ReceiverStatus stopApplication(@Nonnull Application application, boolean synchronous, long responseTimeout) throws IOException
Application.application - the Application to stop.synchronous - true to make this call block until a response
is received or times out, false to make it return
immediately always returning null.responseTimeout - the response timeout in milliseconds if
synchronous is true. If zero or negative,
Channel.DEFAULT_RESPONSE_TIMEOUT will be used.ReceiverStatus or null if
synchronous is false.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the response times out or an error occurs during
the operation.public Session startSession(@Nonnull String sourceId, @Nonnull Application application) throws IOException
Session with the specified Application
unless one already exists, in which case the existing Session is
returned.sourceId - the source ID to use.application - the Application to connect to.Session.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If an error occurs during the operation.public Session startSession(@Nonnull String sourceId, @Nonnull Application application, @Nullable String userAgent, @Nonnull VirtualConnectionType connectionType) throws IOException
Session with the specified Application
unless one already exists, in which case the existing Session is
returned.sourceId - the source ID to use.application - the Application to connect to.userAgent - the user-agent String or null. It's not entirely
clear what this is used for other than reporting to Google, so
it might be that it's better left null.connectionType - The VirtualConnectionType to use. Please
note that only VirtualConnectionType.STRONG and
VirtualConnectionType.INVISIBLE are allowed.Session.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If an error occurs during the operation.public void setVolumeLevel(double level)
throws IOException
Volume instance, so if both mute and volume level should be
changed, it's better to use setVolume(Volume).
If the cast device has Volume.VolumeControlType.MASTER and the volume
level changes more than that of the device specified "step interval", a
Timer that will adjust the volume gradually until it reaches the
target level will be started.
level - the new volume level.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the cast device has
Volume.VolumeControlType.FIXED or an error occurs during the
operation.public void setMuteState(boolean muteState)
throws IOException
Volume instance, so if both mute and volume level should
be changed, it's better to use setVolume(Volume).muteState - true to set muted state, false to set
unmuted state.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the cast device has
Volume.VolumeControlType.FIXED or an error occurs during the
operation.@Nullable public void setVolume(@Nullable Volume volume) throws IOException
Volume for the cast device. The Volume instance
can contain both the volume level and the mute state, so both can be set
using at once.
The Volume instance can be created with only field(s) that should
be changed set.
If the cast device has Volume.VolumeControlType.MASTER and the volume
level changes more than that of the device specified "step interval", a
Timer that will adjust the volume gradually until it reaches the
target level will be started.
volume - the Volume to set.SocketException - If the Channel is closed and
autoReconnect is false.IOException - If the cast device has
Volume.VolumeControlType.FIXED or an error occurs during the
operation.public <T extends Response> T sendGenericRequest(@Nonnull String sourceId, @Nonnull String destinationId, @Nonnull String namespace, Request request, Class<T> responseClass) throws IOException
Request with the specified namespace using
the specified source and destination IDs and
Channel.DEFAULT_RESPONSE_TIMEOUT as the timeout value. This is
for requests that aren't associated with a Session.T - the class of the Response object.sourceId - the source ID to use.destinationId - the destination ID to use.namespace - the namespace to use.request - the Request to send.responseClass - the response class to to block and wait for a
response or null to return immediately.Response if the response is received in time, or
null if the responseClass is null or a
timeout occurs.IllegalArgumentException - If namespace is null or
invalid (see Channel.validateNamespace(String) for
constraints).IOException - If an error occurs during the operation.public <T extends Response> T sendGenericRequest(@Nonnull String sourceId, @Nonnull String destinationId, @Nonnull String namespace, Request request, Class<T> responseClass, long responseTimeout) throws IOException
Request with the specified namespace using
the specified source and destination IDs. This is for requests that
aren't associated with a Session.T - the class of the Response object.sourceId - the source ID to use.destinationId - the destination ID to use.namespace - the namespace to use.request - the Request to send.responseClass - the response class to to block and wait for a
response or null to return immediately.responseTimeout - the response timeout in milliseconds if
responseClass is non-null. If zero or
negative, Channel.DEFAULT_RESPONSE_TIMEOUT will be
used.Response if the response is received in time, or
null if the responseClass is null or a
timeout occurs.IllegalArgumentException - If namespace is null or
invalid (see Channel.validateNamespace(String) for
constraints).IOException - If an error occurs during the operation.public boolean addEventListener(@Nullable CastEvent.CastEventListener listener, CastEvent.CastEventType... eventTypes)
CastEvent.CastEventListener for the specified
CastEvent.CastEventTypes.listener - the CastEvent.CastEventListener to register.eventTypes - the event type(s) to listen to.true if the listener was registered, false if it
already was registered.public boolean removeEventListener(@Nullable CastEvent.CastEventListener listener)
CastEvent.CastEventListener.listener - the CastEvent.CastEventListener to unregister.true if the listener was unregistered, false it
wasn't registered to begin with.protected static ExecutorService createExecutor()
ExecutorService.Copyright © 2022. All rights reserved.