【问题标题】:How to add services to CBPeripheralManager correctly in Xamarin如何在 Xamarin 中正确地将服务添加到 CBPeripheralManager
【发布时间】:2018-05-22 19:22:11
【问题描述】:

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

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

我的xcode版本是9,我的ios目标版本是11。

我的代码如下

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

        _peripheral.WriteRequestsReceived += (object sender, CBATTRequestsEventArgs e) => {
            Console.WriteLine("Write Request Received");
        };


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

            IsAdvertising = true;
            StartAdvertisingOptions advOptions = new StartAdvertisingOptions();
            advOptions.LocalName = "myDevice";

            _peripheral.StartAdvertising(advOptions);
            Console.WriteLine("starting the advertisement");
        };

        _peripheral.AdvertisingStarted += (object sender, Foundation.NSErrorEventArgs e) =>
        {
            Console.WriteLine("Advertising did start");
        };

        _peripheral.StateUpdated += (object sender, EventArgs e) =>
        {
            Console.WriteLine("UpdatedState: {0}", _peripheral.State);
        };

    }

我的服务设置代码是:

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

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

    }

peripheralmanager 的状态是 PoweredOn。在按下添加服务并开始广告的按钮之前,我会检查这一点。

【问题讨论】:

    标签: ios xamarin bluetooth-lowenergy core-bluetooth


    【解决方案1】:

    您的解决方案不起作用的主要原因可能是 CBPeripheralManager 方法只能在 State==PoweredOn 中使用。 Apple documentation 在概述中说明了这一点。

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

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

    public class BleGattServerManager
    {
        private CBPeripheralManager _PeripheralManager;
        private CBUUID _uuidService = CBUUID.FromString("625D74A2-3E61-4D1E-8949-8BE42DFDC6DA");
    
        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("Service added");
        }
    
        private void _PeripheralManager_AdvertisingStarted(object sender, NSErrorEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Advertising started");
        }
    
        private void _PeripheralManager_StateUpdated(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("State=" + _PeripheralManager.State);
    
            if (_PeripheralManager.State == CBPeripheralManagerState.PoweredOn)
            {
                //set up the GATT service
                CBUUID uuidCharact = CBUUID.FromString("5BDAFB34-DA3F-40AA-8F9C-8AD409CB0063");
                NSData readme = NSData.FromString("readme!");
                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 = "my GATT!", ServicesUUID = new CBUUID[] { _uuidService } };
                _PeripheralManager.StartAdvertising(advData);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多