【问题标题】:32feet.net howto discover nearby bluetooth devices async in c#32feet.net 如何在 c# 中异步发现附近的蓝牙设备
【发布时间】:2019-03-02 18:05:01
【问题描述】:

我正在尝试在 C# 应用程序中使用 32feet.NET 蓝牙库来检测附近的设备。我的小应用程序的目的是通过人们手机的蓝牙功能让 PC 知道房间里有谁。

执行此类操作的最佳方法是让我想要“跟踪”的设备连接一次,然后不断检查它们是否可以通过蓝牙检测到。

现在我的问题:

  1. 我是否需要将设备与我的应用程序配对或验证?如何使用 32feet.NET 在 C# 中执行此操作?

  2. 如何持续检查范围内的设备并将其与存储的设备进行比较?

我知道所有这些可能都在库文档中,但对我来说真的很难阅读,而且大多数示例似乎都是在 VB 中,我不知道并且很难翻译成 C#(尤其是当它涉及到 AsyncCallbacks 等)。

如果有人能推动我朝着正确的方向前进,我会非常高兴!

【问题讨论】:

    标签: c# asynchronous bluetooth discovery


    【解决方案1】:

    前面有几个警告,我假设您在这里处理的不是 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();
    }
    

    如果您的设备没有为连接进行广播,那么吸引力不大,但很有用,您可以创建一个新的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);
        }
    }
    

    【讨论】:

      【解决方案2】:

      这不是答案,但我无法将这么多代码放在评论部分。更改以下代码行:

      //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”。

      希望你能理解。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多