반응형
MAUI 프로젝트중 산업용스캐너의 스캐너입력값을 키보드 웨지 wedge 방식 엔터값 기반으로 된 프로그램을 Intent 브로드캐스트 방식으로 변경하는코드
1. Emkit - Intent 설정
ScanSettings => Result type => Intent Broadcast
2. adb 이벤트 확인 스캔이벤트 확인
adb -s 기기ID logcat ActivityManager | findstr "EVENT"
3. BroadcastReceiver 상속 구현
[BroadcastReceiver(Enabled = true, Exported = true)]
public class ScannerBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;
// 스캔 데이터 처리
if (action == "device.scanner.EVENT")
{
//byte[] decodeValue = intent.GetByteArrayExtra("EXTRA_EVENT_DECODE_VALUE");
//string decodeStringValue = intent.GetStringExtra("EXTRA_EVENT_DECODE_STRING_VALUE");
//int decodeLength = intent.GetIntExtra("EXTRA_EVENT_DECODE_LENGTH", -1);
//string decodeLetter = intent.GetStringExtra("EXTRA_EVENT_DECODE_LETTER");
//string decodeModifier = intent.GetStringExtra("EXTRA_EVENT_DECODE_MODIFIER");
//long decodeTime = intent.GetLongExtra("EXTRA_EVENT_DECODE_TIME", -1);
//string symbolName = intent.GetStringExtra("EXTRA_EVENT_SYMBOL_NAME");
//int symbolId = intent.GetIntExtra("EXTRA_EVENT_SYMBOL_ID", -1);
//string symbolType = intent.GetStringExtra("EXTRA_EVENT_SYMBOL_TYPE");
//string source = intent.GetStringExtra("EXTRA_EVENT_SOURCE");
bool decodeResult = intent.GetBooleanExtra("EXTRA_EVENT_DECODE_RESULT", false);
if (decodeResult)
{
string scanData = Encoding.UTF8.GetString(intent.GetByteArrayExtra("EXTRA_EVENT_DECODE_VALUE"));
string barcodeType = intent.GetStringExtra("EXTRA_EVENT_SYMBOL_NAME");
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;
}
}
4. MainActivity Intent 등록
ScannerBroadcastReceiver scannerBroadcastReceiver;
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;
scannerBroadcastReceiver = new ScannerBroadcastReceiver();
RegisterReceiver(scannerBroadcastReceiver, new IntentFilter("device.scanner.EVENT"));
}
rotected override void OnDestroy()
{
base.OnDestroy();
if (scannerBroadcastReceiver != null)
{
UnregisterReceiver(scannerBroadcastReceiver);
scannerBroadcastReceiver.Dispose();
scannerBroadcastReceiver = null;
}
}
5. 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);
}