|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Mar 2004
Città: castel san giorgio(salerno)
Messaggi: 1218
|
Applicazione Client(cellulare)/Server(pc) con connessione Bluetooth
Ragazzi come dico nel titolo vorrei creare una applicazione Client/Server dove il client è un telefonino e il servere un pc.
Sul cellulare ovviamente girerà una midlet. Il programma Java per il pc l'ho gia fatto: Codice:
import java.io.IOException;
public class ServerTest {
public static void main(String[] args){
FileServerx f=new FileServerx();
try {
f.action();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Codice:
import javax.microedition.io.*;
import java.io.*;
import javax.bluetooth.*;
import javax.obex.*;
public class FileServerx extends ServerRequestHandler
//implements ActionListener{
{
public FileServerx(){}
//Bluetooth Service name
private static final String myServiceName = "MyBtService";
// Bluetooth Service UUID of interest
private static final String myServiceUUID = "2d26618601fb47c28d9f10b8ec891363";
private UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, false);
public void action() throws IOException {
// Define the server connection URL
String url = "btgoep://localhost:" +MYSERVICEUUID_UUID.toString()+";name=FTP;authenticate=false;master=false;encrypt=false";
//String connURL = "btspp://localhost:"+MYSERVICEUUID_UUID.toString()+";name="+myServiceName+";";
// Create a server connection (a notifier)
//StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(connURL);
SessionNotifier scn = (SessionNotifier)Connector.open(url);
// Accept a new client connection
System.out.println("[server:] Now waiting for a client to connect");
//StreamConnection sc = scn.acceptAndOpen();
//StreamConnection conn = scn.acceptAndOpen();
scn.acceptAndOpen(this);
System.out.println("[server:] The client has created an OBEX session");
// RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
//System.out.println("New client connection... " +
// rd.getFriendlyName(false));
/*
try {
java.io.InputStream is = conn.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ conn.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + conn.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
//return ResponseCodes.OBEX_HTTP_OK;
//}*/
}
public int onConnect(HeaderSet request, HeaderSet reply) {
System.out.println("[server:] The client has created an OBEX session");
return ResponseCodes.OBEX_HTTP_OK;
}
public void onDisconnect (HeaderSet req, HeaderSet resp) {
System.out.println("[server:] The client has disconnected the OBEX session");
}
public int onPut (Operation op) {
try {
java.io.InputStream is = op.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ op.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + op.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
return ResponseCodes.OBEX_HTTP_OK;
}
}
Ecco il codice della midlet: Codice:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ClientMidlet extends MIDlet implements CommandListener{
public Display display;
public Form form;
public Command connetti;
public void startApp(){
display = Display.getDisplay(this);
form = new Form("Client Application");
connetti = new Command("connetti", Command.ITEM, 0);
form.addCommand(connetti);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){
notifyPaused();
}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command comando, Displayable display){
if(comando==connetti){
AvetanaConnection conn = new AvetanaConnection(Display.getDisplay(this));
conn.start();
}
}
}
Codice:
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import java.io.*;
public class AvetanaConnection extends Thread{
private Display display=null;
private Connection conn = null;
private InputStream is = null;
private OutputStream os = null;
ClientSession con = null;
HeaderSet hdr = null;
public AvetanaConnection(Display d){
this.display = d;
}
public void run(){
try{
Connection conn = Connector.open("btgoep://0009DD104AB2:1;authenticate=false;master=false;encrypt=false");
Form f = new Form("nuovo form");
f.append("we");
display.setCurrent(f);
}
catch(IOException e){
Form f = new Form("form errore");
f.append("errore");
display.setCurrent(f);}
}
}
|
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Jul 2003
Città: pisa
Messaggi: 141
|
non sono un esperto di applicazioni di questo tipo, ma credo che la midlet debba comunque fare un device discovery per trovare i dispositivi bluetooth presenti prima di tentare una connessione (anche se conosci già l'indirizzo). prova a leggere la documentazione e questo
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Mar 2004
Città: castel san giorgio(salerno)
Messaggi: 1218
|
Ero arrivato a questa conclusione, cioè di fare prima il discovery ma esce dall'applicazione dando un errore ogni vota che arriva alla riga dove si instanzia il LocalDevice.
Questa è la MIDlet: Codice:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.bluetooth.*;
public class ClientMidlet extends MIDlet implements CommandListener{
private Display display = Display.getDisplay(this);;
private Form form;
private Command connetti;
public void startApp(){
form = new Form("Client Application");
connetti = new Command("connetti", Command.ITEM, 0);
form.addCommand(connetti);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){
notifyPaused();
}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command comando, Displayable display){
if(comando==connetti){
try{
LocalDevice localDevice = LocalDevice.getLocalDevice();
DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
DeviceDiscoverer myDeviceDiscoverer =
new DeviceDiscoverer(Display.getDisplay(this));
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, myDeviceDiscoverer);
}
catch(Exception e){
Form formErrore = new Form("errore");
formErrore.append(e.getMessage());
this.display.setCurrent(formErrore);
}
}
Credo che il problema sia da attribuirsi al fatto che non ha le Api JSR-085. In questo caso come si puo procedere, tenendo conto che ho il .jar jsr085 nella cartella lib del Wireless Toolkit? Ultima modifica di boysna : 28-01-2007 alle 17:33. |
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Mar 2004
Città: castel san giorgio(salerno)
Messaggi: 1218
|
JSR082, pardon...
|
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Jul 2003
Città: pisa
Messaggi: 141
|
se non ha le api per il bluetooth, mi sa che non ci puoi fare nulla...
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:48.



















