C#_SDK_Quick star

C#_SDK_quick start

Where to download

Please check the below link to download

https://github.com/WITMOTION/WitBluetooth_BWT901BLE5_0

What is BLE 5.0 protocol?

Bluetooth 5.0 protocol: It is the protocol used by WitMotion Bluetooth 5.0 sensors; the protocol stipulates that the sensor returns data packets beginning with 55, and the host computer sends data packets beginning with FF AA; Bluetooth 5.0 protocol: It is the protocol used by WitMotion Bluetooth

Routine Introduction

This routine introduces how to use C# to develop the upper computer to connect to the Bluetooth 5.0 protocol sensor, receive sensor data and communicate with the sensor; Before viewing this routine, please read the relevant sensor manual to understand the protocol used by the sensor and the basic functions of the sensor

Routine Diretory

The routine project directory is as follows

Dll: The dependent files of the project, please import these dlls into your project before running the project.

Form1:There is only one Form window in the routine, all logic codes are in the Form window file, and there are no other files

The BWT901BLE object represents the BWT901BLE device in the program, and you can communicate with the device through it; when searching for devices, call bluetoothManager.StartScan() to start searching; when opening the device, you need to specify the Bluetooth address of the Bluetooth sensor, and call BWT901BLE.Open() after specifying method

/// <summary>
/// start searching
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void startScanButton_Click(object sender, EventArgs e)
{
      // Clear found devices
      FoundDeviceDict.Clear();
      // Close previously opened devices
      for (int i = 0; i < FoundDeviceDict.Count; i++)
      {
            var keyValue = FoundDeviceDict.ElementAt(i);
            BWT901BLE bWT901BLE = keyValue.Value;
            bWT901BLE.Close();
      }
      // Let the bluetooth manager start searching for devices
      WitBluetoothManager bluetoothManager =                   WitBluetoothManagerHelper.GetWitBluetoothManager();
      bluetoothManager.OnDeviceFound += new       WitBluetoothManager.OnDeviceFoundHalder(OnFoundDevice);
      bluetoothManager.OnDeviceStatu += new       WitBluetoothManager.OnDeviceStatuHalder(OnDeviceStatu);
      bluetoothManager.StartScan();
}
/// <summary>
///This method will be called back when a Bluetooth device is found
/// </summary>
/// <param name="macAddr"></param>
/// <param name="sName"></param>
/// <param name="dType"></param>
/// <param name="mType"></param>
private void OnFoundDevice(string macAddr, string sName, int dType, int mType)
{
      // If the Bluetooth device starting with WT is found
      if (sName != null && sName.Contains("WT"))
      {
            / If the device is newly found
            if (FoundDeviceDict.ContainsKey(macAddr) == false)
            {
                  BWT901BLE bWT901BLE = new BWT901BLE();
                  //Specify the Bluetooth MAC code for the connection
                  bWT901BLE.SetMacAddr(macAddr);
                  // set device name
                  bWT901BLE.SetDeviceName(sName);
                  FoundDeviceDict.Add(macAddr, bWT901BLE);
                  // turn on this device
                  bWT901BLE.Open();
                  bWT901BLE.OnRecord += BWT901BLE_OnRecord;
            }
      }
}

Close the search and call the bluetoothManager.StopScan() method

/// <summary>
/// stop searching
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void stopScanButton_Click(object sender, EventArgs e)
{
      // Tell the bluetooth manager to stop searching
      WitBluetoothManager bluetoothManager =             WitBluetoothManagerHelper.GetWitBluetoothManager();
      bluetoothManager.StopScan();
}

Receive the sensor data

Get data

The BWT901BLE object will automatically calculate the sensor data and save it on itself. The sensor data can be obtained through the BWT901BLE.GetDeviceData() method. BWT901BLE.GetDeviceData() needs to pass in a key to get sensor data. Please check the key that needs to be used in the routine, the key is stored in the WitSensorKey class

/// <summary>
/// Get device data
/// </summary>
private string GetDeviceData(BWT901BLE BWT901BLE)
{
      StringBuilder builder = new StringBuilder();
      builder.Append(BWT901BLE.GetDeviceName()).Append("\n");

// acceleration
builder.Append("AccX").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AccX)).Append("g \t");
 builder.Append("AccY").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AccY)).Append("g \t");
 builder.Append("AccZ").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AccZ)).Append("g \n");

// angular velocity
builder.Append("GyroX").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AsX)).Append("°/s \t");
 builder.Append("GyroY").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AsY)).Append("°/s \t");
 builder.Append("GyroZ").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AsZ)).Append("°/s \n");

// angle
builder.Append("AngleX").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AngleX)).Append("° \t");
 builder.Append("AngleY").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AngleY)).Append("° \t");
 builder.Append("AngleZ").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.AngleZ)).Append("° \n");

// magnetic field
builder.Append("MagX").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.HX)).Append("uT \t");
 builder.Append("MagY").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.HY)).Append("uT \t");
 builder.Append("MagZ").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.HZ)).Append("uT \n");

//version number
builder.Append("VersionNumber").Append(":").Append(BWT901BLE.GetDeviceData(WitSensorKey.VersionNumber)).Append("\n");
      return builder.ToString();
}

Record data

The data of the sensor can be obtained through the BWT901BLE object, but usually the host computer needs to record the data of the sensor. BWT901BLE has an OnRecord event that will notify you when the data should be recorded, and the OnRecord event can be realized when the device is turned on; and then through cooperation with BWT901BLE.GetDeviceData( ) method to record the data

/// <summary>
/// This method will be called back when a Bluetooth device is found
/// </summary>
/// <param name="macAddr"></param>
/// <param name="sName"></param>
/// <param name="dType"></param>
/// <param name="mType"></param>
private void OnFoundDevice(string macAddr, string sName, int dType, int mType)
{
      // If the Bluetooth device starting with WT is found
      if (sName != null && sName.Contains("WT"))
      {
            // If the device is newly found
            if (FoundDeviceDict.ContainsKey(macAddr) == false)
            {
                  BWT901BLE bWT901BLE = new BWT901BLE();
                  // Specify the Bluetooth MAC code for the connection
                  bWT901BLE.SetMacAddr(macAddr);
                  // set device name
                  bWT901BLE.SetDeviceName(sName);
                  FoundDeviceDict.Add(macAddr, bWT901BLE);
                  // turn on this device
                  bWT901BLE.Open();
                  bWT901BLE.OnRecord += BWT901BLE_OnRecord;
            }
      }
}
/// <summary>
/// This is called when the sensor data is refreshed, you can log the data here
/// </summary>
/// <param name="BWT901BLE"></param>
private void BWT901BLE_OnRecord(BWT901BLE BWT901BLE)
{
      string text = GetDeviceData(BWT901BLE);
      Debug.WriteLine(text);
}

Setting the sensor

The sensor can be operated by means of BWT901BLE

BWT901BLE.UnlockReg() Send unlock register command

BWT901BLE.AppliedCalibration() Send accelerometer calibration

BWT901BLE.StartFieldCalibration() Send start magnetic field calibration command

BWT901BLE.EndFieldCalibration() Send end magnetic field calibration command

BWT901BLE.SendProtocolData() send other commands

Accelerometer Clibration

Applied calibration of the sensor by calling the BWT901BLE.AppliedCalibration() method

/// <summary>
/// Accelerometer Calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void appliedCalibrationButton_Click(object sender, EventArgs e)
{
      // All connected Bluetooth devices are calibrated
      for (int i = 0; i < FoundDeviceDict.Count; i++)
      {
            var keyValue = FoundDeviceDict.ElementAt(i);
            BWT901BLE bWT901BLE = keyValue.Value;
            if (bWT901BLE.IsOpen() == false)
            {
                  return;
            }
            try
            {
                  // Unlock registers and send commands
                  bWT901BLE.UnlockReg();
                  bWT901BLE.AppliedCalibration();
                  // The following two lines are equivalent to the above, it is recommended to use the above
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x01, 0x00 });
            }
            catch (Exception ex)
            {
                  MessageBox.Show(ex.Message);
            }
      }
}

Magnetic field calibration

Calibrate the magnetic field of the sensor by calling the BWT901BLE.StartFieldCalibration() method and the BWT901BLE.EndFieldCalibration() method

/// <summary>
///Start Magnetic Field Calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void startFieldCalibrationButton_Click(object sender, EventArgs e)
{
      // Start magnetic field calibration of all connected bluetooth devices
      for (int i = 0; i < FoundDeviceDict.Count; i++)
      {
            var keyValue = FoundDeviceDict.ElementAt(i);
            BWT901BLE bWT901BLE = keyValue.Value;
            if (bWT901BLE.IsOpen() == false)
            {
                  return;
            }
            try
            {
                  // Unlock registers and send commands
                  bWT901BLE.UnlockReg();
                  bWT901BLE.StartFieldCalibration();
                  // The following two lines are equivalent to the above, it is recommended to use the above
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x07, 0x00 });
                  MessageBox.Show("Start magnetic field calibration, please turn around the sensor's XYZ three axes, and click [End Magnetic Field Calibration]");
            }
            catch (Exception ex)
            {
                  MessageBox.Show(ex.Message);
            }
      }
}
/// <summary>
/// End magnetic field calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void endFieldCalibrationButton_Click(object sender, EventArgs e)
{
      // End magnetic field calibration for all connected bluetooth devices
      for (int i = 0; i < FoundDeviceDict.Count; i++)
      {
            var keyValue = FoundDeviceDict.ElementAt(i);
            BWT901BLE bWT901BLE = keyValue.Value;
            if (bWT901BLE.IsOpen() == false)
            {
                  return;
            }
            try
            {
                  //Unlock registers and send commands
                  bWT901BLE.UnlockReg();
                  bWT901BLE.EndFieldCalibration();
                  // The following two lines are equivalent to the above, it is recommended to use the above
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x00, 0x00 });
            }
            catch (Exception ex)
            {
                  MessageBox.Show(ex.Message);
            }
      }
}

More

Pleaes reference to the sensor datasheet.

Read sensor register

The register of the sensor can be read through the BWT901BLE.SendReadReg() method, or the BWT901BLE.SendProtocolData() method can be used

After sending the read command, the register value will be saved in BWT901BLE, you need to get the register data through BWT901BLE.GetDeviceData()

/// <summary>
/// Read 03 register
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void readReg03Button_Click(object sender, EventArgs e)
{
      string reg03Value = "";
      // Read the 03 registers of all connected bluetooth devices
      for (int i = 0; i < FoundDeviceDict.Count; i++)
      {
            var keyValue = FoundDeviceDict.ElementAt(i);
            BWT901BLE bWT901BLE = keyValue.Value;
            if (bWT901BLE.IsOpen() == false)
            {
                  return;
            }
            try
            {
                  // waiting time
                  int waitTime = 3000;
                  // Send a read command and wait for the sensor to return data. If it is not read, you can extend the waitTime, or read it several times
                  bWT901BLE.SendReadReg(0x03, waitTime);
                  // The following line is equivalent to the above. It is recommended to use the above
                  //bWT901BLE.SendProtocolData(new byte[] { 0xff, 0xaa, 0x27, 0x03, 0x00 }, waitTime);
                  // Get the value of all connected bluetooth devices
                  reg03Value += bWT901BLE.GetDeviceName() + "The value of register 03:" +bWT901BLE.GetDeviceData("03") + "\r\n";
            }
            catch (Exception ex)
            {
                  MessageBox.Show(ex.Message);
            }
      }
      MessageBox.Show(reg03Value);
}

BWT901BLE API

Method

Illustrate

Parameter introduction

Return value

void SetMacAddr(string macAddr)

Set the bluetooth address to open

macAddr:bluetooth address

void

void SetDeviceName(string DeviceName)

set device name

DeviceName:device name

void

void Open()

turn on the device

none

void

bool IsOpen()

Is the device turned on

none

返回是否打开打开:true关闭:false

void Close()

turn off the device

nones

void

void SendData(byte[] data, out byte[] returnData, bool isWaitReturn, int waitTime , int repetition)

send data

data: the data to be sent returnData: the data returned by the sensor isWaitReturn: Whether the sensor needs to return data waitTime: wait for the sensor to return data time, unit ms, default 100ms repetition: the number of times to repeat sending

void

void SendProtocolData(byte[] data)

send protocol data

data:data to send

void

void SendProtocolData(byte[] data, int waitTime)

Send data with protocol, and specify the waiting time

data:data to send

waitTime:waiting time

void

void SendReadReg(byte reg, int waitTime)

Send the command to read the register

reg:command to send

waitTime:waiting time

void

void UnlockReg()

unlock register

none

void

void SaveReg()

save register

none

void

void AppliedCalibration()

Accelerometer Calibration

none

void

void StartFieldCalibration()

Start Magnetic Field Calibration

none

void

void EndFieldCalibration()

End magnetic field calibration

none

void

void SetReturnRate(byte rate)

set return rate

rate:Return rate to be set

void

string GetDeviceName()

get device name

none

return device name

string GetDeviceData(string key)

Get Key value data

key:data key

return data value

英文说明

📎Bluetooth Protocol SDK Quick Guide.pdf

Last updated