Skip to content

Demonstration of how MAUI Android works with SerialPort

License

Notifications You must be signed in to change notification settings

c3n9/SerialPort-in-MAUI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serial Port in MAUI (Android)

Getting start

  1. Reference the library to your project. We will use the source code, since there is no such library on nuget.

  2. Copy the device_filter.xml from the example app to your Platforms/Android/Resources folder. Make sure that the Build Action is set to AndroidResource.

  3. Add the following attribute to the main activity to enable the USB Host.

[assembly: UsesFeature("android.hardware.usb.host")]
  1. Add the following IntentFilter to the main activity to receive USB device attached notifications.
[IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached })]
  1. Add the MetaData attribute to associate the device_filter with the USB attached event to only see the devices that we are looking for.
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
  1. Add a global interface to implement an action with our controller.
public interface IUsbService
{
  Task<IEnumerable<string>> GetAvailablePortsAsync();
  Task<bool> ConnectAsync(string portName);
  Task SendMessageAsync(string message);
}
  1. Add the UsbServiceAndroid class to the Android folder, which will implement the created interface.

  2. With the help of DependencyService, we will turn to platform-dependent code and you can work safely with our controller.

Arduino firmware Code

const int ledPin =  LED_BUILTIN;// the number of the LED pin

void setup() {
  pinMode(ledPin, OUTPUT); // Set the pin mode to output
  Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}

void loop() {
  if (Serial.available()) { // Check if data is available to read
    commandValue = Serial.read(); // Read the incoming data

    if (commandValue == '1') { // If the received data is '1'
      Serial.println("1"); // Send '1' to serial monitor
      digitalWrite(ledPin, HIGH); // Turn on the LED
    }
    else if (commandValue == '0') { // If the received data is '0'
      Serial.println("2"); // Send '2' to serial monitor
      digitalWrite(ledPin, LOW); // Turn off the LED
    }
  }
  delay(10); // Wait for 10 milliseconds before the next loop iteration
}

Working with bluetooth / Not tested set and get value

Everything has been tested using an Arduino MEGA 2560, ESP32 and a Google Pixel 7a smartphone.

About

Demonstration of how MAUI Android works with SerialPort

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published