Bluetooth device detection
Hello,
My current project is based on Bluetooth beacon detection. when the mobile phone detects an hotspot (bluetooth spot), it gets some information from my server. I created a background thread which continuously scans for Bluetooth beacons: Here is my code:
public void run() {
lock = new Object();
while (!done) {
doSearch();
try {
thread.sleep(DISCOVERY_LATENCY); //2000ms
} catch (Throwable t) {
}
}
}
/**
* Start a new Device discovery
*
*/
synchronized public void doSearch() {
try {
//get local bluetooth device
local = LocalDevice.getLocalDevice();
//get discovery agent
agent = local.getDiscoveryAgent();
//start a new Bluetooth discovery inquiry
agent.startInquiry(DiscoveryAgent.GIAC,this);
//we lock the thread and wait the end of the current inquiry
synchronized(lock) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
} catch (Exception e) {
}
}
/**
*We detected one bluetooth devices
*/
public void deviceDiscovered(javax.bluetooth.RemoteDevice remoteDevice, javax.bluetooth.DeviceClass deviceClass) {
try {
// get the bluetooth MAC address of that device and remove any white space
String value = remoteDevice.getBluetoothAddress().trim();
if(checkTable(value) && doneDetection == false)
{
doneDetection = true;
launchHotSpotPage("test");
}
} catch (Exception e) {
System.err.println("BLUETOOH ERROR");
}
}
/**
* The bluetooth discovery inquiry has been completed
*
*/
public void inquiryCompleted(int discType) {
local = null;
agent = null;
//We unlock the thread in order to make a new discovery
synchronized(lock) {
try {
lock.notify();
} catch (Exception e) {
System.err.println("BLUETOOH ERROR");
lock = null;
lock = new Object();
}
}
}
If the mobile detects a bluetooth device, it will get its MAc address and check our bluetooth hotspot mac address table, to see if it is a beacon. If it detects successfully, it will display my HelloWorld Form page and set the doneDetection flag to true. At the moment it will detect a spot only once.
here is my problem:
I noticed that on Nokia devices, the mobile phone keeps detecting previously discovered bluetooth devices, even if that devices is switched off or out of range. I guess there is some kind of cache which is not flushed properly. Is there a trick to prevent this ? I just want to detect my beacons when in range.
thanks a lot
Seb

