2011-04-08 109 views
1

我用下面的函數,GOTS微軟:藍牙編程 - 可用設備

 public List<Device> DiscoverAllDevices() 
     { 
      List<Device> devices = new List<Device>(); 

      // Initialize WinSock 
      WsaData wsadata = new WsaData(); 

      int result = 
       BluetoothHelper.WSAStartup(BluetoothHelper.MakeWord(2, 2), 
              ref wsadata); 
      if (result != 0) 
       BluetoothHelper.GetError(); 

      // Scan for bluetooth devices 
      QuerySet wsaq = new QuerySet(); 
      //Initialize queryset structure with device specific 
      //information. 
      wsaq.Size = Marshal.SizeOf(typeof(QuerySet)); 
      wsaq.NameSpace = BluetoothHelper.NS_BTH; 
      IntPtr lookup = IntPtr.Zero; 
      uint flags = BluetoothHelper.LUP_RETURN_NAME 
          | BluetoothHelper.LUP_CONTAINERS 
          | BluetoothHelper.LUP_RETURN_ADDR 
          | BluetoothHelper.LUP_FLUSHCACHE 
          | BluetoothHelper.LUP_RETURN_TYPE 
          | BluetoothHelper.LUP_RETURN_BLOB 
          | BluetoothHelper.LUP_RES_SERVICE; 

      //Initiates a client query that is constrained by the 
      //information contained within a queryset structure. 

      result = BluetoothHelper.WSALookupServiceBegin(wsaq, 
                  flags, 
                  ref lookup); 
      if (result != 0) 
       BluetoothHelper.GetError(); 

      while (0 == result) 
      { 
       int buffer = 0x10000; 

       IntPtr bufferPtr = Marshal.AllocHGlobal(buffer); 
       QuerySet qsResult = new QuerySet(); 

       //Retrieves the requested device information. 
       result = BluetoothHelper.WSALookupServiceNext(lookup, 
                 flags, 
                 ref buffer, 
                 bufferPtr); 
       if (0 == result) 
       { 
        Marshal.PtrToStructure(bufferPtr, qsResult); 
        devices.Add(new Device(qsResult)); 
       } 
       else 
       { 
        BluetoothHelper.GetError(); 
       } 
      } 
      //end device-lookup 
      result = BluetoothHelper.WSALookupServiceEnd(lookup); 
      if (result != 0) 
       BluetoothHelper.GetError(); 

      // cleanup winsock 
      result = BluetoothHelper.WSACleanup(); 

      if (result != 0) 
       BluetoothHelper.GetError(); 

      return devices; 
     } 

,但我需要知道實際的數據,如果設備在範圍內或沒有。這個代碼總是找到設備,如果它之前被發現,即使這個設備被關閉。爲什麼以及如何解決這個問題? 我花了幾乎整整一天找到解決方案 謝謝

回答

0

不幸的是,沒有直接的API。 :-(它是Win32上微軟藍牙堆棧的API中的一大空白。

但是它可以通過組合API的數量。我調查和實施爲我的我的共享源代碼的.NET藍牙,http://32feet.codeplex.com如果你的程序可以寫成託管代碼,然後它將爲你節省大量的時間在使用藍牙。:-)

無論如何,我的方式獲取'可發現只'設備列表記錄在http://32feetnetdev.wordpress.com/2010/11/15/device-discovery-improvements-on-msftwin32/因此,這是使用藍牙事件API查看發現的設備,並同時運行正常發現。