Reading Raw BLE Advertising Data

crecre USUniversity ✭✭
edited June 2015 in Xamarin.iOS

Hi everybody

I am trying to read the raw BLE advertising data on an iPhone. According to this SO post it should be possible as long as the advertisment data is NOT in the iBeacon format.

To test this I am using two iPhones. One to send advertisments and one to receive them.

The code to send advertisments looks like this:

`

        string dataServiceUUIDsKey = "71DA3FD1-7E10-41C1-B16F-4430B5060000";
        string customBeaconServiceUUIDsKey = "71DA3FD1-7E10-41C1-B16F-4430B5060001";
        string customBeaconCharacteristicUUIDKey = "71DA3FD1-7E10-41C1-B16F-4430B5060002";
        string identifier = "71DA3FD1-7E10-41C1-B16F-4430B5060003";

    var peripheralDelegate = new PeripheralManagerDelegate();
        peripheralManager = new CBPeripheralManager(peripheralDelegate, DispatchQueue.DefaultGlobalQueue);

        var customBeaconServiceUUID = CBUUID.FromString(customBeaconServiceUUIDsKey);
        var customBeaconCharacteristicUUID = CBUUID.FromString(customBeaconCharacteristicUUIDKey);

        var service = new CBMutableService(customBeaconServiceUUID, true);
        var dataUUID = NSData.FromString(identifier, NSStringEncoding.UTF8);

        var characteristic = new CBMutableCharacteristic(
            customBeaconCharacteristicUUID,
            CBCharacteristicProperties.Read,
            dataUUID,
            CBAttributePermissions.Readable);
        service.Characteristics = new CBCharacteristic[] { characteristic };
        peripheralManager.AddService(service);

        var localName = new NSString("CustomBeacon");

        var advertisingData = new NSDictionary(
            CBAdvertisement.DataLocalNameKey,
            localName,
            CBAdvertisement.IsConnectable,
            FromObject(false),
            CBAdvertisement.DataManufacturerDataKey,
            CBUUID.FromString(dataServiceUUIDsKey),
            CBAdvertisement.DataServiceUUIDsKey,
            CBUUID.FromString(dataServiceUUIDsKey));

        peripheralManager.StartAdvertising(advertisingData);`

And the app to discover and read the advertisment data I am using this data:

`

    public void Initialize()
    {
        this.centralManager = new CBCentralManager(DispatchQueue.DefaultGlobalQueue);
        this.centralManager.RetrievedPeripherals += CentralManagerOnRetrievedPeripherals;
        this.centralManager.DiscoveredPeripheral += CentralManagerOnDiscoveredPeripheral;
        this.centralManager.UpdatedState += CentralManagerOnUpdatedState;
        this.centralManager.Init();
    }

    private void CentralManagerOnUpdatedState(object sender, EventArgs eventArgs)
    {
        if (centralManager.State == CBCentralManagerState.PoweredOn)
        {
            //var options = new NSDictionary(CBCentralManager.ScanOptionAllowDuplicatesKey, true);
            this.centralManager.ScanForPeripherals(null, new PeripheralScanningOptions{AllowDuplicatesKey = true});
        }
    }

    private void CentralManagerOnDiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
    {
        if (e.AdvertisementData.ContainsKey(CBAdvertisement.DataManufacturerDataKey))
        {
            // For debugging
            var data = e.AdvertisementData[CBAdvertisement.DataManufacturerDataKey];
        }
        Console.WriteLine(e.AdvertisementData);
    }


    private void CentralManagerOnRetrievedPeripherals(object sender, CBPeripheralsEventArgs e)
    {
        this.logService.WriteLog("Retrieved Peripherals");
    }`

Unfortunately I don't see any data in the advertisment.
Am I doing something completely wrong?

Tagged:

Answers

  • ireshikaireshika USMember ✭✭
                var UUI = new CBUUID[] { CBUUID.FromString("E20A39F4-73F5-4BC4-A12F-17D1AD07A961") };
    
                NSArray arry = NSArray.FromObjects(UUI);
                var test=NSObject.FromObject(arry);
                var advertisingData = new NSDictionary(
                    CBAdvertisement.DataServiceUUIDsKey,
                test);
    
                ph.StartAdvertising(advertisingData) ;
    
Sign In or Register to comment.