DEV Community

HarmonyOS
HarmonyOS

Posted on

Obtain Manufacturer Data from Bluetooth Broadcast Packet

Read the original article:Obtain Manufacturer Data from Bluetooth Broadcast Packet

Context

Bluetooth Low Energy (BLE) advertising payloads are composed of Length–Type–Value (LTV) structures. Manufacturer-specific data uses advertising Type 0xFF and contains a 2-byte Company Identifier (little-endian) followed by vendor-defined bytes. Extracting this block enables device identification and custom payload parsing during scans.

Description

  • Each advertising element: [Length][Type][Value…].
  • Manufacturer-specific data (often labeled manufacturerData) is identified by Type = 0xFF.
  • The first 2 bytes of the Value are the Company Identifier; the remaining bytes are vendor specific.
  • The provided helper walks the advertising buffer and returns the first 0xFF block as a Uint8Array.

Solution / Approach

  1. Obtain the raw scan result advertising bytes as an ArrayBuffer from the scanning API.
  2. Pass that buffer into the helper to locate Type 0xFF and slice the corresponding Value.
  3. If the returned array has length 0, no manufacturer-specific block was present.
  4. For demonstration, the sample prints the first six bytes in a MAC-like hex format (interpretation depends on vendor format).
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  test_str: string = 'Hello World';
  private data :ArrayBuffer = new Uint8Array([17,255,84,145,175,64,66,2,1,1,1,1,0,0,0,0,0,0,0,10,9,68,68,80,48,53,52,50,48,50]);


  private data2 :ArrayBuffer = new Uint8Array([30,252,6,0,1,15,32,2,65,16,127,93,105,253,204,139,171,16,211,202,235,173,94,110,247,204,107,204,227,36,189]);
  test1(){
      let manctureData = this.getManctureData(this.data)
      if (manctureData.byteLength ==0){
        console.log("get failed");
      }else{
        console.log(get Mac ${manctureData[0].toString(16)}:${manctureData[1].toString(16)}:${manctureData[2].toString(16)}:${manctureData[3].toString(16)}:${manctureData[4].toString(16)}:${manctureData[5].toString(16)})
      }
  }


  getManctureData ( data : ArrayBuffer ): Uint8Array {


let dataBuf = new Uint8Array(data);
for(let i = 0; i < dataBuf.length; i++){
  if (dataBuf[i+1]!= 0xff){
    i+= dataBuf[i];
  }else{
    return new Uint8Array(data.slice(i+2, i+dataBuf[i]+1))
  }
}
return new Uint8Array(0)

  }


  build() {
    Row() {
      Column() {
        Text(this.message)
          . fontSize ( 50 )
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.test1()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Manufacturer data is identified by AD Type 0xFF within the advertising payload.
  • The helper scans LTV fields and returns the first manufacturer-specific block as a Uint8Array.
  • The first two bytes of that block represent the Company Identifier; subsequent bytes are vendor-defined.
  • Feeding the scan result buffer directly into the helper enables consistent extraction during discovery.

Written by Fatih Turan Gundogdu

Top comments (0)