PDA

View Full Version : Applicazione Client(cellulare)/Server(pc) con connessione Bluetooth


boysna
27-01-2007, 18:29
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:


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();
}
}

}




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;
}
}


Per quanto riguarda la midlet ho solo fatto una semplice connessione ma ovviamente mi da un errore, ed è qui che non capisco il perchè:

Ecco il codice della midlet:

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();
}

}
}




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);}



}

}

pela
28-01-2007, 11:21
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 (http://www.ub.uib.no/elpub/2004/h/413009/Masteroppgave.pdf)

boysna
28-01-2007, 16:04
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:

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);

}
}




Secondo me è un problema sul mio cell, Motorola V635, perchè sull'emulatore del Wireless Toolkit della Sun va bene. Cioè fa il discovery, non trova niente ma almeno parte il discovery.

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?

boysna
28-01-2007, 16:35
JSR082, pardon...

pela
28-01-2007, 16:53
se non ha le api per il bluetooth, mi sa che non ci puoi fare nulla...