Beispiel #1
0
        private TimeSpan SCAN_INTERVAL_PRESENT = new TimeSpan(0, 30, 1); // Keep a multiple of this slightly longer than AWAY_TIMEOUT to prevent evaulating 1sec before the timeout.

        #endregion Fields

        #region Constructors

        public BluetoothService(ServiceCreationInfo info)
            : base("bluetooth", info)
        {
            mBluetooth = new BluetoothHelper();

            // Refresh available devices
            mBluetooth.RefreshDevices();
            mDeviceNames = mBluetooth.GetDevices();
            Log.Info("Bluetooth devices: {0}", mDeviceNames);

            // Set up scan thread.
            mEvents = new EventWaitHandle[] { mStopEvent, mDeviceEvent };
            mThread = new Thread(_searchThread);
            mThread.Start();
        }
Beispiel #2
0
        public BluetoothService(ServiceCreationInfo info)
            : base("bluetooth", info)
        {
            mBluetooth = new BluetoothHelper();

            // Refresh available devices
            mBluetooth.RefreshDevices();
            mDeviceNames = mBluetooth.GetDevices();
            if (mDeviceNames != null)
            {
                Log.Info("Bluetooth devices: {0}", mDeviceNames);
            }

            // Set up scan thread.
            mEvents = new EventWaitHandle[] { mStopEvent, mDeviceEvent };
            mThread = new Thread(_searchThread);
            mThread.Start();
        }
Beispiel #3
0
        private void _searchThread()
        {
            Log.Info("Starting Bluetooth search thread");

            // Avoid the initial lock contestion for mDevices
            Thread.Sleep(5000);

            DateTime intensiveScanTime = DateTime.Now;

            while (mRunning)
            {
                try
                {
                    mBluetooth.RefreshDevices();

                    DateTime currTime = DateTime.Now;
                    DateTime nextScan = currTime + new TimeSpan(10, 0, 0);  // Just some high dummy value to diff against

                    if (mIntensiveScan)
                    {
                        Log.Debug("Starting intensive scan");
                        mIntensiveScan    = false;
                        intensiveScanTime = currTime + INTENSIVE_SCAN_DURATION;
                    }

                    lock (mDevices)
                    {
                        foreach (var info in mDevices)
                        {
                            string          btName = info.Device.BtName;
                            BluetoothDevice device = info.Device;

                            // Scan for device
                            mBluetooth.UpdateDevice(btName);
                            bool inRange = mBluetooth.InRange(btName);

                            // Update last time device was seen.
                            if (inRange)
                            {
                                info.LastTime = currTime;
                            }

                            // Initial notify to set up device state properly, but don't notify it's listeners
                            if (info.FirstScan)
                            {
                                info.FirstScan = false;

                                // Notify device of it's initial state.
                                if (inRange)
                                {
                                    Log.Info("Bluetooth: device {0} initially present", btName);
                                }
                                else
                                {
                                    Log.Info("Bluetooth: device {0} initially away", btName);
                                }
                                device.OnStatusUpdate(inRange, true);
                            }
                            else if (device.InRange && !inRange && (currTime - info.LastTime) >= AWAY_TIMEOUT)
                            {
                                // Lost device
                                Log.Debug("Lost device: {0}", btName);
                                info.Device.OnStatusUpdate(inRange);
                            }
                            else if (!device.InRange && inRange)
                            {
                                // Found device
                                Log.Debug("Found device: {0}", btName);
                                info.Device.OnStatusUpdate(inRange);
                            }

                            TimeSpan delayTime;
                            // Delay dependent on current state
                            if (device.InRange)
                            {
                                delayTime = SCAN_INTERVAL_PRESENT;
                            }
                            else
                            {
                                delayTime = SCAN_INTERVAL_AWAY;
                            }
                            info.NextScan = currTime + delayTime;

                            if (info.NextScan < nextScan)
                            {
                                nextScan = info.NextScan;
                            }
                        }
                    }

                    // Running intensive scan so don't sleep just yet.
                    if (currTime < intensiveScanTime)
                    {
                        continue;
                    }

                    EventWaitHandle.WaitAny(mEvents, nextScan - currTime);
                }
                catch (Exception e)
                {
                    Logging.Log.Exception(e);
                }
            }
        }