Skip to content

Commit

Permalink
add Andriod, fix BLE, UI to suit both
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkimming2 committed Jun 7, 2024
1 parent 53978cd commit 19ba534
Show file tree
Hide file tree
Showing 45 changed files with 32,739 additions and 76 deletions.
19 changes: 19 additions & 0 deletions Batman2.Android/Assets/AboutAssets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with your package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
44 changes: 44 additions & 0 deletions Batman2.Android/Assets/DemoInit.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"Battery", "DT", "WH", "W", "A", "V"
"DemoBatt1",09/08/2021 21:30:31,277.62055,-2.31933,-0.081,28.666
"DemoBatt1",09/08/2021 23:38:27,274.96475,-2.30407,-0.079,28.626
"DemoBatt1",10/08/2021 13:22:14,274.87302,-2.2583,-0.078,28.623
"DemoBatt1",10/08/2021 13:27:18,274.93789,-0.33569,-0.012,28.648
"DemoBatt1",10/08/2021 13:34:05,274.96603,39.18457,1.353,28.952
"DemoBatt1",10/08/2021 16:35:52,274.86589,-0.33569,-0.01,28.648
"DemoBatt1",10/08/2021 19:47:34,205.83984,-0.35095,-0.012,27.772
"DemoBatt1",11/08/2021 22:40:13,205.79606,-0.35095,-0.012,28.678
"DemoBatt1",12/08/2021 16:41:28,205.73993,-2.24304,-0.081,27.801
"DemoBatt1",12/08/2021 20:15:56,140.37507,-0.32043,-0.012,26.709
"DemoBatt1",13/08/2021 09:41:16,140.43003,54.55017,2.005,27.207
"DemoBatt1",13/08/2021 09:48:21,146.92484,55.19103,2.014,27.393
"DemoBatt1",13/08/2021 13:49:53,294.95215,-2.42614,-0.083,29.103
"DemoBatt1",13/08/2021 14:51:48,294.3134,-2.27355,-0.078,29.043
"DemoBatt1",13/08/2021 20:33:08,190.29992,-0.35095,-0.012,27.312
"DemoBatt1",14/08/2021 21:01:42,142.93084,-2.16674,-0.081,26.602
"DemoBatt1",14/08/2021 22:41:17,142.2737,-2.28881,-0.086,26.61
"DemoBatt1",15/08/2021 13:54:25,140.86697,-79.8645,-2.581,26.042
"DemoBatt1",15/08/2021 18:29:08,99,-2.182,-0.083,24.51
"DemoBatt1",17/08/2021 13:11:37,5,0,0,0
"DemoBatt1",17/08/2021 13:16:37,9.85768,52.24609,2.011,25.977
"DemoBatt1",17/08/2021 15:20:32,123.79276,-0.35095,-0.012,27.811
"DemoBatt1",17/08/2021 17:21:08,123.7742,-0.27465,-0.009,27.998
"DemoBatt1",17/08/2021 17:26:08,128.50521,57.92236,2.023,28.618
"DemoBatt1",17/08/2021 17:41:08,141.71135,-0.27465,-0.009,28.278
"DemoBatt1",17/08/2021 19:08:33,141.69446,-0.39672,-0.015,28.222
"DemoBatt1",17/08/2021 19:13:33,145.775,57.75451,2.006,28.791
"DemoBatt1",17/08/2021 19:18:33,146.76955,-0.27465,-0.009,28.295
"DemoBatt1",17/08/2021 20:20:02,146.75897,-0.28991,-0.009,28.263
"DemoBatt1",17/08/2021 20:25:02,150.41582,52.01721,1.806,28.798
"DemoBatt1",17/08/2021 22:33:32,204.09158,-0.30517,-0.009,29.128
"DemoBatt1",17/08/2021 23:38:30,204.0691,-2.34985,-0.081,29.068
"DemoBatt1",17/08/2021 23:48:35,199.64622,-2.24304,-0.077,29.047
"DemoBatt1",18/08/2021 11:45:38,199.43106,-2.2583,-0.077,29.001
"DemoBatt1",18/08/2021 21:24:31,123.41898,-2.19726,-0.078,27.871
"DemoBatt1",19/08/2021 00:04:10,122.75263,-2.19726,-0.078,27.854
"DemoBatt1",19/08/2021 19:37:36,61.58707,-2.09045,-0.078,26.777
"DemoBatt1",19/08/2021 19:42:36,65.15132,54.82482,2.001,27.387
"DemoBatt1",19/08/2021 23:56:52,208.60938,-2.22778,-0.076,29.083
"DemoBatt1",19/08/2021 23:57:41,200,0,0,0
"DemoBatt1",20/08/2021 00:02:42,199.8158,-2.21252,-0.076,29.069
"DemoBatt1",20/08/2021 00:07:42,199.63146,-2.21252,-0.076,29.059
"DemoBatt1",20/08/2021 00:12:42,199.47888,-0.30517,-0.009,29.067
130 changes: 130 additions & 0 deletions Batman2.Android/Batman2.Android.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E6ECEBEB-46B5-4816-9DC2-07FF64A58379}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TemplateGuid>{c9e5eea5-ca05-42a1-839b-61506e0a37df}</TemplateGuid>
<OutputType>Library</OutputType>
<RootNamespace>Batman2.Droid</RootNamespace>
<AssemblyName>Batman2.Android</AssemblyName>
<Deterministic>True</Deterministic>
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v13.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<EmbedAssembliesIntoApk>false</EmbedAssembliesIntoApk>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
<MandroidI18n />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Android" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml" />
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.Essentials" Version="1.7.0" />
<PackageReference Include="Plugin.BLE">
<Version>2.1.2</Version>
</PackageReference>
<PackageReference Include="sqlite-net-pcl">
<Version>1.7.335</Version>
</PackageReference>
<PackageReference Include="Plugin.DeviceOrientation">
<Version>1.0.7</Version>
</PackageReference>
<PackageReference Include="Accord.Statistics">
<Version>3.8.0</Version>
</PackageReference>
<PackageReference Include="Microcharts.Forms-Fdab">
<Version>0.9.5.40</Version>
</PackageReference>
<PackageReference Include="SkiaSharp.Views.Forms">
<Version>2.80.3</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms">
<Version>5.0.0.2291</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Include="MainActivity.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
<None Include="Properties\AndroidManifest.xml" />
<AndroidAsset Include="Assets\DemoInit.csv" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\styles.xml" />
<AndroidResource Include="Resources\values\colors.xml" />
<AndroidResource Include="Resources\mipmap-anydpi-v26\icon.xml" />
<AndroidResource Include="Resources\mipmap-anydpi-v26\icon_round.xml" />
<AndroidResource Include="Resources\mipmap-hdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-hdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-mdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-mdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-xhdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-xhdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-xxhdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-xxhdpi\launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\icon.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\launcher_foreground.png" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\Batman2\Batman2.csproj">
<Project>{f1a63a8a-8e4d-4e76-aba9-d5cd45b4bb21}</Project>
<Name>Batman2</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-hdpi\Logo.png" />
<AndroidResource Include="Resources\drawable-mdpi\Logo.png" />
<AndroidResource Include="Resources\drawable-xhdpi\Logo.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\Logo.png" />
<AndroidResource Include="Resources\drawable-xxxhdpi\Logo.png" />
<AndroidResource Include="Resources\drawable\Logo.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>
34 changes: 34 additions & 0 deletions Batman2.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using static Android.Provider.SyncStateContract;
using Xamarin.Forms;
using Java.Security;

namespace Batman2.Droid
{
[Activity(Label = "Batman2", Icon = "@drawable/Logo", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
AndroidPermissions permissions = new AndroidPermissions();
await permissions.CheckAndRequestBluetoothPermissions();

LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
48 changes: 48 additions & 0 deletions Batman2.Android/Permissions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Batman2.Droid {
public class AndroidPermissions
{
public async Task<PermissionStatus> CheckAndRequestBluetoothPermissions()
{
PermissionStatus status;
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.S)
{
status = await Permissions.CheckStatusAsync<BluetoothSPermission>();

if (status == PermissionStatus.Granted)
return status;

status = await Permissions.RequestAsync<BluetoothSPermission>();
}
else
{
status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

if (status == PermissionStatus.Granted)
return status;


if (Permissions.ShouldShowRationale<Permissions.LocationWhenInUse>())
{
await Application.Current.MainPage.DisplayAlert("Permission required", "Location permission is required for bluetooth scanning. We do not store or use your location at all.", "OK");
}

status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
return status;
}
}

public class BluetoothSPermission : Xamarin.Essentials.Permissions.BasePlatformPermission
{
public override (string androidPermission, bool isRuntime)[] RequiredPermissions => new List<(string androidPermission, bool isRuntime)>
{
(Android.Manifest.Permission.BluetoothScan, true),
(Android.Manifest.Permission.BluetoothConnect, true)
}.ToArray();
}
}
8 changes: 8 additions & 0 deletions Batman2.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.kmleung.Batman3">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
<application android:label="Batman2.Android" android:theme="@style/MainTheme"></application>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
</manifest>
30 changes: 30 additions & 0 deletions Batman2.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Android.App;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Batman2.Android")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Batman2.Android")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

// Add some common permissions, these can be removed if not needed
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
50 changes: 50 additions & 0 deletions Batman2.Android/Resources/AboutResources.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable-hdpi/
icon.png

drawable-ldpi/
icon.png

drawable-mdpi/
icon.png

layout/
main.xml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called
"Resource" that contains the tokens for each one of the resources included. For example,
for the above Resources layout, this is what the Resource class would expose:

public class Resource {
public class drawable {
public const int icon = 0x123;
}

public class layout {
public const int main = 0x456;
}

public class strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use R.drawable.icon to reference the drawable/icon.png file, or Resource.layout.main
to reference the layout/main.xml file, or Resource.strings.first_string to reference the first
string in the dictionary file values/strings.xml.
Loading

0 comments on commit 19ba534

Please sign in to comment.