Пример #1
0
        public async void OnDeviceArrival(object sender, CaptureHelper.DeviceArgs arrivedDevice)
        {
            // There are a few interesting things you can find out about the arrived scanner
            // by calling the GetDeviceInfo() member function. It returns a DeviceInfo
            // structure which, among other things, contains the FriendlyName of the device.
            string FriendlyName = arrivedDevice.CaptureDevice.GetDeviceInfo().Name;

            // Other CapturHelper functions can be used to learn even more about
            // the connected device, such as the battery level (and many other
            // device properties).
            CaptureHelperDevice.BatteryLevelResult resultBattery = await arrivedDevice.CaptureDevice.GetBatteryLevelAsync();

            // You can easily modify this routine to report the BDADDR of the
            // connected device, too - arrivedDevice.CaptureDevice.GetBluetoothAddressAsync()
            // would do the trick.

            // And since CaptureHelper is given to you in the form of C# source
            // code, you can add your own helper functions and extend the
            // capabilities by studying the implementation of the existing functions.
            // If you decide to do this, you should create an extension of CaptureHelper
            // and implement the additions in a new source file. That way, when you
            // update the CaptureHelper SDK at a later date, you won't lose your
            // changes when the new CaptureHelper.cs file is written to your workstation.

            // Now show it all in a toast - be sure to run on the UI thread since this callback (and other CaptureHelper callbacks) do not come in on the UI thread!
            RunOnUiThread(() =>
            {
                Android.Widget.Toast.MakeText(this, "Arrival:\n" + FriendlyName + "\nBattery: " + resultBattery.Percentage, Android.Widget.ToastLength.Long).Show();
            });
        }
Пример #2
0
        public async void OnDeviceArrival(object sender, CaptureHelper.DeviceArgs arrivedDevice)
        {
            // first, get the BDADDR of the device so we can supply that to the GUI
            CaptureHelperDevice.BluetoothAddressResult resultBdAddr = await arrivedDevice.CaptureDevice.GetBluetoothAddressAsync("");

            // next get the battery level
            CaptureHelperDevice.BatteryLevelResult resultBattery = await arrivedDevice.CaptureDevice.GetBatteryLevelAsync();

            // now call the universal event
            ScannerSupport.OnDeviceArrival(arrivedDevice.CaptureDevice.GetDeviceInfo().Name, resultBattery.Percentage, resultBdAddr.BluetoothAddress);
        }
Пример #3
0
        // Callback for Capture, when a scanner connects

        async void OnDeviceArrival(object sender, CaptureHelper.DeviceArgs arrivedDevice)
        {
            Console.WriteLine("Start ScannerConnect()");
            if (CurrentDevice == null)
            {
                CurrentDevice = arrivedDevice.CaptureDevice;

                // Friendly name is available immediately
                InvokeOnMainThread(() =>
                {
                    scannerLabel.Text = CurrentDevice.GetDeviceInfo().Name;
                    Console.WriteLine("Device friendly name is: " + CurrentDevice.GetDeviceInfo().Name);
                });

                // Get the Bluetooth address of the connected device
                Console.WriteLine("Requesting the Bluetooth address.");
                CaptureHelperDevice.BluetoothAddressResult resultBdAddr = await CurrentDevice.GetBluetoothAddressAsync(".");

                // Put the BdAddr out there
                if (SktErrors.SKTSUCCESS(resultBdAddr.Result))
                {
                    Console.WriteLine("Got the Bluetooth address.");
                    InvokeOnMainThread(() =>
                    {
                        Console.WriteLine("BdAddr is: " + resultBdAddr.BluetoothAddress);
                        bdaddrLabel.Text = resultBdAddr.BluetoothAddress;
                    });
                }
                else
                {
                    Console.WriteLine("Error retrieving the Bluetooth address!");
                }

                // Get the current battery level
                Console.WriteLine("Requesting the battery level.");
                CaptureHelperDevice.BatteryLevelResult resultBattery = await CurrentDevice.GetBatteryLevelAsync();

                // Put the Battery Level out there
                if (SktErrors.SKTSUCCESS(resultBattery.Result))
                {
                    Console.WriteLine("Got the battery level.");
                    InvokeOnMainThread(() =>
                    {
                        battLevelLabel.Text = resultBattery.Percentage;
                    });
                }
                else
                {
                    Console.WriteLine("Error retrieving the battery level!");
                }

                var result = await CurrentDevice.SetDecodeActionAsync(false, false, false);

                if (!result.IsSuccessful())
                {
                    Console.WriteLine("SetDecodeAction() failed with " + result.Result + "!");
                }

                result = await capture.SetDataConfirmationModeAsync(ConfirmationMode.kApp);

                if (!result.IsSuccessful())
                {
                    Console.WriteLine("SetDataConfigurationMode() failed with " + result.Result + "!");
                }

                Connected = true;

                while (Connected)
                {
                    resultBattery = await CurrentDevice?.GetBatteryLevelAsync();

                    if (!SktErrors.SKTSUCCESS(resultBattery?.Result ?? -1))
                    {
                        Console.WriteLine("GetBatteryLevel() failed with " + resultBattery.Result + "!");
                    }
                    InvokeOnMainThread(() => battLevelLabel.Text = resultBattery.Percentage);
                    await Task.Delay(TimeSpan.FromSeconds(1));
                }

                // This section causing problems on some scanners

#if false
                CaptureHelperDevice.PowerStateResult powerState = await CurrentDevice.GetPowerStateAsync();

                if (SktErrors.SKTSUCCESS(powerState.Result))
                {
                    string strPowerState;

                    switch (powerState.State)
                    {
                    case CaptureHelper.PowerState.AC:
                        strPowerState = "On AC";
                        break;

                    case CaptureHelper.PowerState.Battery:
                        strPowerState = "On Battery";
                        break;

                    case CaptureHelper.PowerState.Cradle:
                        strPowerState = "In Cradle";
                        break;

                    default:
                        strPowerState = "Unknown";
                        break;
                    }
                    InvokeOnMainThread(() =>
                    {
                        powerStatusLabel.Text = strPowerState;
                    });
                }
                else
                {
                    Console.WriteLine("Error retrieving the power state!");
                }

                //See if we can register for live battery status updates

                //Should assign a handler for battery status changes up where
                //the capture openAsyc() happens.

                Console.WriteLine("Requesting notifications.");

                CaptureHelperDevice.NotificationsResult resultNotifications = await CurrentDevice.GetNotificationsAsync();

                Console.WriteLine("Got the notifications.");
                if (resultNotifications.IsSuccessful())
                {
                    if ((!resultNotifications.Notifications.BatteryLevel) || (!resultNotifications.Notifications.PowerState))
                    {
                        resultNotifications.Notifications.BatteryLevel = true;
                        resultNotifications.Notifications.PowerState   = true;
                        CaptureHelper.AsyncResult result = await CurrentDevice.SetNotificationsAsync(resultNotifications.Notifications);

                        if (!result.IsSuccessful())
                        {
                            Console.WriteLine("Unable to set the power notifications.");
                        }
                        else
                        {
                            CurrentDevice.DeviceBatteryLevel += OnDeviceBatteryLevel;
                            CurrentDevice.DevicePowerState   += OnDevicePowerState;
                        }
                    }
                    else
                    {
                        CurrentDevice.DeviceBatteryLevel += OnDeviceBatteryLevel;
                        CurrentDevice.DevicePowerState   += OnDevicePowerState;
                    }
                }
                else
                {
                    // display an error message...
                    Console.WriteLine("Device.GetNotificationsAsync did not work.");
                }
                //DoScannerConnect();
#endif
            }
            else
            {
                // Maybe a new scanner has arrived, but the simple act of
                // assigning the DeviceArrival handler will cause Capture
                // to notify us if a scanner is connected.

                Console.WriteLine("We already have a scanner connected!! Closing...");
                await arrivedDevice.CaptureDevice.CloseAsync();
            }

            Console.WriteLine("End ScannerConnect()");
        }