For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > [Bluetooth/Java J2SE] Devices available (permanent checking)???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author [Bluetooth/Java J2SE] Devices available (permanent checking)???
Sarah Klage

2005-02-23, 4:02 pm

Hello,

I'd like to know if it's possible to perform a check with Java if a device
is still available a few seconds after I did a complete scanning for
bluetooth devices.

In other words: I know how to discover bluetooth Devices with J2SE (via
javax.bluetooth package). I can put all devices that has been discovered in
a container (e.g. a linkedlist) and print their attributes (name, address,
hashcode, ...).

But now I want to perform a check every X seconds if the discovered
Bluetooth Devices are still within reach of my local device. Something like
a ping or whatever. My first thought was just to request permanently e.g.
the name of the device and as soon as I get an exception or an empty string
I could assume that the device is not available any more.

Unfortunately a command like
"myDiscoveredRemoteDevice.getFriendlyName(true);" also gives the right name
back if the device is not really available any more!

So, please, can anyone help me and give me a hint how to perform a check if
a bluetooth device is still within reach.

Thanks in advance
Sarah

P.S: Here a simple example of a class which scans the environment for
bluetooth devices and prints the bluetooth Address of the Devices
discovered. Feel free to modify so that the class can do a permanent check
if the discovered devices are still within reach :-)

import java.util.LinkedList;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

public class PermanentChecking {

/*
* Definition of global variables
*/
// The local bluetooth Device
private LocalDevice locDevice = null;
// Necessary for Remote Device discovery
private DiscoveryAgent agent = null;
// List of discovered RemoteDevices
private static LinkedList devices = new LinkedList();


/*
* Main Method, creates instance of this class
*/
public static void main(String args[]){
PermanentChecking myCheck = new PermanentChecking();
}

/*
* Constructor, calls only the method scanForDevices()
*/
public PermanentChecking(){
scanForDevices();
}

/*
* Scans the environment for BT Devices
*/
public void scanForDevices()
{
try {
// Obtain reference to the local BT Device (USB)
locDevice = LocalDevice.getLocalDevice();
// Assign the DiscoveryAgent to the proper local Device
agent = locDevice.getDiscoveryAgent();
//Start of the bluetooth inquiry.
agent.startInquiry( DiscoveryAgent.GIAC, new Listener() );
} catch (BluetoothStateException e) {
e.printStackTrace();
}
}

/*
* Inner class, implementing the DiscoveryListener Interface
* That's necessary for discovering BT devices.
*/
private static class Listener implements DiscoveryListener{

/*
* This method is always called after the (Discovery-)
* Listener has found some bluetooth Devices
*
*/
public void deviceDiscovered(RemoteDevice remoteDevice,
DeviceClass deviceClass)
{
// store the found device
devices.add(remoteDevice);
// print the Address of the device. Also
// available are "Friendly Name", "Hashcode"
// and some more.
System.out.println(remoteDevice.getBluetoothAddress());
remoteDevice.getFriendlyName(true);
}

/*
* Method is automaticly been called after inquiry is finished
* and all available devices has been discovered via
* 'deviceDiscovered()'. The variabel 'complete' gets
* the value '0' if inquiry was successful.
*/
public void inquiryCompleted(int complete)
{
System.out.println("Done!");
}

public void servicesDiscovered(int transId, ServiceRecord[] records)
{
; // nothing to do
}

public void serviceSearchCompleted(int transId, int complete)
{
; // nothing to do
}
}
}





Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com