Skip to main content

Trouble to send text with bluetooth

1 reply [Last post]
arald
Offline
Joined: 2008-07-25

Hi,
I have a trouble: I want to send text from my laptop to Nokia N95 using bluetooth tecnology.

This is my source:

ClientSession clientSession = (ClientSession) Connector.open(serverURL);
HeaderSet hsConnectReply = clientSession.connect(null);
if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
System.out.println("Failed to connect");
return;
}

HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
hsOperation.setHeader(HeaderSet.TYPE, "text");

//Create PUT Operation
Operation putOperation = clientSession.put(hsOperation);

// Send some text to server
byte data[] = "Hello world!".getBytes("iso-8859-1");
OutputStream os = putOperation.openOutputStream();
os.write(data);
os.close();
putOperation.close();
clientSession.disconnect(null);
clientSession.close();

The application crashes at clientSession.connect(null)

The program open the connection, device show me the dialog view to accept the bluetooth connection but if I accept the application stops. No error are been notified but the program wait for something and it doesn't go on.

Any idea?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
sfitzjava
Offline
Joined: 2003-06-15

You need to supply a HeadSet object to your ClientSession.connect() instead of "null".

HeadSet headerSet = clientSession.createHeaderSet();
clientSession.connect(headerSet);

Here is a link to a page that discusses this method of connection request and another issue that you might run into later.
https://developer.sonyericsson.com/message/75888

Also is your code wrapped in a try/catch, as there are statements that can throw exceptions and these may be what are making your app appear to stop. If you do have a try/catch, you might see if an exception is reported. The other possibility is your app is blocking (stopped) waiting on the device. However since it is null it never finds it and hangs.

I am also wondering why you are using this style of bluetooth connection, as opposed to the more common StreamConnectionNotifier strategy?

Regards,
-Shawn