Implementation of Node Discovery in Java: Full Code in Java

Implementation of Node Discovery in Java: Full Code in Java
Implementation of Node Discovery in Java: Full Code in Java

Agent Discovery:
  1. It is a method by which a mobile network concludes whether it is currently connected to a foreign network or a home network.
  2. Also through this method, a mobile node can identify when it has moved from one network to another.
  3. The method allows determining the foreign care-of address being offered by each foreign agent on that network when connected to a foreign network.


Code:

Device 1 & 2 Code:

package mypackage;

import java.io.*;

import javax.microedition.midlet.*;

import javax.microedition. lcdui.*;

import javax.microedition. io.*;

import javax.bluetooth.*;

import java.util.*;

public class Blue extends MIDlet implements

CommandListener,DiscoveryListener
{
  
private List activeDevices;

private Command select,exit;

private Display display;

private LocalDevice local=null;

private DiscoveryAgent agent = null;

private Vector devicesFound = null;

private ServiceRecord[] servicesFound = null;

private String connectionURL = null;

public void startApp() {

display = Display.getDisplay(this);

activeDevices = new

List("Active Devices", List.IMPLICIT); select = new
Command("Search Again",  Command.OK, 0);

exit = new Command("Exit",

Command.EXIT, 0);

activeDevices.addCommand(exit);

activeDevices.setCommandListener(this); try {

local = LocalDevice.getLocalDevice();  }

catch (Exception e) {}

doDeviceDiscovery();

display.setCurrent(activeDevices);

}

public void pauseApp() {}

public void destroyApp(boolean

unconditional) { notifyDestroyed(); }

public void commandAction(Command cmd, Displayable disp) {

if (cmd == select && disp == activeDevices) {

activeDevices.deleteAll();

doDeviceDiscovery();

}

if (cmd == exit) {

destroyApp(false); }

}

public void inquiryCompleted(int param) { try {

switch (param) {

case

DiscoveryListener.INQUIRY_COMPLETED:

if

(devicesFound.size() > 0) {

activeDevices.addCommand(select);

activeDevices.setSelectCommand(select);

}

else {

activeDevices.append("No Devices Found", null); } break;

}

}

catch (Exception e) {}

}

public void serviceSearchCompleted(int transID, int respCode) {}

public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {}

public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {

String str = null;

try {

str = remoteDevice.getBluetoothAddress()  + " - ";

str += remoteDevice.getFriendlyName(true);

} catch (Exception e) {} activeDevices.append(str, null);

devicesFound.addElement(remoteDevice);

try {

if (!agent.startInquiry(DiscoveryAgent.GIAC, this)) {}
} catch (BluetoothStateException  e) {

// TODO Auto-generated  catch block

e.printStackTrace();

}
}

private void doDeviceDiscovery() {

try {
local = LocalDevice.getLocalDevice();
agent = local.getDiscoveryAgent();
devicesFound = new Vector();
} catch (Exception e) {}
}
}

Main Device:


package mypackage;

import java.io.IOException;
import javax.bluetooth.*;

import javax.bluetooth.DiscoveryListener; import javax.microedition. lcdui.*; import javax.microedition.midlet.*;

public class discover_device extends MIDlet implements CommandListener,DiscoveryListener {

private final List deviceList;

private final Command Exit,Refresh; private String deviceName; private DiscoveryAgent agent; private Alert dialog;

public discover_device()
{
deviceList = new List("List of
Devices",List.IMPLICIT);
Exit= new Command("Exit",Command.EXIT,  0);
Refresh = new
Command("Refresh",Command.SCREEN,  1);

deviceList.addCommand(Exit); deviceList.addCommand(Refresh);

deviceList.setCommandListener(this); Display.getDisplay(this).setCurrent(deviceList);
}

public void startApp() {
try {
deviceList.deleteAll();

LocalDevice local = LocalDevice.getLocalDevice(); local.setDiscoverable(DiscoveryAgent.GIAC); deviceName = local.getFriendlyName();

agent = local.getDiscoveryAgent(); }

catch (BluetoothStateException ex) { ex.printStackTrace(); }

try {
agent.startInquiry(DiscoveryAgent.GIAC,  this);

}

catch (BluetoothStateException ex) { ex.printStackTrace(); }

}

public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}







public void commandAction(Command  c, Displayable
d)  {
if(c==Exit)
{
this.destroyApp(true);

notifyDestroyed();
}
if(c==Refresh){
this.startApp();
}
}

public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { String deviceaddress = null;
try {
deviceaddress =

btDevice.getBluetoothAddress();//btDevice. getFriendl
yName(true);

} catch (Exception ex) { ex.printStackTrace();
}
deviceList.insert(0,  deviceaddress , null);
}

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
throw new UnsupportedOperationException("Not
supported yet.");
}

public void serviceSearchCompleted(int transID, int respCode) {

throw new UnsupportedOperationException("Not supported yet.");
}





public void inquiryCompleted(int discType) { Alert dialog = null;
if (discType !=
DiscoveryListener.INQUIRY_COMPLETED) {

dialog = new Alert("Bluetooth Error","The inquiry failed to complete normally",null, AlertType.ERROR);

}
else {

dialog = new Alert("Inquiry Completed","The inquiry completed normally", null,AlertType.INFO);
}
dialog.setTimeout(500);


Display.getDisplay(this).setCurrent(dialog);                          }
}






Post a Comment

0 Comments