Extract data from kCBAdvDataManufacturerData on Swift

狂风中的少年 提交于 2020-12-03 07:50:28

问题


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.

I am executing the following in Swift:

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()
    }
}

And this is what I see in the console:

AdvertisementData:["kCBAdvDataIsConnectable": 0, "kCBAdvDataManufacturerData": <0d00fe05 0c6f32>, "kCBAdvDataLocalName": CLIMBC]

The data that I am interested in decoding is kCBAdvDataManufacturerData : <0d00fe05 0c6f32> and displaying each field on the screen. Specifically, this is what the numbers represent in my case:

  1. 0d00 - TI manufacturer ID
  2. fe - the node ID that I have given
  3. 05 - state of the node (something that remains constant
  4. c6f - is the sensor tag battery voltage
  5. 32- is the BLE packet counter.

In Android I am able to decode as following:

private static String getNodeIdFromRawPacket(byte[] manufSpecField) {
    if(manufSpecField != null && manufSpecField.length > 1) {
        return String.format("%02X", manufSpecField[0]);
    }else{
        return null;
    }
}
private static int getNodeBatteryVoltageFromRawPacket(byte[] manufSpecField){
    if(manufSpecField != null && manufSpecField.length > 4) {
        return (((((int) manufSpecField[manufSpecField.length - 3]) << 24) >>> 24) << 8) + ((((int) manufSpecField[manufSpecField.length - 2]) << 24) >>> 24);
    }else{
        return 0;
    }
}

private byte[] extractManufacturerSpecificData(byte[] scanRecord, int manufacturer_id){

     if(scanRecord != null) {
         int ptr = 0;
         while (ptr < scanRecord.length && 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] << 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;
     }
  }
};

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.


回答1:


Seeing the output of your console, advertisementData["kCBAdvDataManufacturerData"] seems to be an NSData containing 7 bytes. You can easily access it as a Swift Data, and each byte in a Data can be accessed with subscript:

if let manufacturerData = advertisementData["kCBAdvDataManufacturerData"] as? Data {
    assert(manufacturerData.count >= 7)
    //0d00 - TI manufacturer ID
    //Constructing 2-byte data as little endian (as TI's manufacturer ID is 000D)
    let manufactureID = UInt16(manufacturerData[0]) + UInt16(manufacturerData[1]) << 8
    print(String(format: "%04X", manufactureID)) //->000D
    //fe - the node ID that I have given
    let nodeID = manufacturerData[2]
    print(String(format: "%02X", nodeID)) //->FE
    //05 - state of the node (something that remains constant
    let state = manufacturerData[3]
    print(String(format: "%02X", state)) //->05
    //c6f - is the sensor tag battery voltage
    //Constructing 2-byte data as big endian (as shown in the Java code)
    let batteryVoltage = UInt16(manufacturerData[4]) << 8 + UInt16(manufacturerData[5])
    print(String(format: "%04X", batteryVoltage)) //->0C6F
    //32- is the BLE packet counter.
    let packetCounter = manufacturerData[6]
    print(String(format: "%02X", packetCounter)) //->32
}



回答2:


Here is an implementation of swift 3 Data method subdata with an example of a string converted to data and then split out to bytes that you can convert back to strings:

let input = "505450578"
let data = input.data(using: .utf8)

let manufacturerId:Range<Int> = 0..<2
let nodeId:Range<Int> = 2..<4
let nodeState:Range<Int> = 4..<5
let voltage:Range<Int> = 5..<6
let packetCounter:Range<Int> = 6..<9

let subdata1 = data?.subdata(in: manufacturerId)
let subdata2 = data?.subdata(in: nodeId)
let subdata3 = data?.subdata(in: nodeState)
let subdata4 = data?.subdata(in: voltage)
let subdata5 = data?.subdata(in: packetCounter)

//Results from original given string
let str1 = String(data: subdata1!, encoding:.utf8) //50
let str2 = String(data: subdata2!, encoding:.utf8) //54
let str3 = String(data: subdata3!, encoding:.utf8) //5
let str4 = String(data: subdata4!, encoding:.utf8) //0
let str5 = String(data: subdata5!, encoding:.utf8) //578



回答3:


Updated for Swift:

let yourServiceUUIDString = "FFF0"
if let mAdvData = advertisementData["kCBAdvDataServiceUUIDs"] as? [AnyObject], (mAdvData.contains { ($0 as? CBUUID)?.uuidString == yourServiceUUIDString}) {
    print("BLE device found..!")
}


来源:https://stackoverflow.com/questions/42140636/extract-data-from-kcbadvdatamanufacturerdata-on-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!