title: Extract data from kCBAdvDataManufacturerData on Swift
body: <p>I have a TI sensor Tag as a peripheral that broadcasts BLE data in the form of kCBAdvDataManufacturerData. I would like to extract different values from this data in iOS.</p>
  
  <p>I am executing the following in Swift:</p>
  
  <pre><code>func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){     
       for (index, foundPeripheral) in peripherals.enumerated(){
           if foundPeripheral.peripheral?.identifier == peripheral.identifier{
               peripherals[index].lastRSSI = RSSI
               print("AdvertisementData:\(advertisementData)")
               return
  
           }
       }  
  
       let isConnectable = advertisementData["kCBAdvDataIsConnectable"] as! Bool
       let displayPeripheral = DisplayPeripheral(peripheral: peripheral, lastRSSI: RSSI, isConnectable: isConnectable)
       peripherals.append(displayPeripheral)
       tableView.reloadData()
      }
  }
  </code></pre>
  
  <p>And this is what I see in the console:</p>
  
  <p>AdvertisementData:["kCBAdvDataIsConnectable": 0, "kCBAdvDataManufacturerData": &lt;0d00fe05 0c6f32>, "kCBAdvDataLocalName": CLIMBC]</p>
  
  <p>The data that I am interested in decoding is  kCBAdvDataManufacturerData : &lt;0d00fe05 0c6f32> and displaying each field on the screen.
  Specifically, this is what the numbers represent in my case:</p>
  
  <ol>
  <li>0d00 - TI manufacturer ID</li>
  <li>fe - the node ID that I have given</li>
  <li>05 - state of the node (something that remains constant</li>
  <li>c6f - is the sensor tag battery voltage</li>
  <li>32- is the BLE packet counter.</li>
  </ol>
  
  <p>In Android I am able to decode as following:</p>
  
  <pre><code>private static String getNodeIdFromRawPacket(byte[] manufSpecField) {
      if(manufSpecField != null &amp;&amp; manufSpecField.length &gt; 1) {
          return String.format("%02X", manufSpecField[0]);
      }else{
          return null;
      }
  }
  private static int getNodeBatteryVoltageFromRawPacket(byte[] manufSpecField){
      if(manufSpecField != null &amp;&amp; manufSpecField.length &gt; 4) {
          return (((((int) manufSpecField[manufSpecField.length - 3]) &lt;&lt; 24) &gt;&gt;&gt; 24) &lt;&lt; 8) + ((((int) manufSpecField[manufSpecField.length - 2]) &lt;&lt; 24) &gt;&gt;&gt; 24);
      }else{
          return 0;
      }
  }
  
  private byte[] extractManufacturerSpecificData(byte[] scanRecord, int manufacturer_id){
  
       if(scanRecord != null) {
           int ptr = 0;
           while (ptr &lt; scanRecord.length &amp;&amp; scanRecord[ptr] != 0) {
               int field_length = scanRecord[ptr];
               if (scanRecord[ptr + 1] == (byte) (0xFF)) { //this is true when the manufacturer specific data field has been found
                   if (((scanRecord[ptr + 3] &lt;&lt; 8) + scanRecord[ptr + 2]) == manufacturer_id) {
                      byte[] manufacturerSpecificData = new byte[field_length - 3];
                       System.arraycopy(scanRecord, ptr + 4, manufacturerSpecificData, 0, field_length - 3);
                       return manufacturerSpecificData;
                   }
               }
               ptr += (field_length + 1);
           }
           return null;
       }else{
          return null;
       }
    }
  };
  </code></pre>
  
  <p>How exactly can I achieve this? I am new to Swift that is why I am finding some difficulties. Any code snippet will be most welcome.</p>