Bluetooth, send msg from soft key evet.

B

Boki

Hi All,
I try a bluelet demo, I change some code here, I want to move
these codes of run()
========================================================
out.writeUTF( "Boki Back Msg" );
out.flush();
========================================================

to a soft key event, but it never work ( exception error is 0 / null )

public void commandAction(Command c, Displayable s) {
========================================================
out.writeUTF( "Boki Back Msg" );
out.flush();
========================================================
}

My original thread:

=============================================
public void run()
{

StreamConnection c = null;
try
{
localDevice = LocalDevice.getLocalDevice(); // obtain reference to
singleton
localDevice.setDiscoverable(DiscoveryAgent.GIAC); // set Discover mode
to LIAC

String uuidString = "102030405060708090A0B0C0D0E0F010";
UUID uuid = new UUID(uuidString, false);

String url = "btspp://localhost:" + uuid.toString()
+";name=Boki_SPP";

server = (StreamConnectionNotifier)Connector.open( url );
localDevice.setDiscoverable(DiscoveryAgent.GIAC);

ServiceRecord rec = localDevice.getRecord( server );

rec.setAttributeValue( 0x0008, new DataElement(
DataElement.U_INT_1, 0xFF ) );


} catch (Exception e)
{

System.out.println("try1 fail"+e.getMessage());
System.out.println(e);
}

while( !done)
{
try {
System.out.println("try2");

c = server.acceptAndOpen();
RemoteDevice rdev = RemoteDevice.getRemoteDevice( c );

DataInputStream in = c.openDataInputStream();
String s = in.readUTF();

DataOutputStream out = c.openDataOutputStream();
========================================================
out.writeUTF( "Boki Back Msg" );
out.flush();
========================================================
// close current connection, wait for the next one
// c.close();


} catch (Exception e)
{
System.out.println("try2 fail");
}

} // while

}
 
T

Tor Iver Wilhelmsen

Boki said:
public void commandAction(Command c, Displayable s) {
========================================================
out.writeUTF( "Boki Back Msg" );
out.flush();
========================================================
}

How do you obtain "out" in this case? Do you use the samme Connection
calls you do in the posted run() method?
 
B

boki

Tor Iver Wilhelmsen said:
How do you obtain "out" in this case? Do you use the samme Connection
calls you do in the posted run() method?

Yes, I use the same conector.open
 
D

Darryl Pierce

Boki said:
Hi All,
I try a bluelet demo, I change some code here, I want to move
these codes of run()
========================================================
out.writeUTF( "Boki Back Msg" );
out.flush();
========================================================

to a soft key event, but it never work ( exception error is 0 / null )

Right there's your answer, but you didn't write out the actual error so
I have to assume it. You're getting a null pointer exception. Think
about it for a minute: what could be null in the following code block?
 
B

Boki

Yes, it is a null pointer exception, you are right.

but, what could I do...

the variables define in try {} will be deleted when process exit.

Should I keep the try { c=connector.open(XXX) .........} live?

Best regards,
Boki.
 
D

Darryl Pierce

Boki said:
Yes, it is a null pointer exception, you are right.

but, what could I do...

the variables define in try {} will be deleted when process exit.

Should I keep the try { c=connector.open(XXX) .........} live?

That depends on your application's needs. If the connection needs to be
used for more than a single method invokation then you need to have a
reference to it somewhere so it can be accessed from those other locations.
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top