Word :    Username :           
 

Archived from groups: alt.cellular.bluetooth (More info?)

 

Avrei bisogno del vostro aiuto.
Sto realizzando un'applicazione per lo scambio di messaggi tra due
dispositivi. Il primo dispositivo è un PC e il secondo un telefonino. I due
dispositivi utilizzano Bluetooth per comunicare. Ho decisamente le idee
confuse. C'è qualcuno che conosce il problema? Può dirmi qualcosa in più?
Per quanto riguarda la parte client ho utilizzato j2me, il wireless toolkit
e le API JSR82. Il problema riguarda il server perchè da quello che ho
capito non è possibile utilizzare le API JSR 82 su J2se. Ho utilizzato
allora lo stack che ho trovato su javabluetooth.org. Mi sono anche procurato
anche un dongle USB e ho installato il bridge USB - SerialDriver PL2303 sul
pc.
Questo è il codice per il server. L'esecuzione si blocca sull'istruzione
che ho segnato con gli asterischi e non viene più eseguita nessuna
istruzione.
Il problema riguarda, secondo me le prime istruzioni del main(). Queste
sono necessarie altrimenti viene sollevata un'eccezione e il BluetoothStack
non viene inizializzato. Il fatto è che non si riesce a scrivere niente
sullo stream della porta e l'esecuzione rimane bloccata. L'implementazione
di questo stack utilizza le porte seriali e non l'USB per questo sono
necessarie le prime istruzioni. C'è qualcuno che sà come bisogna fare? Non
so come fare.Grazie!!
N.B. Se volete rispondetemi direttamente.


import javax.microedition.io.*;
import javax.bluetooth.*;


import org.javabluetooth.stack.BluetoothStack;
import org.javabluetooth.stack.BluetoothStackLocal;
import org.javabluetooth.stack.hci.HCIDriver;
import org.javabluetooth.stack.hci.HCIException;
import org.javabluetooth.stack.hci.UARTTransport;



public class L2CAPEchoServer
{
static ClientProcess Client = null;

public static void main(String[] args)
{

try {HCIDriver.init(new UARTTransport("COM4" ));

BluetoothStack.init(new BluetoothStackLocal());
} catch (HCIException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
L2CAPConnectionNotifier Server = null;

try
{
LocalDevice local = LocalDevice.getLocalDevice();
local.setDiscoverable( DiscoveryAgent.GIAC ); *****************
}
catch (BluetoothStateException e)
{
System.err.println( "Failed to start service" );
System.err.println( "BluetoothStateException: " +
e.getMessage() );
return;
}

try
{
// start the echo server (with a fictional UUID)
Server = (L2CAPConnectionNotifier)Connector.open(
"btl2cap://localhost:123;" );
}
catch (IOException e)
{
System.err.println( "Failed to start service" );
System.err.println( "IOException: " + e.getMessage() );
return;
}

System.out.println( "Starting L2CAP Echo Server" );

// This server actually runs forever. However, it can be stopped
// by terminating the KVM from the command line
// The server can terminate client connections by
// setting the client connections public variable "end" to "true"
// like: L2CAPEchoServer.Client.end = true;
while( true )
{
L2CAPConnection conn = null;

try
{
// wait for incoming client connections (blocking method)
conn = Server.acceptAndOpen();

// here we've got one, start it in a separate thread
L2CAPEchoServer.Client = new ClientProcess( conn );
L2CAPEchoServer.Client.start();
}
catch (IOException e)
{
System.out.println("IOException: " + e.getMessage());
}
}
}
}

class ClientProcess extends Thread
{
static L2CAPConnection clientconn;
public boolean end;

// the constructor
ClientProcess( L2CAPConnection conn )
{
this.clientconn = conn;
this.end = false;
}

// start the communication with the client
public void run()
{
byte[] data = null;
int length;

System.out.println( "Client is connected" );

while( !end )
{
try
{
// prepare a receive buffer
length = clientconn.getReceiveMTU();
data = new byte[length];

// read in the data sent by the client (method blocks!)
length = clientconn.receive(data);
System.out.println( "Received " + length + " bytes from
client" );

// and immediately send it back on the same connection
(echo)
clientconn.send(data);
}
catch( IOException e )
{
System.out.println("IOException: " + e.getMessage());
}
}
clientconn.close();
}
}

Sponsored Links
Register or log in to remove.
Tom's Hardware > Forum > Mobility Technologies > Bluetooth > java e bluetooth
Go to:

There are 1064 identified and unidentified users. To see the list of identified users, Click here.

Please mind

You are about to answer a thread that has been inactive for more than 6 months.
If you still wish to proceed, please ensure that your posting is original and does not duplicate or overlap any prior responses to this thread.

Add a reply Cancel
Sponsored links
  • Ask the community now
  • Publish
Ad
They won a badge
Join us in greeting them