Skip to content

Commit dc0835a

Browse files
committed
Update for API 31
Updated UsbManagerExtenstions to support PendingIntents for API 31 Added write button to show show to send a string a text Bumped project sdk to Xamarin.Legacy.Sdk/0.2.0-alpha4 Added .NET as a target Updates sample app to .NET 7
1 parent 567f1f2 commit dc0835a

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

UsbSerialExampleApp/Resources/layout/serial_console.axml

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
android:layout_below="@+id/demoTitle"
2424
android:layout_toRightOf="@+id/sleepButton"
2525
android:text="@string/wakeup" />
26+
<Button
27+
android:id="@+id/helloButton"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_below="@+id/demoTitle"
31+
android:layout_toRightOf="@+id/wakeupButton"
32+
android:text="@string/sendCaption" />
2633
<View
2734
android:id="@+id/separator"
2835
android:layout_width="match_parent"

UsbSerialExampleApp/Resources/values/Strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<string name="refreshing">Refreshing...</string>
55
<string name="sleep">Sleep</string>
66
<string name="wakeup">Wakeup</string>
7+
<string name="sendCaption">hello</string>
78
</resources>

UsbSerialExampleApp/SerialConsoleActivity.cs

+10
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class SerialConsoleActivity : Activity
4444
ScrollView scrollView;
4545
Button sleepButton;
4646
Button wakeButton;
47+
Button sendHelloButton;
4748

4849
SerialInputOutputManager serialIoManager;
4950

@@ -62,6 +63,7 @@ protected override void OnCreate(Bundle savedInstanceState)
6263

6364
sleepButton = FindViewById<Button>(Resource.Id.sleepButton);
6465
wakeButton = FindViewById<Button>(Resource.Id.wakeupButton);
66+
sendHelloButton = FindViewById<Button>(Resource.Id.helloButton);
6567

6668
// The following arrays contain data that is used for a custom firmware for
6769
// the Elatec TWN4 RFID reader. This code is included here to show how to
@@ -78,6 +80,14 @@ protected override void OnCreate(Bundle savedInstanceState)
7880
{
7981
WriteData(wakedata);
8082
};
83+
84+
// The following shows one way to write ASCII text to the serial port
85+
sendHelloButton.Click += delegate
86+
{
87+
byte[] byteArray = Encoding.ASCII.GetBytes("Hello");
88+
89+
WriteData(byteArray);
90+
};
8191
}
8292

8393
protected override void OnPause()

UsbSerialExampleApp/UsbSerialExampleApp.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net7.0-android</TargetFramework>
3+
<TargetFramework>net6.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
55
<OutputType>Exe</OutputType>
66
<Nullable>disable</Nullable>

UsbSerialForAndroid.sln

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33620.401
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbSerialForAndroid", "UsbSerialForAndroid\UsbSerialForAndroid.csproj", "{A64567BC-548A-4CB7-9F2C-ECE75EE1A868}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbSerialForAndroid", "UsbSerialForAndroid\UsbSerialForAndroid.csproj", "{A64567BC-548A-4CB7-9F2C-ECE75EE1A868}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UsbSerialExampleApp", "UsbSerialExampleApp\UsbSerialExampleApp.csproj", "{59C7DFF3-4E5E-42D7-BB77-E6C4F4E74512}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbSerialExampleApp", "UsbSerialExampleApp\UsbSerialExampleApp.csproj", "{59C7DFF3-4E5E-42D7-BB77-E6C4F4E74512}"
99
EndProject
1010
Global
1111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,4 +27,7 @@ Global
2727
GlobalSection(SolutionProperties) = preSolution
2828
HideSolutionNode = FALSE
2929
EndGlobalSection
30+
GlobalSection(ExtensibilityGlobals) = postSolution
31+
SolutionGuid = {5A3F5991-F724-4D65-B724-3EC768A7304E}
32+
EndGlobalSection
3033
EndGlobal

UsbSerialForAndroid/Extensions/UsbManagerExtensions.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Android.Hardware.Usb;
1111
using Android.App;
1212
using Android.Content;
13+
using Android.OS;
1314
using System.Collections.Generic;
1415

1516
namespace Hoho.Android.UsbSerial.Util
@@ -28,7 +29,15 @@ public static Task<bool> RequestPermissionAsync(this UsbManager manager, UsbDevi
2829
var usbPermissionReceiver = new UsbPermissionReceiver(completionSource);
2930
context.RegisterReceiver(usbPermissionReceiver, new IntentFilter(ACTION_USB_PERMISSION));
3031

31-
var intent = PendingIntent.GetBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0);
32+
// Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
33+
#if NET6_0_OR_GREATER
34+
PendingIntentFlags pendingIntentFlags = Build.VERSION.SdkInt >= BuildVersionCodes.S ? PendingIntentFlags.Mutable : 0;
35+
#else
36+
PendingIntentFlags pendingIntentFlags = Build.VERSION.SdkInt >= (BuildVersionCodes)31 ? (PendingIntentFlags).33554432 : 0;
37+
#endif
38+
39+
var intent = PendingIntent.GetBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), pendingIntentFlags);
40+
3241
manager.RequestPermission(device, intent);
3342

3443
return completionSource.Task;

UsbSerialForAndroid/UsbSerialForAndroid.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<Project Sdk="Xamarin.Legacy.Sdk/0.2.0-alpha3">
1+
<Project Sdk="Xamarin.Legacy.Sdk/0.2.0-alpha4">
22
<PropertyGroup>
3-
<TargetFrameworks>net6.0-android;monoandroid5.0</TargetFrameworks>
3+
<TargetFrameworks>net6.0-android;net7.0-android;monoandroid5.0</TargetFrameworks>
44
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
55
<Nullable>disable</Nullable>
66
<ImplicitUsings>disable</ImplicitUsings>

0 commit comments

Comments
 (0)