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);
[b]HeaderSet hsConnectReply = clientSession.connect(null);[/b]
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

Recommended Answers

All 2 Replies

Sorry, but with the code above you have very little chances of success as you ignored basics

HeaderSet connect(HeaderSet headers) throws java.io.IOException

Where is yours try-catch cause?
Also here is HeaderSet method as we know from API

void setHeader(int headerID, java.lang.Object headerValue)

this is your attempt

hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");

Hello.txt hardly fit in any of the categories for headerValue COUNT, NAME, TYPE, LENGTH, TIME_ISO_8601, TIME_4_BYTE, DESCRIPTION, TARGET , HTTP, WHO, OBJECT_CLASS or APPLICATION_PARAMETER

If I may I will recommend you to read Kicking Butt with MIDP and MSA (website holds also examples from the book 19 - Text and Multimedia Messaging and 20- Bluetooth and OBEX. However even they are holding great examples of code, without rest of the book to explain you will not learn too much)

public void send(String conn,String msg){
        try {
            System.out.println("addres");
            ClientSession cs = (ClientSession) OBEXConnector.open(conn);
           
            System.out.println("opening");
            HeaderSet hs = cs.connect(cs.createHeaderSet());
            System.out.println("created header set");
            byte[] text =msg.getBytes("UTF8");
            hs.setHeader(HeaderSet.NAME, "msg.txt");
            hs.setHeader(HeaderSet.TYPE, "txt");
            //hs.setHeader(0x49, text);
            System.out.println("putting....");
            Operation po = cs.put(hs);
            System.out.println("put....");
            po.openOutputStream().write(text);
            po.close();
            cs.disconnect(null);
            cs.close();
            System.out.println("closed...");
        } catch (Exception e) {
           System.out.println("exception    ="+e);
        }

}

that code work correctly
just pass the phon url and your message

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.