-
Reference the library to your project. We will use the source code, since there is no such library on nuget.
-
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.
-
Add the following attribute to the main activity to enable the USB Host.
[assembly: UsesFeature("android.hardware.usb.host")]
- Add the following IntentFilter to the main activity to receive USB device attached notifications.
[IntentFilter(new[] { UsbManager.ActionUsbDeviceAttached })]
- 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")]
- 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);
}
-
Add the UsbServiceAndroid class to the Android folder, which will implement the created interface.
-
With the help of DependencyService, we will turn to platform-dependent code and you can work safely with our controller.
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
}