-
Notifications
You must be signed in to change notification settings - Fork 73
/
MainActivity.cs
222 lines (174 loc) · 7.69 KB
/
MainActivity.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* Copyright 2017 Tyler Technologies Inc.
*
* Project home page: https://github.com/anotherlab/xamarin-usb-serial-for-android
* Portions of this library are based on usb-serial-for-android (https://github.com/mik3y/usb-serial-for-android).
* Portions of this library are based on Xamarin USB Serial for Android (https://bitbucket.org/lusovu/xamarinusbserial).
*/
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Hardware.Usb;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;
using Hoho.Android.UsbSerial.Driver;
using Hoho.Android.UsbSerial.Extensions;
using Hoho.Android.UsbSerial.Util;
[assembly: UsesFeature("android.hardware.usb.host")]
namespace UsbSerialExampleApp
{
[Activity(Label = "UsbSerialExampleApp", MainLauncher = true, Icon = "@drawable/icon")]
[IntentFilter(new[] { 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.hoho.android.usbserial.examples.USB_PERMISSION";
UsbManager usbManager;
ListView listView;
TextView progressBarTitle;
ProgressBar progressBar;
UsbSerialPortAdapter adapter;
BroadcastReceiver detachedReceiver;
UsbSerialPort selectedPort;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
usbManager = GetSystemService(Context.UsbService) as UsbManager;
listView = FindViewById<ListView>(Resource.Id.deviceList);
progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar);
progressBarTitle = FindViewById<TextView>(Resource.Id.progressBarTitle);
}
protected override async void OnResume()
{
base.OnResume();
adapter = new UsbSerialPortAdapter(this);
listView.Adapter = adapter;
listView.ItemClick += async (sender, e) => {
await OnItemClick(sender, e);
};
await PopulateListAsync();
//register the broadcast receivers
detachedReceiver = new UsbDeviceDetachedReceiver(this);
RegisterReceiver(detachedReceiver, new IntentFilter(UsbManager.ActionUsbDeviceDetached));
}
protected override void OnPause()
{
base.OnPause();
// unregister the broadcast receivers
var temp = detachedReceiver; // copy reference for thread safety
if (temp != null)
UnregisterReceiver(temp);
}
internal static Task<IList<IUsbSerialDriver>> FindAllDriversAsync(UsbManager usbManager)
{
// using the default probe table
// return UsbSerialProber.DefaultProber.FindAllDriversAsync (usbManager);
// adding a custom driver to the default probe table
var table = UsbSerialProber.DefaultProbeTable;
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(object sender, AdapterView.ItemClickEventArgs e)
{
Log.Info(TAG, "Pressed item " + e.Position);
if (e.Position >= adapter.Count)
{
Log.Info(TAG, "Illegal position.");
return;
}
// request user permission to connect to device
// NOTE: no request is shown to user if permission already granted
selectedPort = adapter.GetItem(e.Position);
var permissionGranted = await usbManager.RequestPermissionAsync(selectedPort.Driver.Device, this);
if (permissionGranted)
{
// start the SerialConsoleActivity for this device
var newIntent = new Intent(this, typeof(SerialConsoleActivity));
newIntent.PutExtra(SerialConsoleActivity.EXTRA_TAG, new UsbSerialPortInfo(selectedPort));
StartActivity(newIntent);
}
}
async Task PopulateListAsync()
{
ShowProgressBar();
Log.Info(TAG, "Refreshing device list ...");
var drivers = await FindAllDriversAsync(usbManager);
adapter.Clear();
foreach (var driver in drivers)
{
var ports = driver.Ports;
Log.Info(TAG, string.Format("+ {0}: {1} port{2}", driver, ports.Count, ports.Count == 1 ? string.Empty : "s"));
foreach (var port in ports)
adapter.Add(port);
}
adapter.NotifyDataSetChanged();
progressBarTitle.Text = string.Format("{0} device{1} found", adapter.Count, adapter.Count == 1 ? string.Empty : "s");
HideProgressBar();
Log.Info(TAG, "Done refreshing, " + adapter.Count + " entries found.");
}
void ShowProgressBar()
{
progressBar.Visibility = ViewStates.Visible;
progressBarTitle.Text = GetString(Resource.String.refreshing);
}
void HideProgressBar()
{
progressBar.Visibility = ViewStates.Invisible;
}
#region UsbSerialPortAdapter implementation
class UsbSerialPortAdapter : ArrayAdapter<UsbSerialPort>
{
public UsbSerialPortAdapter(Context context)
: base(context, global::Android.Resource.Layout.SimpleExpandableListItem2)
{
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var row = convertView;
if (row == null)
{
var inflater = Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
row = inflater.Inflate(global::Android.Resource.Layout.SimpleListItem2, null);
}
var port = this.GetItem(position);
var driver = port.GetDriver();
var device = driver.GetDevice();
var title = string.Format("Vendor {0} Product {1}",
HexDump.ToHexString((short)device.VendorId),
HexDump.ToHexString((short)device.ProductId));
row.FindViewById<TextView>(global::Android.Resource.Id.Text1).Text = title;
var subtitle = device.Class.SimpleName;
row.FindViewById<TextView>(global::Android.Resource.Id.Text2).Text = subtitle;
return row;
}
}
#endregion
#region UsbDeviceDetachedReceiver implementation
class UsbDeviceDetachedReceiver
: BroadcastReceiver
{
readonly string TAG = typeof(UsbDeviceDetachedReceiver).Name;
readonly MainActivity activity;
public UsbDeviceDetachedReceiver(MainActivity activity)
{
this.activity = activity;
}
public async override void OnReceive(Context context, Intent intent)
{
var device = intent.GetParcelableExtra(UsbManager.ExtraDevice) as UsbDevice;
Log.Info(TAG, "USB device detached: " + device.DeviceName);
await activity.PopulateListAsync();
}
}
#endregion
}
}