菜鸟教程小白 发表于 2022-12-11 19:25:01

ios - 如何在 Xamarin 中正确地将服务添加到 CBPeripheralManager


                                            <p><p>我正在尝试使用 Visual Studio for Mac 将服务添加到 IOS 的 CBPeripheralManager。但是,当我使用其他设备扫描我的外围设备时,我没有看到我添加的服务。广告名称是正确的,但我只看到其他服务(大概是iphone默认的)。</p>

<p>顺便说一句,我用 swift 编写了程序并遇到了同样的问题。</p>

<p>我的xcode版本是9,我的ios目标版本是11。</p>

<p>我的代码如下</p>

<pre><code>CBPeripheralManager _peripheral;
public BLEPeripheral()
    {
      _peripheral = new CBPeripheralManager();//this, DispatchQueue.CurrentQueue);
      _peripheral.CharacteristicSubscribed += (object sender, CBPeripheralManagerSubscriptionEventArgs e) =&gt;
      {
            Console.WriteLine(&#34;Device Subscribed: {0}&#34;, e.Central.UUID);
            _connectedDevices.Add(e.Central);
            ServiceSubscribed(this, e);
      };

      _peripheral.WriteRequestsReceived += (object sender, CBATTRequestsEventArgs e) =&gt; {
            Console.WriteLine(&#34;Write Request Received&#34;);
      };


      _peripheral.ServiceAdded += (object sender, CBPeripheralManagerServiceEventArgs e) =&gt;
      {
            Console.WriteLine(&#34;Service {0} was added&#34;, e.Error);
            Console.WriteLine(e.Service.Characteristics.Length.ToString());
            Console.WriteLine(e.Service.UUID.ToString());

            IsAdvertising = true;
            StartAdvertisingOptions advOptions = new StartAdvertisingOptions();
            advOptions.LocalName = &#34;myDevice&#34;;

            _peripheral.StartAdvertising(advOptions);
            Console.WriteLine(&#34;starting the advertisement&#34;);
      };

      _peripheral.AdvertisingStarted += (object sender, Foundation.NSErrorEventArgs e) =&gt;
      {
            Console.WriteLine(&#34;Advertising did start&#34;);
      };

      _peripheral.StateUpdated += (object sender, EventArgs e) =&gt;
      {
            Console.WriteLine(&#34;UpdatedState: {0}&#34;, _peripheral.State);
      };

    }
</code></pre>

<p>我的服务设置代码是:</p>

<pre><code>public void setupService()
    {
      txChar = new CBMutableCharacteristic(txCharUUID, CBCharacteristicProperties.Notify, null, CBAttributePermissions.Readable);
      var MyServiceUUID = CBUUID.FromString(&#34;A66C4FFC-52C6-4C04-B97C-34E5E91DG5BG&#34;);

      MyService = new CBMutableService(MyServiceUUID, true);
      MyService.Characteristics = new CBMutableCharacteristic[] { txChar };
      _peripheral.AddService(MyService);

    }
</code></pre>

<p>peripheralmanager 的状态为 PoweredOn。在按下添加服务并开始广告的按钮之前,我会检查这一点。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的解决方案不起作用的<strong>主要原因</strong>可能是 <code>CBPeripheralManager</code> 方法必须仅在其处于 <code>State==PoweredOn</code> 时使用。 <a href="https://developer.apple.com/documentation/corebluetooth/cbperipheralmanager" rel="noreferrer noopener nofollow">Apple documentation</a>在概述中说明了这一点。</p>

<p>我可以在您的示例中看到另一个潜在的小问题。在 <code>StartAdvertisingOption</code> 中,您没有设置 <strong>ServiceUUID</strong>,因此您没有宣传您的服务。应该是 <code>StartAdvertisingOptions advOptions = new StartAdvertisingOptions { LocalName = "myDevice", ServicesUUID = new CBUUID[] { _uuidService } };</code></p>

<p>下面是一个可行的解决方案,我在 <code>UpdateState</code> 事件中移动了所有 <code>CBPeripheralManager</code> 逻辑:</p>

<pre><code>public class BleGattServerManager
{
    private CBPeripheralManager _PeripheralManager;
    private CBUUID _uuidService = CBUUID.FromString(&#34;625D74A2-3E61-4D1E-8949-8BE42DFDC6DA&#34;);

    public BleGattServerManager()
    {
      //set up peripheral
      _PeripheralManager = new CBPeripheralManager();
      _PeripheralManager.StateUpdated += _PeripheralManager_StateUpdated;
      _PeripheralManager.AdvertisingStarted += _PeripheralManager_AdvertisingStarted;
      _PeripheralManager.ServiceAdded += _PeripheralManager_ServiceAdded;
    }

    private void _PeripheralManager_ServiceAdded(object sender, CBPeripheralManagerServiceEventArgs e)
    {
      System.Diagnostics.Debug.WriteLine(&#34;Service added&#34;);
    }

    private void _PeripheralManager_AdvertisingStarted(object sender, NSErrorEventArgs e)
    {
      System.Diagnostics.Debug.WriteLine(&#34;Advertising started&#34;);
    }

    private void _PeripheralManager_StateUpdated(object sender, EventArgs e)
    {
      System.Diagnostics.Debug.WriteLine(&#34;State=&#34; + _PeripheralManager.State);

      if (_PeripheralManager.State == CBPeripheralManagerState.PoweredOn)
      {
            //set up the GATT service
            CBUUID uuidCharact = CBUUID.FromString(&#34;5BDAFB34-DA3F-40AA-8F9C-8AD409CB0063&#34;);
            NSData readme = NSData.FromString(&#34;readme!&#34;);
            CBMutableCharacteristic _CharacRead = new CBMutableCharacteristic(uuidCharact, CBCharacteristicProperties.Read, readme, CBAttributePermissions.Readable);
            CBMutableService service = new CBMutableService(_uuidService, true);
            service.Characteristics = new CBMutableCharacteristic[] { _CharacRead };

            _PeripheralManager.AddService(service);

            StartAdvertisingOptions advData = new StartAdvertisingOptions { LocalName = &#34;my GATT!&#34;, ServicesUUID = new CBUUID[] { _uuidService } };
            _PeripheralManager.StartAdvertising(advData);
      }
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Xamarin 中正确地将服务添加到 CBPeripheralManager,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/47716181/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/47716181/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Xamarin 中正确地将服务添加到 CBPeripheralManager