| Reply | « Previous Thread | Next Thread » |
|
Hello
I am developping an application which look for latitude and longitude by using GPS.If the device can not find GPS then I want it to look for bluetooth devices.I can find latitude and longitude by using GPS without any problem.Unfortunately, I am not able to look for bluetooth devices if I cant find any GPS. I found some codes to look for bluetooth devics but it is a whole class.You know a function or a method which is used to look for bluetooth devices? I will be appreciated if you can help me. Best Regards Buyuk Altayli |
|
Hello,
I've not got why you can't find bluetooth devices when you don't have latitude/longitude. I want to say that they are independent. Code:
//gets local bluetooth device reference LocalDevice localDevice = LocalDevice.getLocalDevice(); //an agent to discover bluetooth devices and services DiscoveryAgent agent = this.localDevice.getDiscoveryAgent(); //start searching bluetooth devices //discoveryListener implements the DiscoveryListener interface agent.startInquiry(DiscoveryAgent.GIAC, discoveryListener); Don't forget to turn the bluetooth connection on at you device's settings. |
| thiagobrunoms |
| View Public Profile |
| Find all posts by thiagobrunoms |
|
Hello thiagobrunoms
I used the following code but unfortunately my mobile phone is not able to look for any devices by using bluetooth.I do not know what I am doing wrong.Could you please check the code and tell me where I am doing wrong? Thank you in advance. Regards Buyuk Altayli Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
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;
import javax.microedition.midlet.*;
import java.util.Vector;
// bluetooth classes
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
/**
* @author user
*/
public class Midlet extends MIDlet {
public class BTManager implements DiscoveryListener {
public static final int BLUETOOTH_TIMEOUT = 30000;
public Vector btDevicesFound;
public Vector btServicesFound;
private boolean isBTSearchComplete;
BTManager btManager = null;
BTManager instance() {
if (btManager == null) {
btManager = new BTManager();
}
return btManager;
}
/** Creates a new instance of BTManager */
private BTManager() {
btDevicesFound = new Vector();
btServicesFound = new Vector();
}
public UUID[] getRFCOMM_UUID() {
System.out.println("getRFCOMM_UUID içerisindeyim");
UUID[] uuidSet;
UUID RFCOMM_UUID = new UUID(0x1101); // RFCOMM service
uuidSet = new UUID[1];
uuidSet[0] = RFCOMM_UUID;
return uuidSet;
}
/**
* Finds bluetooth devices
*
* @param aServices,
* an array with the service UUID identifiers you want to search
* @returns the number of devices found
*/
public int find(UUID[] aServices) {
System.out.println("find UUID içerisindeyim");
findDevices();
return btDevicesFound.size();
}
public int findDevices() {
System.out.println("findDevices() içerisindeyim");
try {
// cleans previous elements
btDevicesFound.removeAllElements();
isBTSearchComplete = false;
LocalDevice local = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = local.getDiscoveryAgent();
// discover new devices
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
while ((!isBTSearchComplete)) {
// waits for a fixed time, to avoid long search
synchronized (this) {
this.wait(BTManager.BLUETOOTH_TIMEOUT);
}
// check if search is completed
if (!isBTSearchComplete) {
// search no yet completed so let's cancel it
discoveryAgent.cancelInquiry(this);
}
}
System.out.println("try icerisindeyim");
} catch (Exception e) {
System.out.println("catch icerisindeyim");
e.printStackTrace();
}
return btDevicesFound.size();
}
public void servicesDiscovered(int param, ServiceRecord[] serviceRecord) {
int index = btServicesFound.size() - 1;
for (int i = 0; i < serviceRecord.length; i++) {
btServicesFound.setElementAt(serviceRecord[i], index);
}
}
public void serviceSearchCompleted(int transID, int respCode) {
isBTSearchComplete = true;
// notifies and wake mains thread that service search is completed
synchronized (this) {
this.notify();
}
}
/**
* Get a human readable name of the BT device.
*
* @param deviceID
* @return the friendly name for the BT device
*/
public String getDeviceName(int deviceID) {
try {
return ((RemoteDevice) btDevicesFound.elementAt(deviceID))
.getFriendlyName(false);
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}
/**
* Gets the URL address of the the service you want to connect
*
* @param deviceID
* @return the Url address for the device and service found
*/
public String getServiceURL(int deviceID) {
try {
return ((ServiceRecord) btServicesFound.elementAt(deviceID))
.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
} catch (Exception e) {
e.printStackTrace();
}
return "Error";
}
public void deviceDiscovered(RemoteDevice remoteDevice,
DeviceClass deviceClass) {
btDevicesFound.addElement(remoteDevice);
}
public void inquiryCompleted(int param) {
isBTSearchComplete = true;
// notifies and wake main thread that device search is completed
synchronized (this) {
this.notify();
}
}
}
public void startApp() {
getForm1A();
}
public void getForm1A(){
Display.init(this);
Form a1=new Form();
BTManager bt=new BTManager();
bt.findDevices();
a1.show();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
|
|
while checking on real device did you manually turn on the bluettoth bcoz in java one cannot open the bluetooth by code
Regards, Saurabh |
| ingsaurabh |
| View Public Profile |
| Find all posts by ingsaurabh |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| Listbox Search | patil_ruturaj | General Symbian C++ | 1 | 2008-08-25 08:02 |
| Problem with BT devices search on mobile phone | atiqkamran | Mobile Java Networking & Messaging & Security | 2 | 2007-07-24 07:05 |
| Alerting on BT devices out of range or off | jameelh | Bluetooth Technology | 8 | 2006-04-07 16:44 |
| Error During Search for Bluetooth devices... | sblider | General Symbian C++ | 1 | 2004-02-22 18:42 |
| Bluetooth search devices CODE!? | sblider | General Symbian C++ | 0 | 2003-05-20 21:58 |