Menu

DiscoveryAgent

The DiscoveryAgent is responsible to broadcast and receive network information from the devices connected to the same sub-network. Session participants should register to SessionInfoAnnouncedListener to get the callbacks on session announce information. The SessionAnnounceInfo contains session and network connection information.

The DiscoveryAgent provides the following discovery methods:

  • Wi-Fi multicast: Fast and simple to announce, and receive session information.
  • BLE: Device can advertise and scan session information through BLE.

The following is the list of DiscoveryAgent API methods:

API Description
setDiscoveryMethod(DiscoveryMethod discoveryMethod) Sets one discovery method like Wi-Fi multicast or BLE
startAnnouncingSession(SessionAnnounceInfo sessionInfo) Session master starts broadcasting the session information
stopAnnouncingSession() Stops announcing the session information
setAnnounceSessionInfoListener(SessionInfoAnnouncedListener listener) Sets the SessionInfoAnnouncedListener to listen for session information
startReceivingSession() Session participants start receiving session information
stopReceivingSession() Session participants stop receiving session information
getAnnouncedSessionInfo() Get the latest announced session list
isAnnouncingSessionInfo() Checks whether master is announcing the session
isReceivingSessionInfo() Checks whether participant is receiving the session

Permissions required

  • Wi-Fi multicast: android.permission.CHANGE_WIFI_MULTICAST_STATE
  • BLE : android.permission.BLUETOOTH, android.permission.BLUETOOTH_ADMIN

The following is a sample code for sending course information:

AgentManager agentManager = AgentManager.getInstance();
DiscoveryAgent discoveryAgent = (DiscoveryAgent)
agentManager.getAgent(DiscoveryAgent.class);
SessionAnnounceInfo sessionInfo = new SessionAnnounceInfo();
sessionInfo.sessionName = courseNameEdit.getText().toString();

try {
 discoveryAgent.setDiscoveryMethod(DiscoveryMethod.DISCOVERY_ON_WIFI);
 discoveryAgent.startAnnouncingSession(sessionInfo);
} catch (Exception e) {
 e.printStackTrace();
}