简体   繁体   English

从 iOS 设备上的 BLE 获取制造商数据

[英]Get Manufacturer Data from BLE on iOS devices

In my Xamarin.Android app, I can read Manufacturer Data from BLE with the following code:在我的 Xamarin.Android 应用程序中,我可以使用以下代码从 BLE 读取制造商数据:

public class CustomScanCallback : ScanCallback
{
    public override void OnScanResult([GeneratedEnum] ScanCallbackType callbackType, ScanResult result)
    {
        base.OnScanResult(callbackType, result);

        if (result.ScanRecord.ManufacturerSpecificData != null)
        {
            var dataByteResult = result.ScanRecord.GetManufacturerSpecificData(0xFFFE);

            if (dataByteResult != null)
            {
                Guid dataResult = new Guid(dataByteResult);

                if (dataResult.ToString() == "myUuid")
                {
                    // found the uuid from my UWP app
                }
            }
        }
    }
}

How can I do the same on Xamarin.iOS?如何在 Xamarin.iOS 上做同样的事情? I have a callback for when peripherals are discovered:当发现外围设备时,我有一个回调:

_cbCentralManager.DiscoveredPeripheral += CBCentralManager_DiscoveredPeripheral;

private void CBCentralManager_DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
{
    // how to find "myUuid" ?
}

And have tried many things to find it, but couldn't.并尝试了很多方法来找到它,但找不到。 An answer in Swift/Obective-C would help too as I can translate it to C#. Swift/Obective-C 中的答案也会有所帮助,因为我可以将其转换为 C#。

EDIT编辑

I looked through the advertisement data parameter with the key CBAdvertisementDataManufacturerDataKey and found the value:我使用键CBAdvertisementDataManufacturerDataKey查看了广告数据参数并找到了值:

<06000109 2002aa4e 7e54b91f 2212c398 74eb0fe9 9fc3ecce 4ce76d8f aa>

It looks like it's encoded in hex format, but don't think it's my uuid that I am looking for...看起来它是以十六进制格式编码的,但不要认为这是我正在寻找的 uuid ......

Hex code is the NSData which you can see it is from these docs.十六进制代码是NSData ,您可以从这些文档中看到它。 To get that into a string I would use the following.要将其转换为字符串,我将使用以下内容。

     NSData data = NSData.FromData(theValueFromDictionary);
     string uuidString = data.ToString();
     //Try this if the other line doesn't work. But string encoding should be UTF-16 (the default) or UTF-8
     //string uuidString = data.ToString(NSStringEncoding.UTF8);

If that isn't your desired string then you could play around with the type of encoding you are using, but in my experience apple usually goes between data and strings with UTF-8 .如果这不是您想要的字符串,那么您可以尝试使用您正在使用的编码类型,但根据我的经验,苹果通常在数据和字符串之间使用UTF-8

Perhaps the UUID is another key in this dictionary.也许 UUID 是这本字典中的另一个键。 Take a look a look at all the available keys here看看这里所有可用的键

You can get the manufacturer info in the advertisementData.您可以在广告数据中获取制造商信息。 Following this code .按照这个代码。

#pragma mark - CBCentralManagerDelegate
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI {
    NSString *manufacturer = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
    NSLog(@"manufacturer:%@", manufacturer); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM