Getting Byte Array From BLE (weight Scale ) Device But Could Not Interpret The Value From It E.g. Weight

89 views Asked by At

I'm getting these buffer array in the console from this BLE device outputImage

"12 11 FF 3E C0 13 00 03 FF 0A 03 05 00 00 05 07 53"

I'm able to connect device and read the this notifiable characteristic Don't know how to convert them to get the value weight. Note: I'm not aware of manufactures Byte Structure that's why it's causing me trouble to find out which byte is for which purpose.

I tired with Web Bluetooth package from angular here is the code

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class BluetoothServiceService {
  private device: BluetoothDevice;
  private characteristic: BluetoothRemoteGATTCharacteristic;
  private  timer:any;
  constructor() { }

  async connectToDevice(): Promise<void> {
    try {
      // Request Bluetooth device
      this.device = await navigator.bluetooth.requestDevice({
        // filters: [{ services: ['0000ffe0-0000-1000-8000-00805f9b34fb'] }],
         optionalServices: ['0000fff0-0000-1000-8000-00805f9b34fb'],
        acceptAllDevices: true
      });
     console.log(this.device.name);
     console.log(this.device.id);
     console.log(this.device.gatt);
      // Connect to the device
      const server = await this.device.gatt.connect();

      // Get the primary service
      const service = await server.getPrimaryService('0000fff0-0000-1000-8000-00805f9b34fb');
      console.log('inside blue3');
      // Get the characteristic you want to read data from
      this.characteristic = await service.getCharacteristic('0000fff1-0000-1000-8000-00805f9b34fb');
      console.log( 'inside blue4');
      // Start receiving data
      await this.characteristic.startNotifications();
      console.log('inside blue5');

      // Handle incoming data
      this.characteristic.addEventListener('characteristicvaluechanged', this.handleData);
      this.timer = setTimeout(() => {
        // Clear the event listener after 5 seconds
        this.characteristic.removeEventListener('characteristicvaluechanged', this.handleData);
      }, 5000);
      console.log('inside blue6');
    } catch (error) {
      console.error('Bluetooth connection error:', error);
    }
  }

  private handleData(event: Event) {
    const value = (event.target as BluetoothRemoteGATTCharacteristic).value;
    // Process the received data here
    console.log(value);
    const dataView = new DataView(value.buffer);
    const uint8Array = new Uint8Array(value.byteLength);
    for (let i = 0; i < value.byteLength; i++) {
      uint8Array[i] = dataView.getUint8(i);
    }
    // Process the Uint8Array or any other data format based on your characteristic value structure
    const stringValue = String.fromCharCode.apply(null, uint8Array);
    console.log('string value',stringValue);
    // Example: Extracting individual values
    const firstValue = uint8Array[0];
    console.log(firstValue);
    clearTimeout(this.timer);
    // return value;
  }
}
0

There are 0 answers