3

In my Xamarin.Android app, I can read Manufacturer Data from BLE with the following code:

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? 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#.

EDIT

I looked through the advertisement data parameter with the key CBAdvertisementDataManufacturerDataKey and found the value:

<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...

Drake
  • 2,679
  • 4
  • 45
  • 88
  • https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/1518937-centralmanager?language=objc should be called. Then, in the `advertisementData` dictionary parameter, should might have a key (`CBAdvertisementDataManufacturerDataKey`)/value for the manufacturer. I don't remember if the value is of type `NSString` or `NSData`, but you should get it. – Larme Mar 11 '19 at 16:00
  • I edited my post – Drake Mar 11 '19 at 16:10
  • Look through this link. https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/advertisement_data_retrieval_keys?language=objc Have you tried `CBAdvertisementDataServiceUUIDsKey` ? – NSGangster Mar 11 '19 at 17:50
  • https://developer.apple.com/documentation/corebluetooth/cbadvertisementdatamanufacturerdatakey?language=objc If you look here it is NSData, so you'd need to create the NSData object and then create a string from it. – NSGangster Mar 11 '19 at 17:52
  • It's not in the `CBAdvertisementDataServiceUUIDsKey`, I checked it. The app that I'm reading it from put the uuid in the manufacturer data as a string. I know becuase I can read on my Android app by calling `GetManufacturerSpecificData` – Drake Mar 11 '19 at 20:11
  • 1
    You may please checkout following link it might be helpful for you. https://stackoverflow.com/a/22837799/4843725 or if it does not works then checkout this one. https://stackoverflow.com/a/35059094/4843725 – Faiz Fareed Mar 20 '19 at 20:07
  • I read through those posts, and what I understood from them is that it's not possible. – Drake Mar 21 '19 at 14:00

2 Answers2

0

Hex code is the NSData which you can see it is from these docs. 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.

Perhaps the UUID is another key in this dictionary. Take a look a look at all the available keys here

NSGangster
  • 2,397
  • 12
  • 22
  • `data.ToString()` returns the same string in hex format. `data.ToString(NSStringEncoding.UTF8)` throws an exception – Drake Mar 11 '19 at 19:49
  • Have you taken a look at any of the other keys? – NSGangster Mar 13 '19 at 17:48
  • Yes I searched through all of them. In my Android implementation, I'm looking in `0xFFFE` as a key, not sure how to do that in iOS.. – Drake Mar 13 '19 at 18:08
0

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); 
}
刘俊利
  • 358
  • 4
  • 12