Once the BLE connection is established, the Digi IoT Library for .NET MAUI allows you to exchange data with the device using the appropriate send and receive mechanisms.

Send data

All the device classes of the Digi IoT Library for .NET MAUI provide the method SendData(byte[]) to send data to the physical Digi device.

Method Description

SendData(byte[])

Sends the given data to the device. Parameter must be a byte array containing the data to send.

The SendData(byte[]) method is asynchronous, which means that your application does not block during the transmit process. The SendData(byte[]) method may fail for the following reasons:

  • The device is not connected, throwing an InterfaceNotOpenException.

  • Other errors caught as DigiIoTException.

// Instantiate a Digi device object.
DigiBLEDevice myDigiBLEDevice = new DigiBLEDevice("00:00:00:00:00:00", "password");

// Connect the device.
myDigiBLEDevice.Connect();

String data = "Hello Digi device!";

// Send the data to the device.
myDigiBLEDevice.SendData(Encoding.UTF8.GetBytes(data));

Receive data

You can be notified when new data from the Digi device has been received if you are subscribed to the DataReceived event of the device classes.

// Instantiate a Digi device object.
DigiBLEDevice myDigiBLEDevice = new DigiBLEDevice("00:00:00:00:00:00", "password");

// Connect the device.
myDigiBLEDevice.Connect();

// Subscribe to data received event.
EventHandler<DataReceivedEventArgs> handler = (sender, e) => Console.WriteLine(">> Received data: " + Encoding.ASCII.GetString(e.Data));
myDigiBLEDevice.DataReceived += handler;

Whenever data is received, the handler callback is executed and provides as parameter the BLEDataReceivedEventArgs object (e), which contains the received data.