Connect a .NET Maui app to Arduino Bluetooth

In this exercise, you will learn how to create a .NET Maui app and connect it to an Arduino Bluetooth device. The smart home technology industry is booming and there is an increasing demand for innovative products. Many of these products use BLE (Bluetooth low energy) or Bluetooth classic to connect smart home devices to mobile applications. Connecting a microcontroller board to a mobile app will allow you to create exciting new gadgets that can be controlled from a smartphone within close range.

While creating this device I realized I had picked up a Bluetooth classic transceiver, and not a Bluetooth LE transceiver. This article will be targeted to Bluetooth classic however the concept and schematics will be relatively the same.

What is Bluetooth

Bluetooth is a fascinating technology that uses electromagnetic waves to transmit nearly 1,000,000 ones and zeros every second. These electromagnetic waves can pass through obstructions like walls, even concrete within a distance of around 33 feet. Bluetooth uses wavelengths that range from 121 to 124 millimeters in length. A Bluetooth transceiver intercepts these electromagnetic waves and passes ones and zeros to the microcontroller board.

Millimeter wavelengths can be translated to Gigahertz by dividing 299.792458 by the wavelength

This wavelength range or bandwidth can be divided into a set of 79 different channels. For each channel, the bottom of the range will be a binary zero and the top a binary one.

For example:

  • Channel 38 – 2.44313GHz (binary zero) – 2.44387GHz (binary one)
  • Channel 54 – 2.42713GHz (binary zero) – 2.45587GHz (binary one)

As your smartphone and Bluetooth device communicate they jump between these channels passing 1 packet of data per channel at a time. This is called the Frequency-Hoping Spread Spectrum. A Bluetooth connection can hop channels 1600 times in a second. When a channel becomes too crowded your smartphone will avoid using that channel until the noise clears. Only your smartphone and Bluetooth devices know the sequence of channels they will use to communicate across keeping the connection private.

Let’s say your at the gym and there are many smartphones transmitting these electromagnetic Bluetooth waves and many wireless Bluetooth devices receiving data. How can you keep your connection secure between your smartphone and Bluetooth device?

Data packets are made from the ones and zeros that are passed between these devices. The first 72 bits are the access codes that synchronize your smartphone and Bluetooth device to ensure that communication is exclusive. These access codes are used to verify communication is happening between the correct devices. The next 54 bits are metadata about the data packet, and the last 500 bits are the actual payload. If you’re curious as to how binary payload can be translated into audio then take a look at Audio Codex.

Bluetooth classic versus Bluetooth Low Energy

The differences between Bluetooth classic and BLE are how they distribute data with BLE designed to save energy. The two technologies are incompatible with each other, and you must choose one or the other. You don’t necessarily always want BLE sometimes Bluetooth classic is better, it all depends on how you’re applying it. Use Bluetooth classic for wireless headsets, speakers, keyboards, or printers and use BLE for mainly wearable devices like Fitbit, sensors, blood pressure monitoring, etc.

Bluetooth Low Energy is able to offer these energy savings by operating in sleep mode until a user wakes the connection by connecting to exchanging data. This is different from classic Bluetooth which will perpetually broadcast data. BLE is also faster as the data throughput is limited. This is another reason why it’s important to know what type of device you’re building before selecting a Bluetooth transceiver. A classic Bluetooth transceiver would be a better choice for an application with high data throughput.

Gather your supplies

To get started you will want to get your microcontroller parts together so you can create your device. I found that it easiest to just pick up the Arduino starter kit and the Bluetooth transceiver. There is no soldering required for this device it’s purely plug and play.

Connect the Arduino

Head over to Arduinos website and download the latest Arduino IDE for your environment. The Arduino I chose to use for this tutorial is a clone so it does not include the drivers required to recognize the device. You will want to install the VCP drivers then restart your machine in order to connect the microcontroller. Go to Tools > Boards and select Arduino Uno if it’s not already selected. Next, go to Tools / Ports and select the port with Arduino in the name. If you do not see any options for Arduino then your computer cannot detect the device and you may want to troubleshoot installing the VCP drivers.

With everything in place you can hit the upload arrow in the IDE to upload a blank C++ file to the Arduino. If you see any errors in the output window then there is still something wrong with your connection. If there is no output then everything is a go and you just uploaded your program to the Arduino.

Now for the fun part!

Let’s follow this schematic to set up our new Bluetooth device. We can start by wiring up our Bluetooth transceiver to the breadboard. The HC-05 has 6 pins however we only need to use the middle four.

  • VCC – Powers the module by 5v
  • Ground – Connects to system ground
  • TX – Transmits Serial Data
  • RX – Receive Serial Data

Connect the transceiver to the breadboards as illustrated below. You don’t need to add the extra resisters technically so you can draw a line from each circuit to the specified slot on the Arduino board.

  • Fasten a jumper cable from VCC pin to the 5v slot on the board.
  • Do the same to join the ground pin to the GRD slot.
  • Connect the TX pin to the TX-1 slot
  • Affix the RX pin the RX-0 slot

Nice, everything looks good now but I think we’re missing something. Oh yeah, the LED bulb to indicate when a Bluetooth connection has been made. The long end of the LED is the negative and the short the positive. Connect one more jumper cable from the negative end of the bulb over to slot number 8 on the board.

Arduino program

Every Arduino program will start with two methods, setup and loop. The setup is like a constructor that can be used to configure bread board pins and begin listening on the serial port. The loop method executes code in a continuous loop. When a Bluetooth connection is made to the Arduino board it will send 5v to pin 8 which will light up the LED we attached.

  • Serial.begin – Begins intercepting serial communication (Bluetooth) at a baud rate of 9600 bits per second.
  • pinMode – Configures a pin on a breadboard as an input or an output.
  • digitalWrite – Sends specific voltage to a pin
  • delay – delay the program (n) milliseconds
  • Serial.available – The number of connections available.

Detach TX and RX

It’s important to note that you will have to take out TX (1) and RX (0) pins before pushing to your code to the Arduino, otherwise you will have problems. Once your code is pushed you can reconnect these 2 slots.

void setup() {
pinMode(8, OUTPUT);

void loop() {
    digitalWrite(8, HIGH);
  } else {
  digitalWrite(8, LOW);

Create the mobile app

.NET Maui still in preview

The examples in this tutorial may be subject to change.

Our new Maui mobile app will only work for Android as my Arduino device is not MFI (Made-For-iPhone). If you are considering creating a production home automation device that uses Bluetooth you will want to enroll your device into their program in order to connect iOS.

To get started you will need to add the proper Bluetooth permissions to your AndroidManifest.xml. This file is now located in the Platforms > Android folder of a .NET Maui application.

<uses-feature android:name="android.hardware.bluetooth" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Next we add the interface that allows us to interact with the native code. We can do this by adding the interface to a DependancyService. The DependancyServices acts as a service locator that allows Xamarin.Forms to interfact with Xamarin.Native.

public interface IBluetoothConnector
    /// <summary>
    /// Gets a list of all Bluetooth devices connected to the phone
    /// </summary>
    /// <returns></returns>
    List<string> GetConnectedDevices();

    /// <summary>
    /// Connects app to Bluetooth device and writes to device
    /// </summary>
    /// <param name="deviceName"></param>
    void Connect(string deviceName);

In the Platforms > Android folder we can add our implementation for the new IBluetoothConnector.

  • GetConnectedDevices: Gets a list of all the Bluetooth devices that are currently connected to your smartphone. You will have to connect to your Arduino manually to allow the transceiver to populate this list.
  • BluetoothAdapter: Indicates if the device has enabled Bluetooth and if so returns all BondedDevices
  • BondedDevices: List of all connected Bluetooth devices
  • CreateRfcommSocketToServiceRecord: Performs an SSD lookup on a standardized Bluetooth UDID to return the socket connection between devices.
  • OutputStream: Writes to Bluetooth device
  • InputStream: Reads from Bluetooth device
[assembly: Dependency(typeof(BluetoothConnector))]
namespace MauiArduinoBluetoothClassicAndroid.Platforms.Android.Bluetooth
    public class BluetoothConnector : IBluetoothConnector
        /// <inheritdoc />
        public List<string> GetConnectedDevices()
            _adapter = BluetoothAdapter.DefaultAdapter;
            if (_adapter == null)
                throw new Exception("No Bluetooth adapter found.");

            if (_adapter.IsEnabled)
                if (_adapter.BondedDevices.Count > 0)
                    return _adapter.BondedDevices.Select(d => d.Name).ToList();
                Console.Write("Bluetooth is not enabled on device");
            return new List<string>();

        /// <inheritdoc />
        public void Connect(string deviceName)
            var device = _adapter.BondedDevices.FirstOrDefault(d => d.Name == deviceName);
            var _socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString(SspUdid));

            var buffer = new byte[] { 1 };

            // Write data to the device to trigger LED
            _socket.OutputStream.WriteAsync(buffer, 0, buffer.Length);

        /// <summary>
        /// The standard UDID for SSP
        /// </summary>
        private const string SspUdid = "00001101-0000-1000-8000-00805f9b34fb";
        private BluetoothAdapter _adapter;

With all the Maui.Native code in place all that’s left to do is connect to it using our DependancyService. Getting the list of connected devices is not exactly neccessary as we always only connect to HC-05 however I left it here as for a very basic example.

const string ArduinoBluetoothTransceiverName = "HC-05";

var connector = DependencyService.Get<IBluetoothConnector>();

//Gets a list of all connected Bluetooth devices
var ConnectedDevices = connector.GetConnectedDevices();

//Connects to the Arduino
var arduino = ConnectedDevices.FirstOrDefault(d => d == ArduinoBluetoothTransceiverName);

Take aways

In hindsight, I probably would’ve gone with a Bluetooth LE transceiver. There seems to be more documentation and support for creating these types of devices. I also would’ve liked to create an iOS implementation however signing up for Apple’s MFI program is a bit out of scope for this prototype.

Great work!

You’ve created a microcontroller device with Bluetooth that can connect to a cross-platform .NET Maui application. This prototype has many uses as you can now begin branching off and read/write more data to the device. There are a lot of very interesting Arduino projects you can learn from to extend this device into something bigger. Maybe you can create something cool like a face recognition door lock, a 3D projector, or a cool robot. Whatever you build I know it will be great, thanks for following along.

Github example

2 thoughts on “Connect a .NET Maui app to Arduino Bluetooth”

  1. Hi, thanks for the guide. I was just investigating this to make an application with arduino and bluetooth. I have tried to follow your tutorial but using Maui Blazor, I think it is also possible, but I can’t get it to work, could you tell me what I should adjust? thank you very much, greetings!

    • Hi Dani, if you clone the GitHub repository does that work for you? I’m unsure what your implementation looks like so it’s hard to say. You can DM me on LinkedIn

Comments are closed.