2010-03-10 53 views
0

我開發簡單的j2me藍牙客戶端,並且有藍牙設備搜索問題。 找不到函數startInquiry。j2me藍牙客戶端。函數startInquiry什麼都沒有發現

客戶:諾基亞5220 服務器:我與藍牙適配器的PC

所有藍牙設備上。

/* *要更改此模板,請選擇工具|模板 *並在編輯器中打開模板。 */

import javax.microedition.midlet.*; 
import javax.bluetooth.*; 
import java.util.Vector; 
import javax.microedition.lcdui.*; 

/** 
* @author Администратор 
*/ 
public class Midlet extends MIDlet implements DiscoveryListener 
{ 
    private static Vector vecDevices=new Vector(); 
    private static String connectionURL=null; 
    private LocalDevice localDevice; 
    private DiscoveryAgent agent; 
    private RemoteDevice remoteDevice; 
    private RemoteDevice[] devList; 
    private Display display; 
    private Form form; 

    public void startApp() { 
     display = Display.getDisplay(this); 
     form = new Form("Client"); 
     try { 
      localDevice = LocalDevice.getLocalDevice(); 
     } catch(BluetoothStateException e) { 
      e.printStackTrace(); 
     } 

     form.append("Address: "+localDevice.getBluetoothAddress()+"\n\n"); 
     form.append("Name: "+localDevice.getFriendlyName()+"\n\n"); 

     try { 
      agent = localDevice.getLocalDevice().getDiscoveryAgent(); 

      form.append("Starting device inquiry... \n\n"); 

      boolean si = agent.startInquiry(DiscoveryAgent.GIAC, this); 
      if (si) { 
       form.append("true"); 
      } else { 
       form.append("false"); 
      } 
     } catch(BluetoothStateException e) { 
     } 

     int deviceCount = vecDevices.size(); 
     if(deviceCount <= 0){ 
      form.append("No Devices Found ."); 
     } 
     else{ 
      //print bluetooth device addresses and names in the format [ No. address (name) ] 
      form.append("Bluetooth Devices: "); 
      for (int i = 0; i < deviceCount; i++) { 
       remoteDevice=(RemoteDevice)vecDevices.elementAt(i); 
       form.append(remoteDevice.getBluetoothAddress()); 
      } 
     } 

      display.setCurrent(form); 
    } 

    public void pauseApp() { 
    } 

    public void destroyApp(boolean unconditional) { 
    } 

     public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { 
       //add the device to the vector 
       if(!vecDevices.contains(btDevice)){ 
        vecDevices.addElement(btDevice); 
       } 
     } 
     public void inquiryCompleted(int discType) 
     { 
     } 


      //implement this method since services are not being discovered 
      public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { 
       if(servRecord!=null && servRecord.length>0){ 
        connectionURL=servRecord[0].getConnectionURL(0,false); 
       } 
      } 
      //implement this method since services are not being discovered 
      public void serviceSearchCompleted(int transID, int respCode) { 
      } 
} 

回答

1

不知道確切的問題是什麼,但你絕對不希望在MIDlet的startApp()方法來這樣做。這是一種系統生命週期方法,應該很快返回,但掃描藍牙設備會長時間阻止它。您的startApp()方法正在佔用設備的資源,以便進行實際的掃描!

重構,所以您的設備掃描是在一個新的線程完成,然後看看會發生什麼。

0

你說所有設備都是 - 但也檢查是否所有設備是可發現

我犯了這個錯誤在我自己之前!

如果要以編程方式切換模式,請查找方法LocalDevice.setDiscoverable()。

1

您似乎誤解了藍牙API的工作方式。 startInquiry方法只有啓動設備發現過程並立即返回,留下發現在後臺運行。當發現設備時,您會爲每個設備回調deviceDiscovered方法,並且當發現過程完成時,您會收到inquiryCompleted方法的回調。因此,您需要將vecDevices成員的訪問權限和表格操作從startApp移動到inquiryCompleted,以便能夠實際顯示發現的信息。