1

我在 j2me 中开发了一个简单的蓝牙连接应用程序。

我在模拟器上试了一下,服务器和客户端都可以找到对方,但是当我将应用程序部署到黑莓手机并连接到蓝牙蜂设备时,它说service search no records

有什么可能是错的?是j2me在bluetoothbee中找不到服务吗?

j2me自己成功找到了bluetoothbee设备,但是为什么找不到服务呢?

我的代码如下。我不明白的是UUID?如何为未知来源设置 UUID?因为我不知道蓝牙设备的 UUID。

class SearchingDevice extends Canvas implements Runnable,CommandListener,DiscoveryListener{

//......

public SearchingDevice(MenuUtama midlet, Display display){
    this.display = display;
    this.midlet = midlet;
    t = new Thread(this);
    t.start();

    timer = new Timer();
    task = new TestTimerTask();

    /*--------------------Device List------------------------------*/
        select = new Command("Pilih",Command.OK,0);
        back = new Command("Kembali",Command.BACK,0);
        btDevice = new List("Pilih Device",Choice.IMPLICIT);
        btDevice.addCommand(select);
        btDevice.addCommand(back);
        btDevice.setCommandListener(this);

    /*------------------Input Form---------------------------------*/

        formInput = new Form("Form Input");
        nama = new TextField("Nama","",50,TextField.ANY);
        umur = new TextField("Umur","",50,TextField.ANY);
        measure = new Command("Ukur",Command.SCREEN,0);
        gender = new ChoiceGroup("Jenis Kelamin",Choice.EXCLUSIVE);
        formInput.addCommand(back);
        formInput.addCommand(measure);
        gender.append("Pria", null);
        gender.append("Wanita", null);
        formInput.append(nama);
        formInput.append(umur);
        formInput.append(gender);

        formInput.setCommandListener(this);
     /*---------------------------------------------------------------*/


        findDevice();
}


/*----------------Gambar screen searching device---------------------------------*/
protected void paint(Graphics g) {

        g.setColor(0,0,0);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(255,255,255);
        g.drawString("Mencari Device", 20, 20, Graphics.TOP|Graphics.LEFT);

    if(this.counter == 1){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
    }
    if(this.counter == 2){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
    }
    if(this.counter == 3){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
        g.setColor(255,115,200);
        g.fillRect(100, 60, 20, 60);
    }
    if(this.counter == 4){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
        g.setColor(255,115,200);
        g.fillRect(100, 60, 20, 60);
        g.setColor(100,255,255);
        g.fillRect(140, 40, 20, 80);
        //display.callSerially(this);
    }


}
/*--------- Running Searching Screen ----------------------------------------------*/
public void run() {
    while(run){

       this.counter++;
       if(counter > 4){
           this.counter = 1;
       }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("interrupt"+ex.getMessage());
        }
         repaint();
    }
}

/*-----------------------------cari device bluetooth yang -------------------*/
public void findDevice(){
    try {
        devices = new java.util.Vector();
        local = LocalDevice.getLocalDevice();
        agent = local.getDiscoveryAgent();
        local.setDiscoverable(DiscoveryAgent.GIAC);
        agent.startInquiry(DiscoveryAgent.GIAC, this);
    } catch (BluetoothStateException ex) {
        System.out.println("find device"+ex.getMessage());
    }
}
/*-----------------------------jika device ditemukan--------------------------*/
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
    devices.addElement(rd);
}
/*--------------Selesai tes koneksi ke bluetooth server--------------------------*/
public void inquiryCompleted(int param) {
    switch(param){
        case DiscoveryListener.INQUIRY_COMPLETED: //inquiry completed normally
            if(devices.size()>0){ //at least one device has been found
                services = new java.util.Vector();
                this.findServices((RemoteDevice)devices.elementAt(0));
                this.run = false;
                do_alert("Inquiry completed",4000);
            }else{
                do_alert("No device found in range",4000);
            }
            break;
        case DiscoveryListener.INQUIRY_ERROR:
            do_alert("Inquiry error",4000);
            break;
        case DiscoveryListener.INQUIRY_TERMINATED:
            do_alert("Inquiry canceled",4000);
            break;
    }
}
/*-------------------------------Cari service bluetooth server----------------------------*/
public void findServices(RemoteDevice device){
     try {
        // int[] attributes = {0x100,0x101,0x102};
        UUID[] uuids = new UUID[1];
        //alamat server
       uuids[0] = new UUID("F0E0D0C0B0A000908070605040302010",false);
        //uuids[0] = new UUID("8841",true);
        //menyiapkan device lokal
        local = LocalDevice.getLocalDevice();
        agent = local.getDiscoveryAgent();
        //mencari service dari server

        agent.searchServices(null, uuids, device, this);
        //server = (StreamConnectionNotifies)Connector.open(url.toString());

    } catch (BluetoothStateException ex) {
       // ex.printStackTrace();
        System.out.println("Errorx"+ex.getMessage());
    }
}
/*---------------------------Pencarian service selesai------------------------*/
public void serviceSearchCompleted(int transID, int respCode) {
    switch(respCode){
       case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
           if(currentDevice == devices.size() - 1){
               if(services.size() > 0){
                   this.run = false;
                   display.setCurrent(btDevice);
                   do_alert("Service found",4000);
               }else{
                   do_alert("The service was not found",4000);
               }
           }else{
               currentDevice++;
               this.findServices((RemoteDevice)devices.elementAt(currentDevice));
           }
           break;
       case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
           do_alert("Device not Reachable",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_ERROR:
           do_alert("Service search error",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
           do_alert("No records return",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
           do_alert("Inquiry canceled",4000);
           break;
   }    }

public void servicesDiscovered(int i, ServiceRecord[] srs) {
    for(int x=0; x<srs.length;x++)
         services.addElement(srs[x]);
    try {
            btDevice.append(((RemoteDevice)devices.elementAt(currentDevice)).getFriendlyName(false),null);
    } catch (IOException ex) {
        System.out.println("service discover"+ex.getMessage());
    }
}
    public void do_alert(String msg, int time_out){
    if(display.getCurrent() instanceof Alert){
        ((Alert)display.getCurrent()).setString(msg);
        ((Alert)display.getCurrent()).setTimeout(time_out);
    }else{
        Alert alert = new Alert("Bluetooth");
        alert.setString(msg);
        alert.setTimeout(time_out);
        display.setCurrent(alert);
    }
    }

private String getData(){
    System.out.println("getData");
    String cmd="";

     try {
            ServiceRecord service = (ServiceRecord)services.elementAt(btDevice.getSelectedIndex());
            String url = service.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
            conn = (StreamConnection)Connector.open(url);
            DataInputStream in = conn.openDataInputStream();
            int i=0;

            timer.schedule(task, 15000);
            char c1;
               while(time){

                 //while(((c1 = in.readChar())>0) && (c1 != '\n')){
                //while(((c1 = in.readChar())>0) ){
                    c1 = in.readChar();
                cmd = cmd + c1;
                //System.out.println(c1);
               // }
               }
                 System.out.print("cmd"+cmd);


                if(time == false){
                in.close();
                conn.close();
                }

        } catch (IOException ex) {
            System.err.println("Cant read data"+ex);
        }
     return cmd;
}




//timer task fungsinya ketika telah mencapai waktu yg dijadwalkan putus koneksi
private static class TestTimerTask extends TimerTask{

    public TestTimerTask() {
    }
    public void run() {

        time = false;
    }
}   
}
4

0 回答 0