无法发现我正在宣传的CBServices

时间:2018-08-25 12:22:36

标签: ios xamarin bluetooth-lowenergy core-bluetooth

我正在创建一个自定义CBService并将其添加到CBPeripheralManager。我正在与服务广告一起发布服务。但是我看不到在Light Blue应用程序中创建的服务。浅蓝色应用显示了两个服务,我不知道它们是什么,也没有添加。显示广告数据,但不显示服务。以下是我在Xamarin中创建服务并将其发布的实现:

void AdvertiseData()
    {
        var uui = new CBUUID[] { CBUUID.FromString("E20A39F4-73F5-4BC4-A12F-17D1AD07A961") };

        var nsArray = NSArray.FromObjects(uui);
        var nsObject = NSObject.FromObject(nsArray);

        var manufacturerDataBytes = new byte[6] { 5, 255, 76, 0, 25, 35 };

        var advertisementData = new NSDictionary(

             CBAdvertisement.DataLocalNameKey, "id1",
             CBAdvertisement.DataServiceUUIDsKey, nsObject,
             CBAdvertisement.DataManufacturerDataKey, NSData.FromArray(manufacturerDataBytes));


        try
        {
            var newService = new CBMutableService(CBUUID.FromString("1F3A8B1E-4BAA-45DA-B3A4-7A19DDC86305"), false);//1F3A8B1E-4BAA-45DA-B3A4-7A19DDC86305
            newService.Characteristics = new CBCharacteristic[]
              {
                new CBMutableCharacteristic(CBUUID.FromString("906377DD-F782-448C-9A65-796B48E10894"),//906377DD-F782-448C-9A65-796B48E10894
                CBCharacteristicProperties.Read, "FooChar",
                CBAttributePermissions.Readable),
                new CBMutableCharacteristic(CBUUID.FromString("2B77EC99-BE87-45D3-863C-599D5202E4C0"),//2B77EC99-BE87-45D3-863C-599D5202E4C0
                CBCharacteristicProperties.Write, null,
                CBAttributePermissions.Writeable)
              };

            cbPeriphMang.AddService(newService);
            //cbPeriphMang.RemoveAllServices();
        }
        catch(Exception ex)
        {

        }


        if (cbPeriphMang.Advertising) cbPeriphMang.StopAdvertising();

        cbPeriphMang.StartAdvertising(new StartAdvertisingOptions(){LocalName="alpha", ServicesUUID = new CBUUID[] { CBUUID.FromString("1F3A8B1E-4BAA-45DA-B3A4-7A19DDC86305")} } );
    }

0 个答案:

没有答案