In you serviceSearchCompleted you should have a notification code the
same as in inquiryCompleted.
Other wise I believe it prints "Não fora encontrado serviços
disponiveis!" and main will stuck in lock.wait();
Also it would be nice not to create connection in function
serviceSearchCompleted and do it in main or in another function.
> import java.io.*;
> import java.util.HashMap;
> // import java.util.*;
> import javax.microedition.io.*;
> import javax.bluetooth.*;
> import javax.obex.*;
>
> public class FTClient implements DiscoveryListener {
>
> private static int[] attrSet = null;
> // private static RemoteDevice btDev = null;
> private static String serviceURL = null;
> private static ClientSession con = null;
> private static HeaderSet hdr = null;
> private static HashMap<String, RemoteDevice> dispositivos = new HashMap<String, RemoteDevice>();
> String aux;
>
>
> //object used for waiting
> private static Object lock = new Object();
>
>
>
> public void deviceDiscovered(RemoteDevice btDevice,DeviceClass cod){
>
> try {
>
> aux = btDevice.getFriendlyName(true);
> System.out.println("Dispositivo nome: " + aux);
> aux = btDevice.getBluetoothAddress();
> System.out.println("Dispositivo endereco:" + aux);
>
> dispositivos.put(aux, btDevice);
>
> // btDev = btDevice;
> } catch (IOException ex) {
>
> System.out.println("Erro: " + ex);
> }
>
> }
>
> public void servicesDiscovered(int transID, ServiceRecord[] servRecord){
> System.out.println("Discovered a service.... ");
>
> for(int i =0; i < servRecord.length; i++){
> serviceURL = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
> System.out.println("The service URL is "+ serviceURL);
> }
> }
>
> public void serviceSearchCompleted(int transID, int respCode){
>
> if(serviceURL == null){
>
> System.out.println("Não fora encontrado serviços disponiveis!");
>
>
> }
>
> System.out.println("Service search completed.......... . ");
> System.out.println("Opening a connection with the server.... ");
>
> try{
> con = (ClientSession)Connector.open(serviceURL);
> hdr = con.connect(null);
>
> if (hdr.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
> System.out.println("Failed to connect");
> return;
> }
>
> System.out.println("Response code of the server after connect..." + hdr.getResponseCode());
>
> // Dados que serão enviados
>
> //Sending a request to server for file Hello.txt
> hdr = con.createHeaderSet();
> hdr.setHeader(HeaderSet.TYPE, "text/plain");
> hdr.setHeader(HeaderSet.NAME, "Hello.txt");
> // hdr.setHeader(HeaderSet.LENGTH, "12");
> hdr.setHeader(HeaderSet.DESCRIPTION, "Promoção");
> // hdr.setHeader(HeaderSet.WHO, "GoD");
>
>
> //The client is now sending the file
> Operation op = con.put(hdr);
>
> System.out.println(op.getResponseCode());
>
> // if (op.getResponseCode() == ResponseCodes.OBEX_HTTP_ACCEPTED){
> byte data[] = "Hello world!".getBytes("iso-8859-1");
>
> OutputStream os = op.openOutputStream();
> os.write(data);
> os.close();
> // }
>
> op.close(); // finaliza operação
> con.disconnect(null);
> con.close();
>
> }catch(IOException e){
> System.out.println(e.getMessage());
> }
>
> System.out.println("Final do service search completed!");
>
>
> }
>
> public void inquiryCompleted(int discType){
>
> synchronized(lock){
> lock.notify();
> }
>
> switch (discType) {
>
> case DiscoveryListener.INQUIRY_COMPLETED :
> System.out.println("INQUIRY_COMPLETED");
> break;
>
> case DiscoveryListener.INQUIRY_TERMINATED :
> System.out.println("INQUIRY_TERMINATED");
> break;
>
> case DiscoveryListener.INQUIRY_ERROR :
> System.out.println("INQUIRY_ERROR");
> break;
>
> default :
> System.out.println("Unknown Response Code");
> break;
>
> }
>
>
>
>
> }
>
> public static void main(String args[]) throws IOException, InterruptedException {
> // FTClient client = new FTClient();
>
> //create an instance of this class
> FTClient ftClient = new FTClient();
>
> LocalDevice local = LocalDevice.getLocalDevice();
>
> System.out.println("Endereço: "+local.getBluetoothAddress());
> System.out.println("Nome: "+local.getFriendlyName());
> System.out.println("Status:"+local.getDiscoverable());
>
> System.out.println("Starting device inquiry...");
>
> DiscoveryAgent agent = local.getDiscoveryAgent();
> agent.startInquiry(DiscoveryAgent.GIAC, ftClient);
>
> try {
> synchronized(lock){
> lock.wait();
> }
> }catch (InterruptedException e){
> e.printStackTrace();
> }
>
>
> // Verificar se ouve sucesso na inquiry
> System.out.println("Inquiry completed...");
>
> UUID[] uuids = new UUID[1];
> uuids[0] = new UUID("1105",true); // 1105 OBEX.ObjectPush
> // 1106 OBEX.FileTransfer
> // 0100 acha todos os serviços
>
>
> if(dispositivos.size() == 0){
> System.out.println("Nenhum dispositivo descoberto, saindo do programa.... ");
> System.exit(1);
> }
>
> System.out.println("Procurando por serviços...");
>
> for (RemoteDevice btDevice : dispositivos.values()){
>
> // FTClient ftClientAux = new FTClient();
>
> serviceURL = null;
>
> System.out.println("Enviando para: ");
> System.out.println("Dispositivo nome: " + btDevice.getFriendlyName(true));
>
>
> agent.searchServices(attrSet, uuids, btDevice, ftClient);
> try {
>
> synchronized(lock){
> lock.wait();
>
> }
> }catch (InterruptedException e){
> e.printStackTrace();
> }
>
> System.out.println("chegou no fim do loop!");
>
> }
>
> System.exit(1);
>
>
>
> }
>
>
> }
>
--
Vlad