1

I'm relatively new to software development for the Hololens 2 and have a pretty big problem I've been messing around with for a long time and I'm slowly running out of ideas.

My project looks like this. I've written a Unity application to capture data and store it in a database (sqlite). A Xamarin.Forms UWP application is supposed to take the data from the database and use it to paint charts for better visualisation. The big problem is, both apps need to be able to access the same database on the Hololens 2. I thought that I could be the database on a usb stick and both apps could access the usb stick. In the Xamarin app and in the Unity app "removable storage" is enabled. In the Xamarin App I have made the file extension known under Appmanifest declaration. I am trying to get the connection with the following commands:

namespace ARScoliosis.XamarinApp.UWP.Services
{
public class DatabasePath : IDatabasePath
{
    public async Task<string> GetLocalFilePath()
    {

        var messageDialog = new MessageDialog("No Usb connection has been found.");

        StorageFolder externalDevices = KnownFolders.RemovableDevices;

        if(externalDevices == null)
        {
            messageDialog.Commands.Add(new UICommand("No Devices", null));

        }

        StorageFolder usbStick = (await externalDevices.GetFoldersAsync()).FirstOrDefault(); <---According to debugging it stops here and jumps to optionsBuilder.UseSqlite($"Filename={databasePath}");
        
        if (usbStick == null)
        {
            messageDialog.Commands.Add(new UICommand("No UsbStick", null));
        }

        var usbStickFolder = await usbStick.CreateFolderAsync("DB", CreationCollisionOption.OpenIfExists);

        if (usbStickFolder == null)
        {
            messageDialog.Commands.Add(new UICommand("No Folder", null));
        }
    
        var file = await usbStickFolder.CreateFileAsync("Database.db", CreationCollisionOption.OpenIfExists);

        if(file == null)
        {
            messageDialog.Commands.Add(new UICommand("No File", null));
        }
        //var success = await Launcher.LaunchFileAsync(file);

        return file.ToString();
       
    }

My dbcontext file looks something like this:

      namespace XamarinApp.Authentication
      {
      public partial class DBContext : DbContext
      {
      public DBContext()
    {     
       this.Database.EnsureCreated(); <--- Microsoft.Data.Sqlite.SqliteException: "SQLite Error 14: 'unable to open database file'."
       this.Database.Migrate();
    }

    public virtual DbSet<ItemData> ItemDatas { get; set; }

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            var databasePath = DependencyService.Get<IDatabasePath>().GetLocalFilePath();
            optionsBuilder.UseSqlite($"Filename={databasePath}");
        }
    }

  namespace XamarinApp.Helpers
  {
     public interface IDatabasePath
    {
    Task<string> GetLocalFilePath();
    }
  }

Unfortunately this code does not find the Usb stick on the Hololens, i think. When I look in file explorer, I see the stick with all its data. In the Unity App, the stick is also not found, although I use the same code here, only slightly modified.

Does anyone know where my error lies, why I can't access the USB stick with either of the two apps? Has anyone tried something similar and knows how to do it?

i would like to thank you in advance for your help.

Thank you very much.

KiriKatha
  • 41
  • 2

2 Answers2

1

****Hi Hernando - MSFT,

Please excuse my late reply. i had somehow forgotten. I have found a way where I can find my database on the usb stick.

public static async Task<string> GetUsbStick()
    {
        StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();  

        if (UsbDrive == null)
        {
            throw new InvalidOperationException("Usb Drive Not Found");
        }
        else
        {
            IReadOnlyList<StorageFile> FileList = await UsbDrive.GetFilesAsync();
            var Path = UsbDrive.Path.Replace('\\','/');

            foreach (StorageFile File in FileList)
            {
               
                var DBFound = File.Name.Contains("test.db"); 

                if (DBFound == true)
                {
                    return Path + File.Name;
                }
            }
            throw new InvalidOperationException("DataBaseNotFound");

        }

    }

There I get the exact path for the database output. Only that somehow brings nothing. I cannot open it in the next step. "Sqlite cant open database" it says.

public static async Task<int> Init()
    {
        try
        {
            DatabasePath = await GetUsbStick();
           
            StrConnDatabase = "Data Source" + "=" + DatabasePath + ";Mode=ReadWrite;";

        }
        catch (Exception io)
        {
            IsInit = false;
            throw new InvalidOperationException(io.Message);
        }
        finally
        {
            //Try to Open Database to Read
            using (var db = new Microsoft.Data.Sqlite.SqliteConnection(StrConnDatabase))
            {
                try
                {
                    db.Open(); //<- here it breaks off
                    db.Close();
                    IsInit = true;
                }
                catch (Exception io)
                {
                   throw new InvalidOperationException(io.Message);
                }
            }
        }
        return 1; // Succes 

    }

What could be the reason that this does not work?

is there a way to create a working copy within the app, which is then written back to the usb stick?

KiriKatha
  • 41
  • 2
  • The follow-up question about SQLite should be its own question rather than hidden in your answer here. It isn't HoloLens specific so you can look at the general UWP answers to it. The quick summary is that SQLite uses API that can't access files outside of the app install and app data folders. The easy solution is to copy the db to the app's local storage for use. Otherwise you can rebuild SQLite or redirect its calls. See https://stackoverflow.com/questions/59135054/how-can-i-access-files-like-a-sqlite-database-outside-of-my-apps-folder-in-a – Rob Caplan - MSFT Jul 26 '21 at 18:50
0

KnownFolders.RemovableDevices doesn't be supported on the HoloLens, for more information please see:Known folders. It is recommended to take a try at File pickers to pick one file manually.

Hernando - MSFT
  • 2,895
  • 1
  • 5
  • 11