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);
        }
Exemple #2
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()");
        }