You Are Here:

Community: Developer Discussion Boards

#1 Old Unable to detect devices - 2008-02-27, 20:34

Join Date: Feb 2008
Posts: 6
yeezhihao
Offline
Registered User
Hi

The following piece of code for bluetooth detection and connection works(able to detect and connect to other bluetooth devices) on my Sony Ericsson K750i Phone but not on a Nokia N81(unable to detect any bluetooth devices).

Can someone please help, i need this thing urgently.

Many thanks
Mark

package ECG;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.*;
import java.util.Vector;

/**
* Class for handle and establish Bluetooth connection
*/
public class BluetoothConnection extends Thread implements DiscoveryListener
{
private StreamConnection sc;
private DataInputStream input;
private DataOutputStream output;
private BluetoothScreen screen;
private UUID SERIAL = new UUID(0x1101);
private UUID RFCOMM = new UUID(0x0003);
private Vector devices, targetDevices;
private boolean connected;
private StreamConnection con;
private ECGDisplay display;
private int[] transIds;
private boolean inited, inquiry, search;
private int count = 0;
public BluetoothConnection(BluetoothScreen screen, ECGDisplay display)
{
this.screen = screen;
this.display = display;
devices = new Vector();
targetDevices = new Vector();
inited = false;
}

/**
* Method for searching and connect to blutooth device with serial port profile
*/
public boolean searchSerialBT()
{
try
{
DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
String url = agent.selectService(SERIAL, ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
if(url!=null)
{
con = (StreamConnection)Connector.open(url);
input = con.openDataInputStream();
output = con.openDataOutputStream();
output.writeChars("connected");
connected = true;
}
else
connected = false;
}
catch(Exception e)
{
System.out.println(e);
}
return connected;
}
/**
* Method for establish connection with the connection url which identifies the
* address of the bluetooth device
*/
public boolean connect(String conURL)
{
try
{
con = (StreamConnection)Connector.open(conURL);
input = con.openDataInputStream();
output = con.openDataOutputStream();
output.writeChars("connected");
connected = true;
}
catch(Exception e)
{
System.out.println(e);
}
return connected;
}
/**
* Thread method for handle receiving and displaying of ECG data
*/
public void run()
{
while(true)
{
try
{
if(input!=null)
{
byte[] b = new byte[input.available()];
input.read(b);
display.addValues(b);
Thread.sleep(1);
}
}
catch(IOException e)
{
System.out.println(e);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
/**
* Method for disconnecting the Bluetooth connection
*/
public void disconnect()
{
try{
connected = false;
if(input!=null)
input.close();
if(output!=null)
output.close();
if(con!=null)
con.close();
input = null;
output = null;
con = null;
}catch(IOException e)
{
System.out.println(e);
}
}
/**
* Method getting the input stream from the Bluetooth device
*/
public DataInputStream getInput()
{
return input;
}
/**
* Method getting the output stream for writing to the Bluetooth device
*/
public DataOutputStream getOutput()
{
return output;
}
/**
* Event Handler method for logging the neighbouring bluetooth device
*/
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
devices.addElement(btDevice);
}
/**
* method for cancelling the searching of bluetooth devices
*/
public void cancelSearch()
{
try{
DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
if(inquiry)
agent.cancelInquiry(this);
if(search)
{
for(int i=0;i<transIds.length;i++)
agent.cancelServiceSearch(transIds[i]);
}
targetDevices.removeAllElements();
devices.removeAllElements();
}catch(BluetoothStateException e)
{
System.out.println(e);
}
}
/**
* Method for searching the neighbouring bluetooth devices
*/
public void searchDevices()
{
try
{
cancelSearch();
inquiry = true;
DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
agent.startInquiry(DiscoveryAgent.GIAC,this);
}
catch(BluetoothStateException e)
{
System.out.println(e);
}
}
/**
* Method that indicates the searching of bluetooth devices is completed
* thus it will proceed to find the device with support of serial port profile from the list
*/
public void inquiryCompleted(int discType)
{
if(discType==INQUIRY_COMPLETED)
{
try
{
DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
transIds = new int[devices.size()];
for (int i=0;i<devices.size();i++)
{
RemoteDevice device = (RemoteDevice)devices.elementAt(i);
transIds[i] = agent.searchServices(new int[]{0x0003}, new UUID[]{SERIAL}, device, this);
}
}
catch(BluetoothStateException e)
{
System.out.println(e);
}
}
inquiry = false;
}
/**
* Method that indicates the bluetooth devices have the required bluetooth services
*/
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{

for(int i=0;i<servRecord.length;i++)
{
try
{
String[] s = new String[2];
s[0] = servRecord[i].getHostDevice().getFriendlyName(true);
s[1] = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
targetDevices.addElement(s);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
/**
* Method to call the screen to display the list of bluetooth devices discovered
*/
public void serviceSearchCompleted(int transID, int respCode)
{
screen.showDevices(targetDevices);
}
}
Reply With Quote

#2 Old Re: Unable to detect devices - 2008-02-28, 05:27

Join Date: Feb 2008
Posts: 6
yeezhihao
Offline
Registered User
Can someone please help??
I really need this resolved badly.

Thank you v much in advance
Zhihao
Reply With Quote

#3 Old Smile 2008-02-28, 12:00

Join Date: Mar 2003
Posts: 2,617
traud
Offline
Super Contributor
Although we have have Forum Experts here, a forum like this one is voluntarily based: You get an answer when someone has the time to read your question and has an idea to contribute. Consequently, I recommend to avoid adverbs like ‘urgently’. For as fast as possible answers, there is Nokia’s paid technical support. Additionally, replying to your own question within a few hours, might look like your question was answered already.

If you post code, I recommend to have a look at the available vB tags in this forum. This makes posts, especially those with code much easier to read.

Furthermore, please do not cross-post = asking the same question at several places within one forum. When someone wants to answer, it is difficult to judge where to answer especially when there are already answers of other forum members in all cross-posts. As this is a Java related question, let us continue with my answer there…
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect side volume key in Series 40 devices. gimmins Symbian Tools & SDKs 1 2006-05-12 09:06
detect the model of the found devices jlressia Bluetooth Technology 1 2006-05-11 17:25
unable to find bluetooth devices using j2me vitalmd Bluetooth Technology 0 2006-02-16 11:09
Is there a list of devices having animated GIF transparency and subframe problems? hyvatti2 General Browsing 0 2005-12-02 08:35
Discovering devices yourself HershD Bluetooth Technology 12 2003-11-01 07:40

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia