From cbe29e197a703ec5b6b7cc8221b8719192bf47c8 Mon Sep 17 00:00:00 2001
From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com>
Date: Tue, 15 Oct 2024 18:23:45 +0100
Subject: [PATCH] working on VS studio android emulator, need to get running on
 an actual device.

---
 .../Platforms/Android/AndroidManifest.xml     | 17 ++++++++----
 App/RobobinApp/RobobinApp.csproj              |  1 +
 App/RobobinApp/RobobinApp.csproj.user         |  4 +--
 .../ViewModels/ConnectionPageViewModel.cs     | 27 ++++++++++++++++++-
 .../ViewModels/MainPageViewModel.cs           |  1 +
 App/RobobinApp/Views/ConnectionPage.xaml.cs   |  1 +
 App/RobobinApp/Views/MainPage.xaml            |  2 +-
 7 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/App/RobobinApp/Platforms/Android/AndroidManifest.xml b/App/RobobinApp/Platforms/Android/AndroidManifest.xml
index bdec9b59..5601ae70 100644
--- a/App/RobobinApp/Platforms/Android/AndroidManifest.xml
+++ b/App/RobobinApp/Platforms/Android/AndroidManifest.xml
@@ -1,6 +1,13 @@
-<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android">
-	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
-	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
-	<uses-permission android:name="android.permission.INTERNET" />
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1">
+	<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true" android:label="RoboBin"></application>
+	<!-- Required permissions -->
+	<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_ADMIN" />
+	<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
+	<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
+	<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
+	<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
 </manifest>
\ No newline at end of file
diff --git a/App/RobobinApp/RobobinApp.csproj b/App/RobobinApp/RobobinApp.csproj
index 4a445af5..03a33ca2 100644
--- a/App/RobobinApp/RobobinApp.csproj
+++ b/App/RobobinApp/RobobinApp.csproj
@@ -61,6 +61,7 @@
 		<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.91" />
 		<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.91" />
 		<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
+		<PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.91" />
 		<PackageReference Include="Plugin.BLE" Version="3.1.0" />
 		<PackageReference Include="Shiny" Version="2.7.3" />
 		<PackageReference Include="Shiny.BluetoothLE" Version="3.3.3" />
diff --git a/App/RobobinApp/RobobinApp.csproj.user b/App/RobobinApp/RobobinApp.csproj.user
index d5dbc44f..8081d85a 100644
--- a/App/RobobinApp/RobobinApp.csproj.user
+++ b/App/RobobinApp/RobobinApp.csproj.user
@@ -2,8 +2,8 @@
 <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
-    <ActiveDebugFramework>net8.0-windows10.0.19041.0</ActiveDebugFramework>
-    <ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
+    <ActiveDebugFramework>net8.0-android</ActiveDebugFramework>
+    <ActiveDebugProfile>Pixel 5 - API 34 (Android 14.0 - API 34)</ActiveDebugProfile>
     <SelectedPlatformGroup>Emulator</SelectedPlatformGroup>
     <DefaultDevice>pixel_5_-_api_34</DefaultDevice>
   </PropertyGroup>
diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
index fe675991..f79e46b4 100644
--- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
+++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
@@ -5,6 +5,7 @@ using RobobinApp.Views;
 using Plugin.BLE;
 using Plugin.BLE.Abstractions.Contracts;
 using Plugin.BLE.Abstractions.Exceptions;
+//using Microsoft.Maui.E
 
 namespace RobobinApp.ViewModels
 {
@@ -29,7 +30,8 @@ namespace RobobinApp.ViewModels
             _bluetoothLE = CrossBluetoothLE.Current;
             _adapter = _bluetoothLE.Adapter;
 
-            ScanDevices(); // Start scanning for devices
+            // Check and request Bluetooth permissions before scanning
+            CheckAndRequestBluetoothPermissions();
         }
 
         public BluetoothDevice SelectedDevice
@@ -42,6 +44,29 @@ namespace RobobinApp.ViewModels
             }
         }
 
+       
+        private async void CheckAndRequestBluetoothPermissions()
+        {
+            // Check if the Bluetooth scan permission is granted
+            var status = await Permissions.CheckStatusAsync<Permissions.Bluetooth>();
+            if (status != PermissionStatus.Granted)
+            {
+                // Request permission
+                status = await Permissions.RequestAsync<Permissions.Bluetooth>();
+            }
+
+            if (status == PermissionStatus.Granted)
+            {
+                // Permission granted, proceed with scanning devices
+                ScanDevices();
+            }
+            else
+            {
+                // Permission denied, handle accordingly
+                await Application.Current.MainPage.DisplayAlert("Permissions", "Bluetooth scan permission is required to discover devices.", "OK");
+            }
+        }
+
         public async Task OnGoHome()
         {
             await Application.Current.MainPage.Navigation.PushAsync(new MainPage());
diff --git a/App/RobobinApp/ViewModels/MainPageViewModel.cs b/App/RobobinApp/ViewModels/MainPageViewModel.cs
index 8665557a..784b9504 100644
--- a/App/RobobinApp/ViewModels/MainPageViewModel.cs
+++ b/App/RobobinApp/ViewModels/MainPageViewModel.cs
@@ -8,6 +8,7 @@ using Shiny.BluetoothLE;
 namespace RobobinApp.ViewModels
 
 {
+    //https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Domain-ID.html <- Should just work once wifi connection
     public class MainPageViewModel : INotifyPropertyChanged
     {
         private bool _isBusy;
diff --git a/App/RobobinApp/Views/ConnectionPage.xaml.cs b/App/RobobinApp/Views/ConnectionPage.xaml.cs
index 446d0165..27c0fce5 100644
--- a/App/RobobinApp/Views/ConnectionPage.xaml.cs
+++ b/App/RobobinApp/Views/ConnectionPage.xaml.cs
@@ -6,4 +6,5 @@ public partial class ConnectionPage : ContentPage
 	{
 		InitializeComponent();
 	}
+
 }
\ No newline at end of file
diff --git a/App/RobobinApp/Views/MainPage.xaml b/App/RobobinApp/Views/MainPage.xaml
index 983bcd12..75869e06 100644
--- a/App/RobobinApp/Views/MainPage.xaml
+++ b/App/RobobinApp/Views/MainPage.xaml
@@ -42,7 +42,7 @@
         <Frame BackgroundColor="Lavender" BorderColor="Black" CornerRadius="5" 
                HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
                Grid.Column="2" Grid.Row="0">
-            <Label Text="Right Box" HorizontalOptions="Center" VerticalOptions="Center"/>
+            <Button Text="Right Box" HorizontalOptions="Center" VerticalOptions="Center"  Command="{Binding ConnectToRobobinCommand}"/>
         </Frame>
     </Grid>
 </ContentPage>
-- 
GitLab