본문 바로가기

카테고리 없음

M3 모바일 MAUI 샘플 SL20 산업용 안드로이드 스캐너 Intent 연동 C#

반응형

 

 

1. 설정

 

 

 

 

 

 

 

2. BroadcastReceiver 상속 구현

[BroadcastReceiver(Enabled = true, Exported = true)]
public class ScannerBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        string action = intent.Action;
 
        if (action == "com.android.server.scannerservice.broadcast")
        {
            //[0] "m3scanner_code_type"   string
            //[1] "m3scannerdata" string
            //[2] "m3scanner_dec_count"   string
            //[3] "m3scanner_module_type" string
            //[4] "m3scannerdata_length"  string
            //[5] "m3scannerdata_raw" string 


            string scanData = intent.GetStringExtra("m3scannerdata");
            string barcodeType = intent.GetStringExtra("m3scanner_code_type");
            string moduleType = intent.GetStringExtra("m3scanner_module_type");
             
         
            StrongReferenceMessenger.Default.Send(new ScannerEventMessage(scanData, barcodeType));
        } 
    }
}

public class ScannerEventMessage
{
    public string Data { get; }
    public string BarcodeType { get; }

    public ScannerEventMessage(string data, string barcodeType)
    {
        Data = data;
        BarcodeType = barcodeType;
    }
}

 

 

3. MainActivity Intent 등록

 

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
    private PDA001_Login PDA001_Login { get; set; } 
    ScannerBroadcastReceiver scannerBroadcastReceiver;
 

    public static event VolumeButtonPressedEventHandler VolumeButtonPressed;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
 

        var currnet = (AppShell)Microsoft.Maui.Controls.Application.Current.MainPage;

        PDA001_Login = currnet.CurrentPage as PDA001_Login;


         
#if DEBUG
        var deviceName = DeviceInfo.Name;
        if (deviceName.Contains("M3SL20"))
        {

            scannerBroadcastReceiver = new ScannerBroadcastReceiver();
            RegisterReceiver(scannerBroadcastReceiver, new IntentFilter("com.android.server.scannerservice.broadcast"));
          
        }
#elif RELEASE
        scannerBroadcastReceiver = new ScannerBroadcastReceiver();
        RegisterReceiver(scannerBroadcastReceiver, new IntentFilter("com.android.server.scannerservice.broadcast"));
#endif 
 
    }
 

    protected override void OnDestroy()
    {
        base.OnDestroy();
 
        if (scannerBroadcastReceiver != null)
        {
            UnregisterReceiver(scannerBroadcastReceiver);
            scannerBroadcastReceiver.Dispose();
            scannerBroadcastReceiver = null;
        }
    }

 
}

 

 

 

4. ContentPage 구독및 해제 구현

 

    protected override async void OnAppearing()
    {
        base.OnAppearing();
 
 
        StrongReferenceMessenger.Default.Unregister<ScannerEventMessage>(this);

        StrongReferenceMessenger.Default.Register<ScannerEventMessage>(this, (recipient, message) =>
        { 
            //DisplayAlert("Scan Event", message.Data, "OK");   
        }); 


    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
 
        StrongReferenceMessenger.Default.Unregister<UsbDeviceAttachedMessage>(this);
        StrongReferenceMessenger.Default.Unregister<ScannerEventMessage>(this); 
       
    }