How to create a profile in Xamarin Forms, take 2

Hello!

Way back in April I tried to figure out how to automatically configure the profile to set up basic text formatting output. That thread is here:

Since the configuration was relatively simple, I left that task until last, and I am just now getting to implementing the code. After three days of searching and trying, I am not able to get auto import to work in any way, shape, or form. I have tried both a profile, and a configuration. I used the same names that were created by the export. I've tried at least three methods of copying the file. Nothing has worked. Help?

Here's my c# code:

using System;
using System.IO;
using Java.IO;
using System.Threading.Tasks;
using Xamarin.Forms;
using Android.Views;
using Android.Content.Res;
using Plugin.CurrentActivity;
using Acr.UserDialogs;

...

        private bool setuptask()
        {

            // Somewhere I saw a recommendation to not name the asset with a "known" suffix, use

            // something like .dat instead. So I tried that...
            string importpath = "/enterprise/device/settings/datawedge/autoimport/";
            string tempfile = "/enterprise/device/settings/datawedge/datawedge.dat";
            string importfile = "/enterprise/device/settings/datawedge/autoimport/datawedge.db";
            string otherfile = "/enterprise/device/settings/datawedge/autoimport/dwprofile_ScanOrders.db";
            string assetname = "datawedge.dat";
            string filename = "datawedge.db";

            try
            {
                // Make sure directory exists...
                if (Directory.Exists(importpath) == false)
                {
                    Directory.CreateDirectory(importpath);
                }

                // Clear out any old files... I have been switching back and forth

                // between the two files, so I clean up both of them before re-copying.
                if (System.IO.File.Exists(tempfile) == true)
                {
                    System.IO.File.Delete(tempfile);
                }

                if (System.IO.File.Exists(otherfile) == true)
                {
                    System.IO.File.Delete(otherfile);
                }

                // If I set a breakpoint here. The second time I call the function,

                // the file exists. (I wait a minute or two before the 2nd call.)
                if (System.IO.File.Exists(importfile) == true)
                {
                    System.IO.File.Delete(importfile);
                }

                // Create temp file from our assets
                Android.App.Activity context = CrossCurrentActivity.Current.Activity;
                AssetManager assets = context.Assets;

                using (BinaryReader br = new BinaryReader(assets.Open(assetname)))
                {
                    using (BinaryWriter bw = new BinaryWriter(new FileStream(tempfile, FileMode.Create)))
                    {
                        byte[] buffer = new byte[2048];
                        int len = 0;
                        while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            bw.Write(buffer, 0, len);
                        }
                    }
                }

              

                // Move the file into place so auto import doesn't try to pick it up before

                // the copy is complete.
                System.IO.File.Move(tempfile, importfile);

         

                // Set permissions, in case that was the problem. I was setting executable, but

                // saw example code that the author claimed worked, and he only set readable and

                // writable. So I took it back out.
                using (var newfile = new Java.IO.File(importpath, filename))
                {

                    // I added this bit so I could check that the file is actually

                    // created. I don't know if the contents are correct, but the copied

                    // file is ~9000 bytes long.
                    long len = newfile.Length();
                    if ((newfile.Exists() == false) || (len < 1.0))
                    {
                        // Error                   
                    }
                    else
                    {
                        newfile.SetReadable(true);
                        newfile.SetWritable(true);
                    }
                }
            }
            catch (Exception e)
            {
                UserDialogs.Instance.Alert("Exception caught: " + e.Message, "Error");
            }

            return true;
        }

Anonymous (not verified)
Hi, please see the code

Hi, please see the code sample at DataWedge Settings - Zebra Technologies TechDocs  which is known to work.  Also, I have an unofficial sample on my personal github, GitHub - darryncampbell/DataWedgeAutoImport: Sample app to show DataWedge auto import feature

Both samples are in Java but hopefully show the correct principle.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Karen Cate
*sigh*Yep, you pointed me at

*sigh*

Yep, you pointed me at those examples in the first thread (that I included a link to in my first post). The code I pasted above is how I translated those examples into C#. I then tried several variations and tweaks based upon every single forum post I could find. Nothing.

I can tell you that files ARE created in the autoimport directory. I can tell you that the files are not deleted from the autoimport directory. I tried both an exported configuration and an exported profile. I used the appropriate file names for each one.

The only possibility I can think of is that the file is getting corrupted by the transfer from the scanner to the mac. I have no way to figure out if that's the case. The fact that the autoimport directory is in a partition that cannot be accessed by any other means makes debugging this issue even more difficult.

I have no idea what to try next. Any help would be greatly appreciated.

Thanks!

-Karen

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Anonymous (not verified)
You should be able to test if

You should be able to test if the profiles are corrupted by pushing the files from your mac directly to the autoimport directory via adb, if that works then you'll know the profiles are not corrupted.

I too had trouble with the files being in the autoimport directory but not being read by DW - the resolution for me was to set RWX on the files.  I notice you are only setting the files as RWX for the _owner_, I suspect that is your problem, try using SetReadable(System.Boolean,System.Boolean) - Xamarin  and setting readable for everyone.  In Java, my relevant code is DataWedgeAutoImport/MainActivity.java at master · darryncampbell/DataWedgeAutoImport · GitHub

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments