.NET MAUI and Android Intents

Hey guys, I hate to be a bother, but I have a bit of a situation, is there anyone here that works with MAUI that could possibly help me? In short... I need to integrate with a phone that has a built in barcode scanner (UROVO DT50), and I managed to find the API documentation, but I for the life of me cannot grasp the concept of intents and how to use them in MAUI.
1 Reply
apnxmammoth
apnxmammothOP3d ago
Alright, since no one was able to help me, I did manage to find the solution myself eventually. So I am just posting it here so that if someone does run into the same issue, hopefully this post will help: Problem: I kept trying to send intents and registering receivers, but my OnReceive method would never trigger in my custom broadcast receiver. Solution: Instead of declaring my intents and my receiver class in the Manifest.xml file, I created a Service.cs and Service class. In there I would have my OnCreate() method, where then I would initialize my component (in this case it was a scanner), and then I would register my receiver.
public override void OnCreate() {
base.OnCreate();
InitializeScanner();
RegisterReceiver();
}

private void RegisterReceiver() {
if (mScannerReceiver == null) {
mScannerReceiver = new ScannerReceiver();
var filter = new IntentFilter(SCAN_ACTION);
RegisterReceiver(mScannerReceiver, filter);
}
}

private void InitializeScanner() {
if (mScanManager == null) {
mScanManager = new ScanManager();
mScanManager.OpenScanner();
}
}
public override void OnCreate() {
base.OnCreate();
InitializeScanner();
RegisterReceiver();
}

private void RegisterReceiver() {
if (mScannerReceiver == null) {
mScannerReceiver = new ScannerReceiver();
var filter = new IntentFilter(SCAN_ACTION);
RegisterReceiver(mScannerReceiver, filter);
}
}

private void InitializeScanner() {
if (mScanManager == null) {
mScanManager = new ScanManager();
mScanManager.OpenScanner();
}
}
Then, in your MainActivity.cs, in your OnCreate(). you would register your service and start your service:
protected override void OnCreate(Bundle? savedInstanceState) {
base.OnCreate(savedInstanceState);

mScannerService = ServiceHelper.GetService<IBarcodeScannerService>();

if (mScannerService == null) {
throw new InvalidOperationException("BarcodeScannerService not found.");
}

var intent = new Intent(this, typeof(BarcodeScannerService));
StartService(intent);
}
protected override void OnCreate(Bundle? savedInstanceState) {
base.OnCreate(savedInstanceState);

mScannerService = ServiceHelper.GetService<IBarcodeScannerService>();

if (mScannerService == null) {
throw new InvalidOperationException("BarcodeScannerService not found.");
}

var intent = new Intent(this, typeof(BarcodeScannerService));
StartService(intent);
}
This finally triggered my OnReceive() method inside my custom broadcast receiver. I know this is probably super easy work for someone that has been working with MAUI for a while, but this was completely new for me.
Want results from more Discord servers?
Add your server