2011-10-14 64 views
2

我想在C#應用程序中使用32feet.NET藍牙庫來檢測附近的設備。我的小應用程序的目的是讓PC知道誰在房間裏使用人們的手機的藍牙功能。32feet.net如何發現附近的藍牙設備異步在c#

做這種事情的最好方法是讓我想「跟蹤」的設備連接一次,然後不斷檢查是否可以通過藍牙檢測到。

現在我的問題:

  1. 我需要配對或與我申請認證的設備?如何在C#中使用32feet.NET做到這一點?

  2. 如何連續檢查範圍內的設備並將它們與存儲的設備進行比較?

我知道,這一切都是可能的庫文件中,但它確實是很難讀,我和大多數的例子似乎是在VB,我不知道,並覺得很難翻譯成C#(特別是涉及到AsyncCallbacks等)。

如果有人能給我一個正確的方向,我會很高興!

回答

0

這不是一個答案,但我不能夠把這麼多的代碼在評論section.Chagne代碼
這些行

//continue listening for other broadcasting devices 
listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener); 

// create a connection to the device that's just been found 
BluetoothClient client = listener.EndAcceptBluetoothClient(); 


// create a connection to the device that's just been found 
BluetoothClient client = listener.EndAcceptBluetoothClient(); 

// continue listening for other broadcasting devices 
listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener); 

基本上,改變的代碼序列..
至於每次調用BeginXXXX方法都必須有下一個EndXXXX。和以上所有代碼,你試圖BeginAcceptBluetoothClient 已經開始「BeginAcceptBluetoothClient」。

希望你明白。

7

前面幾個警告,我假設你沒有在這裏處理HID設備,他們通常由操作系統處理。我也剛剛開始使用32feet,我正在使用它創建與藍牙條形碼掃描器上的串行端口服務的連接,因此可能有更好的方法滿足您的需求,但這可能會指示您開始正確的方向。

您需要配對設備,是的。如果你在一個WinForms應用程序使用它還有的實際上形成可以顯示它處理掃描設備,並允許您選擇一個,像這樣:

bool PairDevice() 
{ 
    using (var discoverForm = new SelectBluetoothDeviceDialog()) 
    { 
     if (discoverForm.ShowDialog(this) != DialogResult.OK) 
     { 
      // no device selected 
      return false; 
     } 

     BluetoothDeviceInfo deviceInfo = discoverForm.SelectedDevice; 

     if (!deviceInfo.Authenticated) // previously paired? 
     { 
      // TODO: show a dialog with a PIN/discover the device PIN 
      if (!BluetoothSecurity.PairDevice(deviceInfo.DeviceAddress, myPin)) 
      { 
       // not previously paired and attempt to pair failed 
       return false; 
      } 
     } 

     // device should now be paired with the OS so make a connection to it asynchronously 
     var client = new BluetoothClient(); 
     client.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, this.BluetoothClientConnectCallback, client); 

     return true; 
    } 
} 

void BluetoothClientConnectCallback(IAsyncResult result) 
{ 
    var client = (BluetoothClient)result.State; 
    client.EndConnect(); 

    // get the client's stream and do whatever reading/writing you want to do. 
    // if you want to maintain the connection then calls to Read() on the client's stream should block when awaiting data from the device 

    // when you're done reading/writing and want to close the connection or the device servers the connection control flow will resume here and you need to tidy up 
    client.Close(); 
} 

到目前爲止,遠離最好的辦法,如果你的設備是廣播他們可用於連接,就是設置一個BluetoothListener,它將持續監聽廣播設備,當發現一個設備時,您將獲得一個BluetoothClient實例,您可以在首次配對時使用相同的實例:

void SetupListener() 
{ 
    var listener = new BluetoothListener(BluetoothService.SerialPort); 
    listener.Start(); 
    listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener); 
} 


void BluetoothListenerAcceptClientCallback(IAsyncResult result) 
{ 
    var listener = (BluetoothListener)result.State; 

    // continue listening for other broadcasting devices 
    listener.BeginAcceptBluetoothClient(this.BluetoothListenerAcceptClientCallback, listener); 

    // create a connection to the device that's just been found 
    BluetoothClient client = listener.EndAcceptBluetoothClient(); 

    // the method we're in is already asynchronous and it's already connected to the client (via EndAcceptBluetoothClient) so there's no need to call BeginConnect 

    // TODO: perform your reading/writing as you did in the first code sample 

    client.Close(); 
} 

不太吸引人,但如果您的設備沒有廣播連接,可以使用cre吃了一個新的BluetoothClient,並要求它返回所有的設備,它可以找到:

void ScanForBluetoothClients() 
{ 
    var client = new BluetoothClient(); 
    BluetoothDeviceInfo[] availableDevices = client.DiscoverDevices(); // I've found this to be SLOW! 

    foreach (BluetoothDeviceInfo device in availableDevices) 
    { 
     if (!device.Authenticated) 
     { 
      continue; 
     } 

     var peerClient = new BluetoothClient(); 
     peerClient.BeginConnect(deviceInfo.DeviceAddress, BluetoothService.SerialPort, this.BluetoothClientConnectCallback, peerClient); 
    } 
}