Skip to content

Commit

Permalink
Feature/maui (#1042)
Browse files Browse the repository at this point in the history
* add MediaPickSample

* Compact code

* Compact code

* Optimize the code

* fix android 13 ble bug for device name is empty

* fix bug in device only add

* add ios push sample

---------

Co-authored-by: dkx4m8w <[email protected]>
Co-authored-by: LONSID\guohao <[email protected]>
  • Loading branch information
3 people authored Feb 28, 2023
1 parent 84b2d17 commit 7e0ac4b
Show file tree
Hide file tree
Showing 101 changed files with 3,674 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/Masa.Blazor.Maui.Plugin/CleanBinAndObj.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
for /f "tokens=*" %%a in ('dir obj /b /ad /s ^|sort') do rd "%%a" /s/q
for /f "tokens=*" %%a in ('dir bin /b /ad /s ^|sort') do rd "%%a" /s/q
24 changes: 23 additions & 1 deletion src/Masa.Blazor.Maui.Plugin/Masa.Blazor.Full.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Blazor.Maui.Plugin.Bad
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BadgerSample", "BadgerSample\BadgerSample.csproj", "{6A06F0A4-4B4B-4956-B57C-B87B2A972638}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Blazor.Maui.Plugin.Badger", "Masa.Blazor.Maui.Plugin.Badger\Masa.Blazor.Maui.Plugin.Badger.csproj", "{BB195AC4-3068-4122-B13C-64576EC289D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Blazor.Maui.Plugin.Badger", "Masa.Blazor.Maui.Plugin.Badger\Masa.Blazor.Maui.Plugin.Badger.csproj", "{BB195AC4-3068-4122-B13C-64576EC289D4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MediaPick", "MediaPick", "{2B242685-3450-4573-8C4F-5BB6FAF98C70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediaPickSample", "MediaPickSample\MediaPickSample.csproj", "{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "iOSPush", "iOSPush", "{0DA1E67D-9550-4DA5-9ED4-FD1D3B14E2A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "iOSPushSample", "iOSPushSample\iOSPushSample.csproj", "{57747E94-7876-4D9F-AAF8-75EA306511BE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -113,6 +121,18 @@ Global
{BB195AC4-3068-4122-B13C-64576EC289D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB195AC4-3068-4122-B13C-64576EC289D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB195AC4-3068-4122-B13C-64576EC289D4}.Release|Any CPU.Build.0 = Release|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Release|Any CPU.Build.0 = Release|Any CPU
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58}.Release|Any CPU.Deploy.0 = Release|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Release|Any CPU.Build.0 = Release|Any CPU
{57747E94-7876-4D9F-AAF8-75EA306511BE}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -131,6 +151,8 @@ Global
{690E78A1-9065-4210-996B-FC378F32239C} = {ADF847FC-9E99-4718-BA0A-A895761A2883}
{6A06F0A4-4B4B-4956-B57C-B87B2A972638} = {ADF847FC-9E99-4718-BA0A-A895761A2883}
{BB195AC4-3068-4122-B13C-64576EC289D4} = {ADF847FC-9E99-4718-BA0A-A895761A2883}
{D5DBC320-F0F3-44A9-801B-1DAB9C308D58} = {2B242685-3450-4573-8C4F-5BB6FAF98C70}
{57747E94-7876-4D9F-AAF8-75EA306511BE} = {0DA1E67D-9550-4DA5-9ED4-FD1D3B14E2A1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F999AB5B-4C79-44A5-A1BA-90C268DFB039}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using UIKit;
using Foundation;
using UIKit;
using UserNotifications;
namespace Masa.Blazor.Maui.Plugin.Badger
{
public static partial class MasaMauiBadgerService
{
private static void PlatformSetNotificationCount(int count)

{
// Requests the user’s authorization to allow local and remote notifications for your app.
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Badge, (r, e) =>{});

// The number currently set as the badge of the app icon on the Home screen
// Set to 0 (zero) to hide the badge number. The default value of this property is 0.
UIApplication.SharedApplication.ApplicationIconBadgeNumber = count;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0-android;net7.0-ios</TargetFrameworks>
<TargetFrameworks>net7.0-ios;net7.0-android</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.4</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.3</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">28.0</SupportedOSPlatformVersion>

</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ public static event EventHandler AvailabilityChanged
}
}
}
public static Task<IReadOnlyCollection<BluetoothDevice>> ScanForDevicesAsync(string deviceName="")
public static Task<IReadOnlyCollection<BluetoothDevice>> ScanForDevicesAsync(string deviceName = "")
{
return PlatformScanForDevices(deviceName);
}



public static bool IsEnabled()
{
var isEnable = false;
Expand Down Expand Up @@ -70,7 +69,6 @@ public static async Task SendDataAsync(string deviceName, Guid servicesUuid, Gui
{

await PlatformSendDataAsync(deviceName, servicesUuid, characteristicsUuid, dataBytes, gattCharacteristicValueChangedEventArgs);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ partial class BluetoothDevice
private readonly Android.Bluetooth.BluetoothDevice _device;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private RemoteGattServer _gattServer;

private BluetoothDevice(Android.Bluetooth.BluetoothDevice device)
{
_device = device;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Android.Bluetooth;
using Android.Bluetooth.LE;
using static Android.Media.Midi.MidiManager;

namespace Masa.Blazor.Maui.Plugin.Bluetooth
{
Expand All @@ -11,14 +12,17 @@ public static partial class MasaMauiBluetoothService
private static ScanSettings _settings = new ScanSettings.Builder()
.SetScanMode(Android.Bluetooth.LE.ScanMode.Balanced)
?.Build();
private static DevicesCallback _callback = new DevicesCallback();

public static bool PlatformIsEnabled()
{
return _bluetoothAdapter is { IsEnabled: true };
}

private static async Task<IReadOnlyCollection<BluetoothDevice>> PlatformScanForDevices(string deviceName = "")
{
_bluetoothAdapter.Dispose();
_bluetoothAdapter = _manager?.Adapter;
var _callback = new DevicesCallback();
time1 = DateTime.Now;
_bluetoothAdapter.BluetoothLeScanner.StartScan(null, _settings, _callback);

Expand Down Expand Up @@ -47,7 +51,7 @@ await Task.Run(() =>

public static async Task PlatformSendDataAsync(string deviceName, Guid servicesUuid, Guid? characteristicsUuid, byte[] dataBytes, EventHandler<GattCharacteristicValueChangedEventArgs> gattCharacteristicValueChangedEventArgs)
{
BluetoothDevice blueDevice = _discoveredDevices.FirstOrDefault(o => o.Name == deviceName);
BluetoothDevice blueDevice = _discoveredDevices.FirstOrDefault(o => o.LocalName == deviceName);
if (!blueDevice.Gatt.IsConnected)
{
await blueDevice.Gatt.ConnectAsync();
Expand Down Expand Up @@ -133,20 +137,26 @@ public override void OnBatchScanResults(IList<ScanResult> results)

public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
{

System.Diagnostics.Debug.WriteLine("OnScanResult");
System.Diagnostics.Debug.WriteLine($"{result.Device.Name}:{result.ScanRecord.DeviceName}");
if (string.IsNullOrEmpty(scanDeviceName))
{
if (!Devices.Contains(result.Device))
if (!Devices.Contains(result.Device) && (!string.IsNullOrEmpty(result.Device.Name) || !string.IsNullOrEmpty(result.ScanRecord.DeviceName)))
{
Devices.Add(result.Device);
Devices.Last().LocalName = string.IsNullOrEmpty(result.Device.Name)
? result.ScanRecord.DeviceName
: result.Device.Name;
}
}
else
{
if (result.Device?.Name == scanDeviceName)
if (result.Device?.Name == scanDeviceName || result.ScanRecord?.DeviceName == scanDeviceName)
{
Devices.Add(result.Device);
Devices.Last().LocalName = string.IsNullOrEmpty(result.Device.Name)
? result.ScanRecord.DeviceName
: result.Device.Name;
_eventWaitHandle.Set();
}
else
Expand All @@ -156,7 +166,7 @@ public override void OnScanResult(ScanCallbackType callbackType, ScanResult resu
_eventWaitHandle.Set();
time1 = DateTime.MinValue;
}
}
}
}

base.OnScanResult(callbackType, result);
Expand All @@ -175,19 +185,30 @@ public override void OnScanFailed(ScanFailure errorCode)

private class BluetoothPermissions : Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions =>
new List<(string androidPermission, bool isRuntime)>
public override (string androidPermission, bool isRuntime)[] RequiredPermissions
{
get
{
(global::Android.Manifest.Permission.AccessFineLocation, true),
(global::Android.Manifest.Permission.Bluetooth, true),
(global::Android.Manifest.Permission.BluetoothAdmin, true),

#if ANDROID31_0_OR_GREATER
//(global::Android.Manifest.Permission.BluetoothConnect, true),
//(global::Android.Manifest.Permission.BluetoothScan, true),
#endif

}.ToArray();
var list = new List<(string androidPermission, bool isRuntime)>
{

};
if (OperatingSystem.IsAndroidVersionAtLeast(32))
{
list.Add(new(global::Android.Manifest.Permission.BluetoothConnect, true));
list.Add(new(global::Android.Manifest.Permission.BluetoothScan, true));
list.Add(new(global::Android.Manifest.Permission.AccessFineLocation, true));
list.Add(new(global::Android.Manifest.Permission.BluetoothAdvertise, true));
}
else
{
list.Add(new(global::Android.Manifest.Permission.Bluetooth, true));
list.Add(new(global::Android.Manifest.Permission.AccessFineLocation, true));
list.Add(new(global::Android.Manifest.Permission.BluetoothAdmin, true));
}
return list.ToArray();
}
}
}
static Task<bool> PlatformGetAvailability()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public static async Task<PermissionStatus> PlatformCheckAndRequestBluetoothPermi

private static async Task<IReadOnlyCollection<BluetoothDevice>> PlatformScanForDevices(string deviceName = "")
{
_manager.Dispose();
_manager = new CBCentralManager(_delegate, DispatchQueue.DefaultGlobalQueue, new CBCentralInitOptions
{
ShowPowerAlert = true,
});

_delegate.ClearDevices();
time1 = DateTime.Now;
if (!_manager.IsScanning)
{
Expand Down Expand Up @@ -74,7 +81,7 @@ await Task.Run(() =>

public static async Task PlatformSendDataAsync(string deviceName, Guid servicesUuid, Guid? characteristicsUuid, byte[] dataBytes, EventHandler<GattCharacteristicValueChangedEventArgs> gattCharacteristicValueChangedEventArgs)
{
BluetoothDevice blueDevice = _discoveredDevices.FirstOrDefault(o => o.Name == deviceName);
BluetoothDevice blueDevice = _discoveredDevices.FirstOrDefault(o => o.LocalName == deviceName);
if (!blueDevice.Gatt.IsConnected)
{
await blueDevice.Gatt.ConnectAsync();
Expand Down Expand Up @@ -123,7 +130,12 @@ private class BluetoothDelegate : CBCentralManagerDelegate
{
private EventWaitHandle _eventWaitHandle = new(false, EventResetMode.AutoReset);
private string scanDeviceName;
public List<BluetoothDevice> Devices { get; } = new();

public void ClearDevices()
{
Devices = new();
}
public List<BluetoothDevice> Devices { get; private set; } = new();

public void WaitOne()
{
Expand Down Expand Up @@ -157,13 +169,15 @@ public override void DiscoveredPeripheral(CBCentralManager central, CBPeriphera
if (!Devices.Contains(device))
{
Devices.Add(device);
Devices.Last().LocalName = device.Name;
}
}
else
{
if (device?.Name == scanDeviceName)
{
Devices.Add(device);
Devices.Last().LocalName = device.Name;
_eventWaitHandle.Set();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ZXing.Net.Maui" Version="0.1.0-preview.7" />
<PackageReference Include="ZXing.Net.Maui" Version="0.3.0-preview.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,4 +37,9 @@
<Folder Include="Platforms\iOS\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="C:\Users\guohao\source\repos\ZXing.Net.Maui\ZXing.Net.MAUI.Controls\ZXing.Net.MAUI.Controls.csproj" />
<ProjectReference Include="C:\Users\guohao\source\repos\ZXing.Net.Maui\ZXing.Net.MAUI\ZXing.Net.MAUI.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI"
xmlns:controls="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
x:Class="Masa.Blazor.Maui.Plugin.QrCode.MasaBarcodeReader"
Title="MasaBarcodeReader">

<Grid RowDefinitions="2*,4*,1*,1*">

<zxing:CameraBarcodeReaderView
<controls:CameraBarcodeReaderView
Grid.Row="0" Grid.RowSpan="4"
x:Name="BarcodeReader"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public MasaBarcodeReader()
BarcodeReader.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.All,
AutoRotate = true,
Multiple = true
AutoRotate = false,
Multiple = false
};
}
private void CameraBarcodeReaderView_BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions src/Masa.Blazor.Maui.Plugin/MasaSample/MasaSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<CodesignProvision>VS: WildCard Development</CodesignProvision>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net7.0-ios'">
<CodesignKey>Apple Development: Vic Parker (LXZ3399LT9)</CodesignKey>
<CodesignProvision>VS: WildCard Development</CodesignProvision>
</PropertyGroup>


<ItemGroup>
<!-- App Icon -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!--蓝牙权限-->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<!--让应用启动设备发现或操纵蓝牙设置-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
<!-- 如果设配Android9及更低版本,可以申请 ACCESS_COARSE_LOCATION -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<!-- Needed only if your app makes the device discoverable to Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- Needed only if your app communicates with already-paired Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
</manifest>
26 changes: 26 additions & 0 deletions src/Masa.Blazor.Maui.Plugin/MediaPickSample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MediaPickSample"
x:Class="MediaPickSample.App">
<Application.Resources>
<ResourceDictionary>

<Color x:Key="PageBackgroundColor">#512bdf</Color>
<Color x:Key="PrimaryTextColor">White</Color>

<Style TargetType="Label">
<Setter Property="TextColor" Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="FontFamily" Value="OpenSansRegular" />
</Style>

<Style TargetType="Button">
<Setter Property="TextColor" Value="{DynamicResource PrimaryTextColor}" />
<Setter Property="FontFamily" Value="OpenSansRegular" />
<Setter Property="BackgroundColor" Value="#2b0b98" />
<Setter Property="Padding" Value="14,10" />
</Style>

</ResourceDictionary>
</Application.Resources>
</Application>
Loading

0 comments on commit 7e0ac4b

Please sign in to comment.