-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathMainActivity.cs
More file actions
194 lines (164 loc) · 6.68 KB
/
MainActivity.cs
File metadata and controls
194 lines (164 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Android.Content;
using Android.Hardware.Usb;
using Android.Util;
using Android.Views;
using AndroidX.RecyclerView.Widget;
[assembly: UsesFeature("android.hardware.usb.host")]
namespace UsbSerialForAndroidDemo
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
[IntentFilter([UsbManager.ActionUsbDeviceAttached])]
[MetaData(UsbManager.ActionUsbDeviceAttached, Resource = "@xml/device_filter")]
public class MainActivity : Activity
{
static readonly string TAG = typeof(MainActivity).Name;
const string ACTION_USB_PERMISSION = "com.anotherlab.usbserialforandroid.demo.USB_PERMISSION";
UsbManager? usbManager;
RecyclerView? recyclerView;
TextView? statusText;
ProgressBar? progressBar;
UsbDeviceAdapter? adapter;
BroadcastReceiver? detachedReceiver;
readonly List<UsbSerialPort> usbPorts = [];
bool receiverInitalized = false;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
var rootView = FindViewById<Android.Views.ViewGroup>(Android.Resource.Id.Content);
if (rootView != null)
{
var statusBarHeight = GetStatusBarHeight();
rootView.SetPadding(0, statusBarHeight, 0, 0);
}
usbManager = GetSystemService(Context.UsbService) as UsbManager;
recyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
statusText = FindViewById<TextView>(Resource.Id.statusText);
progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
// Setup RecyclerView
recyclerView!.SetLayoutManager(new LinearLayoutManager(this));
adapter = new UsbDeviceAdapter(usbPorts, OnItemClick, this);
recyclerView.SetAdapter(adapter);
}
int GetStatusBarHeight()
{
int result = 0;
int resourceId = Resources?.GetIdentifier("status_bar_height", "dimen", "android") ?? 0;
if (resourceId > 0)
{
result = Resources?.GetDimensionPixelSize(resourceId) ?? 0;
}
return result;
}
protected override async void OnResume()
{
base.OnResume();
await PopulateDeviceListAsync();
// Register the broadcast receiver
detachedReceiver = new UsbDeviceDetachedReceiver(this);
RegisterReceiver(detachedReceiver, new IntentFilter(UsbManager.ActionUsbDeviceDetached));
receiverInitalized = true;
}
protected override void OnPause()
{
base.OnPause();
if (!receiverInitalized)
return;
// Unregister the broadcast receiver
var temp = detachedReceiver;
if (temp != null)
UnregisterReceiver(temp);
receiverInitalized = false;
}
internal static Task<IList<IUsbSerialDriver>> FindAllDriversAsync(UsbManager usbManager)
{
var table = UsbSerialProber.DefaultProbeTable;
// Add custom drivers here if needed
//table.AddProduct(0x1b4f, 0x0008, typeof(CdcAcmSerialDriver)); // IOIO OTG
//table.AddProduct(0x09D8, 0x0420, typeof(CdcAcmSerialDriver)); // Elatec TWN4
var prober = new UsbSerialProber(table);
return prober.FindAllDriversAsync(usbManager);
}
async Task OnItemClick(UsbSerialPort port)
{
Log.Info(TAG, $"Selected USB device: {port.Driver.Device.DeviceName}");
// Request user permission to connect to device
if (usbManager != null)
{
var permissionGranted = await usbManager.RequestPermissionAsync(port.Driver.Device, this);
if (permissionGranted)
{
// Start the UsbDeviceActivity for this device
var intent = new Intent(this, typeof(UsbDeviceActivity));
intent.PutExtra(UsbDeviceActivity.EXTRA_USB_DEVICE, new UsbSerialPortInfo(port));
StartActivity(intent);
}
else
{
Toast.MakeText(this, GetString(Resource.String.toast_permission_denied), ToastLength.Short)!.Show();
}
}
else
{
Toast.MakeText(this, GetString(Resource.String.toast_usb_manager_unavailable), ToastLength.Short)!.Show();
}
}
internal async Task PopulateDeviceListAsync()
{
ShowProgressBar();
Log.Info(TAG, "Refreshing device list...");
try
{
if (usbManager != null)
{
var drivers = await FindAllDriversAsync(usbManager);
usbPorts.Clear();
foreach (var driver in drivers)
{
var ports = driver.Ports;
Log.Info(TAG, $"+ {driver}: {ports.Count} port{(ports.Count == 1 ? "" : "s")}");
foreach (var port in ports)
{
usbPorts.Add(port);
}
}
RunOnUiThread(() =>
{
adapter!.NotifyDataSetChanged();
// Use plurals resource for device count
statusText!.Text = Resources!.GetQuantityString(Resource.Plurals.devices_found, usbPorts.Count, usbPorts.Count);
statusText!.Text += $"\n{GetString(Resource.String.tap_to_connect)}";
});
Log.Info(TAG, $"Done refreshing, {usbPorts.Count} entries found.");
}
}
catch (Exception ex)
{
Log.Error(TAG, $"Error populating device list: {ex.Message}");
RunOnUiThread(() =>
{
statusText!.Text = GetString(Resource.String.status_error_loading_devices);
});
}
finally
{
HideProgressBar();
}
}
void ShowProgressBar()
{
RunOnUiThread(() =>
{
progressBar!.Visibility = ViewStates.Visible;
statusText!.Text = GetString(Resource.String.status_refreshing);
});
}
void HideProgressBar()
{
RunOnUiThread(() =>
{
progressBar!.Visibility = ViewStates.Gone;
});
}
}
}