Hi all,
I have some problem with my Java code, some times it works perfectly and some times it does not work, more accurately when i launch my midlet the buetooth devices are discovered every time, the problem is with searchServices some times it work and some times does not work can any one help me to resolve this pb.
Client My Midlet
public void commandAction(Command arg0, Displayable arg1) {
if (screenSearch == null) {
// write pre-init user code here
screenSearch = new List("Perif Bluetooth", Choice.IMPLICIT);
RemoteDevice[] devs = scanDevs();
for (int i=0; i<devs.length; i++) {
try {
screenSearch.append(devs[i].getFriendlyName(true), null);
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
_display.setCurrent(screenSearch);
// check for spp service
RemoteDevice remoteDevice=devs[0];
UUID[] uuidSet = new UUID[1];
uuidSet[0]=new UUID(0x1101);
int[] attrSet = {0x0100, 0x0003, 0x0004};
try {
agent.searchServices(attrSet,uuidSet,remoteDevice, this);
screenSearch.append("Service search at :" + remoteDevice.getBluetoothAddress(), null);
} catch (BluetoothStateException e1) {
e1.printStackTrace();
}
try {
synchronized(this){
this.wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
// affichage URL.
screenSearch.append("URL"+ connectionURL, null);
//connect to the server and send a line of text
try{
streamConnection=(StreamConnection)Connector.open(connectionURL);
//send string
OutputStream outStream=streamConnection.openOutputStream();
OutputStreamWriter pWriter= new OutputStreamWriter(outStream);
pWriter.write("Hi Server i wana a zeph video\r\n");
pWriter.flush();
pWriter.close();
}catch(Exception e){
}
//read response
/*
InputStream inStream;
try {
inStream = streamConnection.openInputStream();
InputStreamReader bReader2 = new InputStreamReader(inStream);
int c;
StringBuffer str = new StringBuffer();
while((c=bReader2.read())!= -1){
str.append((char)c);
}
screenSearch.append("Server Response :" + str.toString(), null);
} catch (IOException e) {
e.printStackTrace();
}
//BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
//String lineRead=bReader2.readLine();
*/
try{
InputStream inStream = streamConnection.openInputStream();
int c;
FileConnection fc = (FileConnection)Connector.open("file:///C:/predefgallery/predefvideos/a.3gp",Connector.READ_WRITE);
if(!fc.exists()) fc.create();
OutputStream _out = fc.openOutputStream();
while((c=inStream.read())!= -1){
_out.write(c);
}
InputStream is = fc.openInputStream();
Player player = Manager.createPlayer(is,"video/3gp");
player.prefetch();
player.realize();
player.start();
fc.close();
_out.close();
inStream.close();
screenSearch.append("Transfert Terminer", null);
}catch(Exception e){
screenSearch.append("Exception "+e.getMessage(), null);
}finally{
try {
streamConnection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void deviceDiscovered(RemoteDevice dev, DeviceClass cod) {
// TODO Auto-generated method stub
devices.addElement(dev);
}
public void inquiryCompleted(int arg0) {
// TODO Auto-generated method stub
synchronized(this) {
this.notifyAll();
}
}
public void serviceSearchCompleted(int arg0, int arg1) {
synchronized(this){
this.notify();
}
}
public void servicesDiscovered(int arg0, ServiceRecord[] servRecord) {
if(servRecord!=null && servRecord.length>0){
connectionURL=servRecord[0].getConnectionURL(0,false);
//RemoteDevice device = servRecord[0].getHostDevice();
//String adresse = device.getBluetoothAddress();
//screenSearch.append("connecté à :"+ adresse, null);
}
synchronized(this){
this.notify();
}
}
Server
public class SampleSPPServer {
//start server
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID(0x1101);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//read string from spp client
InputStream inStream=connection.openInputStream();
BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
String lineRead=bReader.readLine();
System.out.println(lineRead);
//send response to spp client
/*
OutputStream outStream=connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("hi mal hadchi mabgha ikhdem walmarad\r\n");
pWriter.flush();
pWriter.close();
*/
OutputStream outStream=connection.openOutputStream();
//if(inStream != null){
FileInputStream intputstream = null;
try{
intputstream = new FileInputStream("a.3gp");
int c;
while((c=intputstream.read())!= -1){
System.out.println("suis la ");
outStream.write(c);
}
System.out.println("Transfert terminer");
}catch(Exception e){
System.out.println("Exception : "+e.getMessage());
}finally{
//streamConnNotifier.close();
}
//}
}
public static void main(String[] args) throws IOException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SampleSPPServer sampleSPPServer=new SampleSPPServer();
sampleSPPServer.startServer();
}
}
Regards
Ziad