1

我创建了一个使用 Zebra 打印 API 的 Mono for Android 应用程序。我已经设法在此处定义的 Java 绑定库和 Android 应用程序的标准 Mono 中引用了 ZSDK_API.jar文件

  • 我已将 .jar 文件添加到 JBL 项目(Jars 文件夹)并将其构建操作设置为 InputJar
  • 我还将 jar 添加到 Mono for Android 应用程序,并将构建操作设置为 AndroidJavaLibrary。

发现处理程序

public class DiscoveryHandler : IDiscoveryHandler
{
    private Discovery _reference;

    public DiscoveryHandler(Discovery reference)
    {
        _reference = reference;
    }

    public void DiscoveryError(string message)
    {
        new UIHelper(_reference).showErrorDialogOnGuiThread(message);
    }

    public void DiscoveryFinished()
    {
        _reference.RunOnUiThread(() =>
        {
            Toast.MakeText(_reference, " Discovered " + _reference.discoveredPrinters.Count + " devices", ToastLength.Short).Show();
            _reference.SetProgressBarIndeterminateVisibility(false);
        });
    }

    public void FoundPrinter(DiscoveredPrinter printer)
    {
        _reference.RunOnUiThread(() => 
        {
            DiscoveredPrinterBluetooth p = (DiscoveredPrinterBluetooth)printer;
            _reference.discoveredPrinters.Add(p.Address + " (" + p.FriendlyName + ")");
            _reference.mArrayAdapter.NotifyDataSetChanged();
        }); 
    }

    public void Dispose()
    {

    }

    public IntPtr Handle
    {
        get { return _reference.Handle; }
    }
}

发现.cs

public class Discovery : ListActivity
{
    public List<string> discoveredPrinters = null;
    public ArrayAdapter<string> mArrayAdapter;
    private static IDiscoveryHandler btDiscoveryHandler = null;


    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        RequestWindowFeature(WindowFeatures.IndeterminateProgress);
        SetContentView(Resource.Layout.discovery_results);

        SetProgressBarIndeterminateVisibility(true);
        discoveredPrinters = new List<string>();
        SetupListAdapter();
        btDiscoveryHandler = new DiscoveryHandler(this);

        try
        {
            new Thread(new ThreadStart(() =>
                {
                    Looper.Prepare();

                    try
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "Trying", ToastLength.Short).Show());
                        BluetoothDiscoverer.FindPrinters(this, btDiscoveryHandler);
                        RunOnUiThread(() => Toast.MakeText(this, "And...", ToastLength.Short).Show());
                    }
                    catch (ZebraPrinterConnectionException zex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(zex.Message);
                    }
                    catch (ThreadInterruptedException iex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(iex.Message);
                    }
                    catch (Exception ex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(ex.Message);
                    }
                    finally
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "Quitting looper", ToastLength.Short).Show());
                        Looper.MyLooper().Quit();
                        RunOnUiThread(() => Toast.MakeText(this, "Finished", ToastLength.Short).Show());
                    }
                })).Start();
        }
        catch (Exception ex)
        {
            new UIHelper(this).showErrorDialogOnGuiThread(ex.Message);
        }            
    }

    private void SetupListAdapter()
    {
        mArrayAdapter = new ArrayAdapter<string>(this, global::Android.Resource.Layout.SimpleListItem1, discoveredPrinters);
        ListAdapter = mArrayAdapter;
    }
}

我已确保清单正在请求蓝牙和 Bluetooth_Admin 以及互联网。

应用程序构建,但运行时只是崩溃,没有例外,只是说“应用程序已意外停止”

所有的类都被检测和编译,但我不知道为什么它会像这样轰炸。有没有人成功使用 Mono for Android - Zebra 集成?

4

1 回答 1

1

该死 - 我是一个印章!就在我发布它时,我开始思考——这可能与我将 IntPtr Handle 实现为父句柄这一事实有关——我是对的。这是工作代码的第一步(第一步 - 如果我必须回答自己的问题!):

public class Discovery : ListActivity, IDiscoveryHandler
{
    public List<string> discoveredPrinters = null;
    public ArrayAdapter<string> mArrayAdapter;        

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        RequestWindowFeature(WindowFeatures.IndeterminateProgress);
        SetContentView(Resource.Layout.discovery_results);

        SetProgressBarIndeterminateVisibility(true);
        discoveredPrinters = new List<string>();
        SetupListAdapter();            

        try
        {
            new Thread(new ThreadStart(() =>
                {
                    Looper.Prepare();

                    try
                    {                            
                        BluetoothDiscoverer.FindPrinters(this, this);                         
                    }
                    catch (ZebraPrinterConnectionException zex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(zex.Message);
                    }
                    catch (ThreadInterruptedException iex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(iex.Message);
                    }
                    catch (Exception ex)
                    {
                        new UIHelper(this).showErrorDialogOnGuiThread(ex.Message);
                    }
                    finally
                    {
                        RunOnUiThread(() => Toast.MakeText(this, "Quitting looper", ToastLength.Short).Show());
                        Looper.MyLooper().Quit();
                        RunOnUiThread(() => Toast.MakeText(this, "Finished", ToastLength.Short).Show());
                    }
                })).Start();
        }
        catch (Exception ex)
        {
            new UIHelper(this).showErrorDialogOnGuiThread(ex.Message);
        }            
    }

    private void SetupListAdapter()
    {
        mArrayAdapter = new ArrayAdapter<string>(this, global::Android.Resource.Layout.SimpleListItem1, discoveredPrinters);
        ListAdapter = mArrayAdapter;
    }

    public void DiscoveryError(string message)
    {
        new UIHelper(this).showErrorDialogOnGuiThread(message);
    }

    public void DiscoveryFinished()
    {
        RunOnUiThread(() =>
        {
            Toast.MakeText(this, " Discovered " + discoveredPrinters.Count + " devices", ToastLength.Short).Show();
            SetProgressBarIndeterminateVisibility(false);
        });
    }

    public void FoundPrinter(DiscoveredPrinter printer)
    {
        RunOnUiThread(() =>
        {

            DiscoveredPrinterBluetooth p = printer.JavaCast<DiscoveredPrinterBluetooth>();
            discoveredPrinters.Add(p.Address + " (" + p.FriendlyName + ")");
            mArrayAdapter.NotifyDataSetChanged();
        }); 
    }
}

}

于 2012-11-16T10:14:15.337 回答