A Simple Asynchronous SSDP/1.0 UPNP/1.1 Java Client using JDK APIs only.
This library works on Android as well.
Jar Download:
https://mvnrepository.com/artifact/io.resourcepool/ssdp-client
Maven:
<!-- https://mvnrepository.com/artifact/io.resourcepool/ssdp-client -->
<dependency>
<groupId>io.resourcepool</groupId>
<artifactId>ssdp-client</artifactId>
<version>2.5.1</version>
</dependency>
Gradle:
compile 'io.resourcepool:ssdp-client:2.5.1'
Discover all SSDP services:
SsdpClient client = SsdpClient.create();
DiscoveryRequest all = SsdpRequest.discoverAll();
client.discoverServices(all, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Discover specific SSDP service by serviceType (simple version):
SsdpClient client = SsdpClient.create();
DiscoveryRequest networkStorageDevice = SsdpRequest.builder()
.serviceType("urn:schemas-upnp-org:device:networkstoragedevice:1")
.build();
client.discoverServices(networkStorageDevice, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Discover specific SSDP service by serviceType (with custom options):
SsdpClient client = SsdpClient.create();
DiscoveryOptions options = DiscoveryOptions.builder()
.intervalBetweenRequests(10000L) // optional interval between requests, defaults to 10 000 milliseconds
.maxWaitTimeSeconds(3) // optional max wait time between req and response, defaults to 3 seconds
.userAgent("Resourcepool SSDP Client") // optional custom user-agent, defaults to "Resourcepool SSDP Client"
.build();
DiscoveryRequest networkStorageDevice = SsdpRequest.builder()
.serviceType("urn:schemas-upnp-org:device:networkstoragedevice:1")
.discoveryOptions(options) // optional as well.
.build();
client.discoverServices(networkStorageDevice, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Discover specific SSDP service by serviceType (with custom options):
SsdpClient client = SsdpClient.create();
DiscoveryOptions options = DiscoveryOptions.builder()
.intervalBetweenRequests(10000L) // optional interval between requests, defaults to 10 000 milliseconds
.maxWaitTimeSeconds(3) // optional max wait time between req and response, defaults to 3 seconds
.userAgent("Resourcepool SSDP Client") // optional custom user-agent, defaults to "Resourcepool SSDP Client"
.build();
DiscoveryRequest networkStorageDevice = SsdpRequest.builder()
.serviceType("urn:schemas-upnp-org:device:networkstoragedevice:1")
.discoveryOptions(options) // optional as well.
.build();
client.discoverServices(networkStorageDevice, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Discovery is not a mandatory activity. You might just want to listen to announcements:
SsdpClient client = SsdpClient.create();
client.discoverServices(null, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Disabling automatic lookup for unknown incoming announcements
SsdpClient client = SsdpClient.create();
DiscoveryRequest all = SsdpRequest.discoverAll();
DiscoveryOptions options = DiscoveryOptions.builder()
.intervalBetweenRequests(10000L) // optional interval between requests, defaults to 10 000 milliseconds
.maxWaitTimeSeconds(3) // optional max wait time between req and response, defaults to 3 seconds
.userAgent("Resourcepool SSDP Client") // optional custom user-agent, defaults to "Resourcepool SSDP Client"
.overrideBindingPort(1901)
.build();
client.discoverServices(all, options, new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Ignoring unavailable Multicast Interface errors
SsdpClient client = SsdpClient.create();
DiscoveryRequest all = SsdpRequest.discoverAll();
client.discoverServices(all, SsdpClientOptions.builder().ignoreInterfaceDiscoveryErrors().build(), new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
Overriding default binding port (defaults to 1900)
SsdpClient client = SsdpClient.create();
DiscoveryRequest all = SsdpRequest.discoverAll();
client.discoverServices(all, SsdpClientOptions.builder().ignoreInterfaceDiscoveryErrors().build(), new DiscoveryListener() {
@Override
public void onServiceDiscovered(SsdpService service) {
System.out.println("Found service: " + service);
}
@Override
public void onServiceAnnouncement(SsdpServiceAnnouncement announcement) {
System.out.println("Service announced something: " + announcement);
}
});
When you're done, don't forget to stop the discovery:
ssdpClient.stopDiscovery();
Copyright 2016-2022 Resourcepool
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.