J2SE Bluetooth and java comm API

E

Eta

Hi all,

I'm trying to make a Java class to connect my Laptop with a Phone (Nokia
6600) by Bluetooth Device(on Laptop I use a USB Bluetooth Dongle bridge
as COM Port) and send a String Message to the Phone. For Serial Device
connection and managing I use java COM API from Sun. When I try to run
the attached example code, the connection between phone and PC comes
activate (I see the message on Phone Diplay), but when I try to write
the String to Output Stream , it
does not transmit to the phone, and the class execution seems like being
in standby (doesn't stop or exit). You can find the follow line code
at the end of the example code :

outputStream.write(messageString.getBytes());

Does somebody help me??

Best regards,

Eta

PS:
Have a nice 2005 ... and sorry for my bad english!






/*
* Bluetooth Example
*
*/

package com.eta.blue.util;

import java.io.*;
import java.util.*;
import javax.comm.*;

public class COMAdapter {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println(portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM10")) {
try {
serialPort = (SerialPort)
portId.open("text", 2000);

} catch (PortInUseException e) {
System.out.println("ERRORE GRAVE!!! : Porta in
uso!!");
serialPort.close();
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
serialPort.close();
}

try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
serialPort.close();
}

try {
System.out.println("Trasferimento in corso!!");

outputStream.write(messageString.getBytes());
System.out.println("Fine trasferimento");
outputStream.flush();
} catch (IOException e) {
serialPort.close();
}
}
}
}
}
}
 
B

ByteCoder

Eta said:
Hi all,

I'm trying to make a Java class to connect my Laptop with a Phone (Nokia
6600) by Bluetooth Device(on Laptop I use a USB Bluetooth Dongle bridge
as COM Port) and send a String Message to the Phone. For Serial Device
connection and managing I use java COM API from Sun. When I try to run
the attached example code, the connection between phone and PC comes
activate (I see the message on Phone Diplay), but when I try to write
the String to Output Stream , it
does not transmit to the phone, and the class execution seems like being
in standby (doesn't stop or exit). You can find the follow line code
at the end of the example code :

As is shown here, you have to flush() the OutputStream:

outputStream.write(messageString.getBytes());
outputStream.flush();

If you don't flush the stream the message you sent is kept in the buffer
and might never be sent.
The message would only send without the flush command if the
OutputStream buffer is full.
 
E

Eta

Hi,

sorry but on my source code I have flush() the OutputStream....you can
see it at the end of souce code!

Best regards,

Eta
 
B

ByteCoder

Eta said:
Hi,

sorry but on my source code I have flush() the OutputStream....you can
see it at the end of souce code!

Best regards,

What happens if you use the exact same code in the example and just
change the message String?
 
J

JScoobyCed

Eta said:
Hi all,

I'm trying to make a Java class to connect my Laptop with a Phone (Nokia
6600) by Bluetooth Device(on Laptop I use a USB Bluetooth Dongle bridge
as COM Port) and send a String Message to the Phone. For Serial Device
connection and managing I use java COM API from Sun. When I try to run
the attached example code, the connection between phone and PC comes
activate (I see the message on Phone Diplay), but when I try to write
the String to Output Stream , it
does not transmit to the phone, and the class execution seems like being
in standby (doesn't stop or exit).

What is you phone application ? J2ME ? What is reading the text you send
over the BT connection ? If there is no program listening, then you
won't see anything on the phone.
 
E

Eta

Hello
What happens if you use the exact same code in the example and just
change the message String?

Normally I use the exact same code in the example!
If I change the message String the result is the same!

Eta
 
E

Eta

Hello,

I do not upload the J2ME application on the Phone at the moment.
But if I try to send a file from OS Bluetooth manager a receive e
message on e phone same as an SMS....so why my application can't do the
same result??


Eta
 
B

ByteCoder

Eta said:
Hello


Normally I use the exact same code in the example!
If I change the message String the result is the same!

Eta

Could you only past the non-modified example please?
 
E

Eta

Hi, here you:
Could you only past the non-modified example please?


import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
}
 
B

ByteCoder

Eta said:
Hi, here you:
static String messageString = "Hello, world!\n";
<snip code>

You're telling me that "Hello, World!\n" works, but any other test doesn't?

I hope you didn't forget the newline character at the end of the string.

Have you tried this?
static String messageString = "Work, you POS!\n";
;)

I suppose the newline character is important.
 
J

JScoobyCed

Eta said:
Hello,

I do not upload the J2ME application on the Phone at the moment.
But if I try to send a file from OS Bluetooth manager a receive e
message on e phone same as an SMS....so why my application can't do the
same result??


Eta

Err... I think you have to make sure the phone understand the message
you are sending, and support the protocol you are using.
On your J2SE side (the computer) you open a serial connection over
bluetooth (known as RFCOMM). On the other side, the phone, when you
activate BlueTooth, it will listen for acommunication on a different
port than the phone BTserial port. It is likely you try to connect your
PS/2mouse on the RJ45 ethernet port and expect it to work (ok, don't
take that too seriousely, I intensionaly exagerate on that one :) )
On the phone, you need an application (J2ME or native code like C, C++)
to open and listen for a RFCOMM serial communication.
Another option is to use on the computer side a library le BlueCove (for
windows XP SP2, and Linux) and write a program JSR82 (bluetooth)
compliant. With this you might be able to create a communication
understood by the BT listener on the phone.
 
Joined
Sep 2, 2006
Messages
1
Reaction score
0
J2ME Source code ?

Hi, all. I'm new to Java. :hello:

I need to develop a Java application to connect between a Laptop(client) and mobile phone(server). Regarding the source code J2SE posted, could you please post the J2ME part that work with it for my reference?

Thanks in advance. :fisheye:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top