2011-10-14 66 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); 
    } 
}