C# desktop application to Scan for nearby Bluetooth devices

Bluetooth Low Energy (BLE) is a low-power wireless technology used for connecting devices with each other. It is a popular communication method, especially in the era of the Internet of Things. Several devices around the house have a built-in Bluetooth transceiver and most of them provide useful capabilities to automate jobs. For that reason, it is really interesting to create a desktop application using C# that connects to the devices around the house and manages them.

In this example, we are going to create a simple C# windows form application that scans and shows a list of nearby Bluetooth devices. 

Let’s start

As a first step let’s create a new project in visual studio and select C# windows form application from the list.

Choose a suitable name for your project.
Once the project is created, we will see a blank form screen where we will add buttons and labels to communicate with BleuIO graphically through the serial port.

We will have buttons that connect and disconnects from the dongle. We create a button called Scan. And a text area will display a list of Bluetooth devices.
The form will look like this

The .cs file associated to this will have the following code.
Source code is available at https://github.com/smart-sensor-devices-ab/c-sharp-scan-bluetooth-device.git

using System;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Scan_BLE_devices
{
    public partial class Form1 : Form
    {
        SerialPort mySerialPort = new SerialPort("COM18", 57600, Parity.None, 8, StopBits.One);
        public Form1()
        {
            InitializeComponent();
            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
            mySerialPort.Open();

        }

        //print response from the dongle
        private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string s = sp.ReadExisting();
            output_data.Invoke(new EventHandler(delegate { output_data.Text += s + "\r\n"; }));
            //lbl_output.Invoke(this.myDelegate, new Object[] { s });
        }





   

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btn_disconnect_Click_1(object sender, EventArgs e)
        {
            mySerialPort.Close();
            Environment.Exit(0);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            lbl_test.Text = "Connected";
        }

        private void submit_cmd_Click_1(object sender, EventArgs e)
        {
            output_data.Text = "";
            byte[] bytes = Encoding.UTF8.GetBytes("AT+CENTRAL");
            var inputByte = new byte[] { 13 };
            bytes = bytes.Concat(inputByte).ToArray();
            mySerialPort.Write(bytes, 0, bytes.Length);

            System.Threading.Thread.Sleep(1000);
            output_data.Text = "";
            byte[] bytes2 = Encoding.UTF8.GetBytes("AT+GAPSCAN=3");
            var inputByte2 = new byte[] { 13 };
            bytes2 = bytes2.Concat(inputByte2).ToArray();
            mySerialPort.Write(bytes2, 0, bytes2.Length);
            //System.Threading.Thread.Sleep(3000);
        }

        private void output_data_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

As you can notice I wrote COM18 to connect to serial port because BleuIO device on my computer is connected to COM18.
You can check your COM port from device manager.
Lets run the project and click on connect button.

Once we are connected to BlueIO dongle through serial port, we will be able to scan for nearby Bluetooth device. Clicking on Scan button will show a list of nearby Bluetooth device on the screen.

Output

Share this post on :

Get company identifier information from Bluetooth advertising packets using Python

Bluetooth Advertisements are crucial for any BLE device since they are utilized for all types of applications, whether that’s a device that allows connections or one that simply advertises its presence and includes data for others to discover.

The most important goal of advertising packets is to convey information to other BLE devices via the advertising packet type, and the advertising data types included in the packet.

Bluetooth beacons are the most prominent devices that take full advantage of Bluetooth advertising packets. This is due to the reason that most beacons stay in the advertising state throughout their lifetime (do not allow connections), so they rely completely on advertising for relaying the relevant information to the scanning devices.

I tried to create a simple python example script that scans for nearby Bluetooth devices and returns the manufacturer company information by reading the advertising packet.

Requirments 

Steps

  • Get the script from GitHub at https://github.com/smart-sensor-devices-ab/python_bluetooth_device_info.git
  • Connect the BleuIO to your computer. The script uses pyserial to connect to the Bluetooth USB dongle BleuIO.
  • Update the script and write the correct COM port, where the dongle is connected. (main.py line 6)
  • After connecting to the dongle, we put the dongle into the central role so that it can scan for nearby Bluetooth devices. 
  • Then we do a simple Gap scan using AT+GAPSCAN=3 command to scan for nearby Bluetooth devices for 3 seconds.
  • After that, we read the output from the serial port and print out the list of devices with MAC addresses.
  • User selects a device to get manufacturer company information
  • We read the advertising packet for this specific device. 
  • Pass the response to a function which separates the manufacturer id. 
  • Then we try to find a match from an object which I have downloaded recently from https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers/
  • The company name shows on the screen.

Here is the final script file. 

import serial
import time
from companydata import companyData
       

your_com_port = "COM18"  # Change this to the com port your dongle is connected to.
connecting_to_dongle = True

print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings

# function to get company name from advertised id
def getCompany(adv):
  # example advertised package would look like this 
  # 0201061BFF5B070504220118A3003127ED006901090100000000000001BD03
  # explains here 
  # https://twitter.com/josryke/status/763006284052463617/photo/1
  indentifierReversed=''
  # first part 02 is the length
  length = int(adv[0:2],16)  
  pl =int(adv[length*2+2:length*2+4 ], 16) 
  # this gives us 1B which is 27 in decimal. that is our length
  startsFrom = length*2+4
  # this gives us 8, from where it starts
  # now get the packet
  fd=adv[startsFrom:pl]
  # look for the position of flag FF
  flagPosition = fd.find("FF")
  if flagPosition!=-1:    
    identifier = fd[flagPosition+2:flagPosition+6]
    # get 5B07
    indentifierReversed = identifier[2]+identifier[3]+identifier[0]+identifier[1]
    # get 075B
    # now look for the company name on the list
    for attr in companyData:
        if attr['Hexadecimal']=='0x'+indentifierReversed:
            theName=attr['Company']
  else:
    indentifierReversed='-'
    theName='Unknown'
  
  return theName

while connecting_to_dongle:
    try:
        console = serial.Serial(
            port=your_com_port,
            baudrate=115200,
            parity="N",
            stopbits=1,
            bytesize=8,
            timeout=0,
        )
        if console.is_open.__bool__():
            connecting_to_dongle = False
    except:
        print("Dongle not connected. Please reconnect Dongle.")
        time.sleep(5)

print("Connected to Dongle.")

#put the dongle in dual role, so we can scan for nearby device
console.write(str.encode("AT+CENTRAL"))
console.write("\r".encode())
print("Putting dongle in Central role.")
time.sleep(0.1)
# Scan for nearby devices for 3 seconds
console.write(str.encode("AT+GAPSCAN=3"))
console.write("\r".encode())
time.sleep(0.1)
print("Looking for nearby Bluetooth devices ...")
dongle_output2 = console.read(console.in_waiting)
time.sleep(3)
filtered = []
# Filter out unncecssary outputs and keep only the list of devices (also remove index)
for dev in dongle_output2.decode().splitlines():
    if len(dev)>20:
        filtered.append(dev)

# Get unique device by device id and add distance to each raw        
seen = set()
out = []
for elem in filtered:
    prefix = elem.split(' ')[2]
    if prefix not in seen:
        seen.add(prefix)
        out.append(elem) 

# sort list by closest device
# out.sort(key=lambda x:int(x.split()[3]),reverse=True)
print("Scan Completed! "+ str(len(out)) +" devices found.")
# print(out)
for i in range(0, len(out)):
    print (out[i]) 

getInput = input("Select device from the list to get company identifier information (ex.1): ")
deviceToScan = out[int(getInput)-1].split(" ")[2]
# clear output
console.flushInput()
console.flushOutput()
time.sleep(0.1)
# Scan for advertised data of the selected device for 4 seconds
console.write(str.encode("AT+SCANTARGET="+deviceToScan+"=4"))
console.write("\r".encode())
time.sleep(0.1)
print("Getting company identifier information ...")
dongle_output3 = console.read(console.in_waiting)
time.sleep(5)
resp = dongle_output3.decode().splitlines()
# get the adv data only 
for d in resp:
    if('[ADV]' in d):
        companyName=getCompany(d.split(" ")[-1])
        break;
print(companyName)               
time.sleep(0.1)
console.close()

Output

After running the script, we see a total of 20 devices found nearby. The script prints out the manufacturer company information of the device [05] Device: [1]C1:BF:22:71:81:36

Share this post on :

Scan for nearby Bluetooth device using JAVA

This article is a guide for creating Java applications that can scan for nearby Bluetooth devices. This example project will be helpful to create BLE application easily. Source file is available.

Requirments

  1. BleuIO. (Bluetooth Low Energy USB dongle)
  2. NetBean

About the Project

The script has a COM port settings section. This section shows connected devices to the COM port. Using jSerialComm we get the list of COM ports and show it on a dropdown menu to select.

The connect and disconnect buttons manages the connection of the dongle.

Once we are connected to the BleuIO dongle, we will be able to write AT commands to the dongle using serial port write command.

By clicking on the SCAN button , it will put the dongle into CENTRAL mode, wait for one second and write another AT command (AT+GAPSCAN=3) to scan for nearby Bluetooth devices for three seconds.

The response will be available on the output panel.

The script can be updated as required. For example. If you want to scan for more than three seconds, just update the MainJFrame.java file 271 line.

List of AT commands are available at https://www.bleuio.com/getting_started/docs/commands/

Step 1: Clone the project

Step 2 : Run the project

Connect BleuIO dongle into the computer.

Run the project using NetBean play button.

Alternatively we can open the project using command line interface by going to the root folder of the project and type

java -jar "dist/JavaBleuIO.jar"

The output will look like this.

Lets select a COM port where the BleuIO dongle is connected and click connect. After successful connection, we should be able to click on scan button. The response will be available on output screen.

If we click on scan button again , it will look for nearby devices for three seconds and show the output.

Share this post on :

Measuring distance with Bluetooth in indoor environment using Python

Bluetooth ranging technology is very popular. There are many localization systems that exist based on beacons. Beacon technology usually estimates the distance between devices using the received signal strength (RSSI). 

Bluetooth can be an excellent way to narrow down a search area at close distances when tracking something. This feature can be used in several fields. such as Secure Locks for Buildings and Automotive, Asset localization & tracking, Indoor navigation etc

GPS tracking isn’t excellent at giving accurate measurements of the close distance, especially in the indoor environment. On the other hand, Bluetooth is excellent in short ranges because the waves can go through walls. This might fill the gap that GPS tracking has when tracking devices in indoor spaces.

However, most calculations of the distance between two Bluetooth devices are estimates. It’s hard to determine the exact distance between two Bluetooth devices because many factors affect the calculations. Despite the challenges, there are methods to determine the distance between two Bluetooth devices with an accuracy of at least 80%.

The ranging method is simple to implement, and it has the formula to calculate the distance between two Bluetooth devices. As the name suggests, both devices need to be within Bluetooth range to estimate the distance. 

This article will share a simple python script to determine nearby Bluetooth devices and their distance in meters.

This script scans for nearby Bluetooth devices and gets an approximation of the distance by using the well-known RSSI to distance formula.

Read more about how to calculate the distance

Requirments 

Instructions

  • Get the script from GitHub at https://github.com/smart-sensor-devices-ab/python_bluetooth_device_distance_meter.git
  • Connect the BleuIO to your computer. The script uses pyserial to connect to the Bluetooth USB dongle BleuIO.
  • Update the script and write the correct COM port, where the dongle is connected. 
  • After connecting to the dongle, we put the dongle into the central role so that it can scan for nearby Bluetooth devices. 
  • Then we do a simple Gap scan using AT+GAPSCAN=3 command to scan for nearby Bluetooth devices for 3 seconds.
  • After that, we read the output from the serial port and use our RSSI to distance formula to get the distance in meters. 
  • Finally, we sort the result by distance before printing it out on screen.

Here is the final script file. 

import serial
import time

your_com_port = "COM18"  # Change this to the com port your dongle is connected to.
connecting_to_dongle = True

print("Connecting to dongle...")
# Trying to connect to dongle until connected. Make sure the port and baudrate is the same as your dongle.
# You can check in the device manager to see what port then right-click and choose properties then the Port Settings
# tab to see the other settings

while connecting_to_dongle:
    try:
        console = serial.Serial(
            port=your_com_port,
            baudrate=57600,
            parity="N",
            stopbits=1,
            bytesize=8,
            timeout=0,
        )
        if console.is_open.__bool__():
            connecting_to_dongle = False
    except:
        print("Dongle not connected. Please reconnect Dongle.")
        time.sleep(5)

print("Connected to Dongle.")

# function to convert rssi to distance in meter
def rssiToDistance(rssi):    
  n=2
  mp=-69
  return round(10 ** ((mp - (int(rssi)))/(10 * n)),2)    

#put the dongle in dual role, so we can scan for nearby device
console.write(str.encode("AT+CENTRAL"))
console.write("\r".encode())
print("Putting dongle in Central role.")
time.sleep(0.1)
# Scan for nearby devices for 3 seconds
console.write(str.encode("AT+GAPSCAN=3"))
console.write("\r".encode())
time.sleep(0.1)
print("Looking for nearby Bluetooth devices ...")
dongle_output2 = console.read(console.in_waiting)
time.sleep(3)
print("Scan Complete!")
filtered = []
# Filter out unncecssary outputs and keep only the list of devices (also remove index)
for dev in dongle_output2.decode().splitlines():
    if len(dev)>20:
        filtered.append(dev.split(maxsplit=1)[1])
# Get unique device by device id and add distance to each raw        
seen = set()
out = []
for elem in filtered:
    prefix = elem.split(' ')[1]
    if prefix not in seen:
        seen.add(prefix)
        out.append(elem + " Distance: "+str(rssiToDistance(elem.split()[3]))+" meter") 

# sort list by closest device
out.sort(key=lambda x:int(x.split()[3]),reverse=True)

# print(out)
for i in range(0, len(out)):
    print (out[i]) 

time.sleep(0.1)
console.close()

Output

After running the script, we see a total 20 devices found nearby. The list shows their distance in meter from the central device.

Share this post on :

Get air quality Bluetooth LE sensor data from multiple devices

There are so many Bluetooth-enabled smart devices that it can be confusing how the technology connects the devices. Often we want to connect to multiple peripheral devices simultaneously to get advertised packets or do other operations. In this article, we will see how we can get advertised packets from two different Air quality monitoring sensor devices. 

For this project, we will use Chrome Web serial API to connect to a Bluetooth USB dongle. Using the serial port read/write operation we will scan for specific device advertised data and filter out what we need. After that, we decode the advertised packet to meaningful air quality data using the device documentation.

Requirments

  1. BleuIO Bluetooth USB dongle x1
  2. HibouAir Air quality monitor x2

Steps

At first, we will get the bleuIO javascript library from NPM. This library will help us easily connect to the serial port,wtire AT commands and read responses in real-time.

Type npm i bleuio on the command prompt of your project root folder.

After that, we create two files. index.html and script.js

Index.html will be responsible for the output and layouts of the project. 

Script.js will have the programming and logic to connect to the dongle and read/write data.

There will be two buttons connect and get data.

The connect button will connect to the bleuIO dongle using the serial port. 

The get data button will do several tasks.

At first, we put the dongle in a dual role (central mode) so that it can scan for peripheral devices. Then we will look for advertised data with their sensor ID one after another. 

Using the documentation from the air quality device, we decode the advertised data.

Finally, we print it out on the screen.

Here is the index.html file code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Bootstrap demo</title>
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor"
      crossorigin="anonymous"
    />
  </head>
  <body>
    <div class="container mt-5">
      <h1>Get air quality Bluetooth LE sensor data from multiple devices</h1>
      <br /><br /><br /><button
        id="connect"
        class="btn btn-success btn-lg me-5"
      >
        Connect
      </button>
      <button id="getData" class="btn btn-warning btn-lg ms-5">Get Data</button>
      <br />
      <br />
      <div id="loading" style="display: none">Fetching Data ...</div>
      <br />
      <div id="airData"></div>
    </div>

    <script src="script.js"></script>
  </body>
</html>

Here is script.js file code

import * as my_dongle from 'bleuio'
document.getElementById('connect').addEventListener('click', function(){
  my_dongle.at_connect()
  document.getElementById("connect").classList.add('disabled');
})
let dev1BoardID='45840D'
let dev2BoardID='60FDED'
document.getElementById('getData').addEventListener('click', function(){
    
    //show loading
    document.getElementById("loading").style.display = "block";
    //make the dongle in dual role , so it can scan for peripheral devices advertised data
    my_dongle.at_dual().then(()=>{
        //scan for a devices advertised data, a PM sensor
        my_dongle.at_findscandata(dev1BoardID,8).then((x)=>{
            //it returns an array of advertised data
            //from the array, we take the last one
            //it looks like this "[F9:0D:35:E7:72:65] Device Data [ADV]: 0201061BFF5B07050345840DB1031527FB002A010402040004000400000001"
            //then we split it by space
            //so we can get the advertised data only (the last part)
            return x[x.length-1].split(" ").pop()
        }).then((adv1)=>{ 
            //now lets decode the advertised data for this device using the device documentaion
            let pmEnvData=advDataDecode(adv1)
            //we do the same process to get advertised data of another device
            //after waiting 1 seconds
            setTimeout(()=>{
                my_dongle.at_findscandata(dev2BoardID,8).then((y)=>{
                    let adv2= y[y.length-1].split(" ").pop()
                    //again we decode the advertised data for this device using the device documentaion
                    let co2EnvData=advDataDecode(adv2)
                    //now merge pm data to this array
                    co2EnvData.pm1=pmEnvData.pm1
                    co2EnvData.pm25=pmEnvData.pm25
                    co2EnvData.pm10=pmEnvData.pm10
                    document.getElementById('airData').innerHTML=`
                    Air Quality data from PM and CO2 sensor devices<br/><br/>
                    CO2 : ${co2EnvData.co2} ppm<br/>
                    PM 1.0 : ${co2EnvData.pm1} µg/m³<br/>
                    PM 2.5 : ${co2EnvData.pm25} µg/m³<br/>
                    PM 10 : ${co2EnvData.pm10} µg/m³<br/>
                    Temperature : ${co2EnvData.temp} °C<br/>
                    Humidity : ${co2EnvData.hum} %rH<br/>
                    Pressure : ${co2EnvData.pressure} mbar<br/>
                    Light : ${co2EnvData.light} Lux<br/>

                    `
                    //hide loading
                    document.getElementById("loading").style.display = "none"; 
                })
            },1000)
            
        })
    })
    

  })

  const advDataDecode =((data)=>{
    let pos = data.indexOf("5B0705")
    let dt = new Date();
    let currentTs = dt.getFullYear() 
    + '/' 
    + (dt.getMonth() + 1).toString().padStart(2, "0") 
    + '/' 
    + dt.getDate().toString().padStart(2, "0")
    +' '
    +
    dt.getHours().toString().padStart(2, "0")
    +
    ':'
    +
    dt.getMinutes().toString().padStart(2, "0")
    +
    ':'
    +dt.getSeconds().toString().padStart(2, "0")
    let tempHex=parseInt('0x'+data.substr(pos+22,4).match(/../g).reverse().join(''))
    if(tempHex>1000)
        tempHex = (tempHex - (65535 + 1) )/10
    else
        tempHex = tempHex/10
    return {
      "boardID":data.substr(pos+8,6),
      "type":data.substr(pos+6,2),
      "light":parseInt('0x'+data.substr(pos+14,4).match(/../g).reverse().join('')),
      "pressure":parseInt('0x'+data.substr(pos+18,4).match(/../g).reverse().join(''))/10,
      "temp":tempHex,
      "hum":parseInt('0x'+data.substr(pos+26,4).match(/../g).reverse().join(''))/10,
      "pm1":parseInt('0x'+data.substr(pos+34,4).match(/../g).reverse().join(''))/10,
      "pm25":parseInt('0x'+data.substr(pos+38,4).match(/../g).reverse().join(''))/10,
      "pm10":parseInt('0x'+data.substr(pos+42,4).match(/../g).reverse().join(''))/10,
      "co2":parseInt('0x'+data.substr(pos+46,4)),
      "ts":currentTs
    }
})

Source code also available on github

https://github.com/shuhad/bluetooth_mutiple_device_read

Run the script

You can either clone the script from github or follow the instructions and make a new project.

For this project to run we may need a web build tool. I prefer using parcel.

https://parceljs.org/

install parcel if you don’t have

npm install --save-dev parcel

Now run the script using

parcel index.html

Output

The script can be modified to read more than two devices as required.

Share this post on :

Build a Bluetooth Low energy application with the Chrome. Web Bluetooth vs Web Serial (Bluetooth Dongle)

In general, most of us think of Bluetooth as a simple device-to-device connection used to do things like play music or other audio (speakers/headsets), offer quick access (smartwatches), or perform other tasks. But there is a new Bluetooth standard and it allows the browser to control Bluetooth devices nearby.

These features can be accessed using Web Bluetooth or Web serial API. What is promising is that both features are already available in Chromium-based browsers (Google Chrome, Edge, Opera). It makes it easy for web developers to interact with users’ peripherals in their homes – if the user would of course allow them.

Web applications are adapting to face the challenges of a competitive native environment. There is a concern that the browser may connect to nearby Bluetooth devices – wondering what kind of information the site can access is a question that needs to be asked. The good news is that, as with all other APIs built into browsers like Chrome, every website has to request access. The browser shows a pop-up asking for permission to access the site in question, just as it does for messaging, site access or the webcam. If the user does not respond, the request will be denied automatically. Users can also change this permit decision at any time. 

What is Web Bluetooth?

Web Bluetooth allows websites to communicate with nearby Bluetooth devices. That means no need to install any dedicated native app to connect to a beacon, heart rate monitor, smart light bulb, or any other Bluetooth Low Energy device. Using Web Bluetooth, developers can easily connect to nearby BLE devices and read/write Bluetooth characteristics. However, web Bluetooth has certain limitations when it comes to developing a complex Bluetooth application.

What is Web Serial?

A serial port is a bidirectional communication interface that allows sending and receiving data byte by byte. The Web Serial API provides a way for websites to read from and write to serial devices with JavaScript. These devices may be connected via a serial port, or by USB or Bluetooth devices that emulate a serial port.

Developing a complex Bluetooth Low Energy application for a web browser is relatively easy with Web Serial and a Bluetooth USB dongle BleuIO. The AT commands available on Bluetooth Low Energy USB dongle called Bleuio helps to do BLE operations easily. List of AT commands can be found here, https://www.bleuio.com/getting_started/docs/commands/

Another advantage of using Web Serial; there are good web serial libraries available along with BleuIO’s own JS library , which makes it easier to develop Bluetooth Low Energy applications

We think both the API has the potential to revolutionize how companies will create the BLE devices of the future, allowing users to manage and configure these devices by simply using a browser. We also hope this becomes officially supported by more browsers.

Share this post on :