Exemple #1
0
        public SHRFIDBeaconService()
        {
            // Construct the Universal Bluetooth Beacon manager
            _provider      = new WindowsBluetoothPacketProvider();
            _beaconManager = new BeaconManager(_provider);

            // Subscribe to status change events of the provider
            _provider.WatcherStopped   += WatcherOnStopped;
            _beaconManager.BeaconAdded += BeaconManagerOnBeaconAdded;
            //
            _errBleMessage = "ok";
            _beaconManager.Start();
            _beaconManager.Stop();
        }
        //private struct BLUETOOTH_FIND_RADIO_PARAM
        //{
        //    internal UInt32 dwSize;
        //    internal void Initialize()
        //    {
        //        this.dwSize = (UInt32)Marshal.SizeOf(typeof(BLUETOOTH_FIND_RADIO_PARAM));
        //    }
        //}
        /// <summary>
        /// Closes an open object handle.
        /// </summary>
        /// <param name="handle">[In] A valid handle to an open object.</param>
        ///// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
        //[DllImport("Kernel32.dll", SetLastError = true)]
        //static extern bool CloseHandle(IntPtr handle);

        /// <summary>
        /// Finds the first bluetooth radio present in device manager
        /// </summary>
        /// <param name="pbtfrp">Pointer to a BLUETOOTH_FIND_RADIO_PARAMS structure</param>
        /// <param name="phRadio">Pointer to where the first enumerated radio handle will be returned. When no longer needed, this handle must be closed via CloseHandle.</param>
        /// <returns>In addition to the handle indicated by phRadio, calling this function will also create a HBLUETOOTH_RADIO_FIND handle for use with the BluetoothFindNextRadio function.
        /// When this handle is no longer needed, it must be closed via the BluetoothFindRadioClose.
        /// Returns NULL upon failure. Call the GetLastError function for more information on the error. The following table describe common errors:</returns>
        //[DllImport("irprops.cpl", SetLastError = true)]
        //static extern IntPtr BluetoothFindFirstRadio(ref BLUETOOTH_FIND_RADIO_PARAM pbtfrp, out IntPtr phRadio);

        //[StructLayout(LayoutKind.Sequential)]
        //private struct LE_SCAN_REQUEST
        //{
        //    internal int scanType;
        //    internal ushort scanInterval;
        //    internal ushort scanWindow;
        //}

        //[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Unicode)]
        //static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
        //ref LE_SCAN_REQUEST lpInBuffer, uint nInBufferSize,
        //IntPtr lpOutBuffer, uint nOutBufferSize,
        //out uint lpBytesReturned, IntPtr lpOverlapped);


        //public static void StartScanner(int scanType, ushort scanInterval, ushort scanWindow)
        //{

        //    //Action<object> action = (object obj) => {
        //    //    BLUETOOTH_FIND_RADIO_PARAM param = new BLUETOOTH_FIND_RADIO_PARAM();
        //    //    param.Initialize();
        //    //    IntPtr handle;
        //    //    BluetoothFindFirstRadio(ref param, out handle);
        //    //    uint outsize;
        //    //    LE_SCAN_REQUEST req = new LE_SCAN_REQUEST
        //    //    {
        //    //        scanType = scanType,
        //    //        scanInterval = scanInterval,
        //    //        scanWindow = scanWindow
        //    //    };
        //    //    DeviceIoControl(handle, 0x41118c, ref req, 8, IntPtr.Zero, 0, out outsize, IntPtr.Zero);
        //    //};
        //    //Task task = new Task(action, "nothing");
        //    //task.Start();
        //}


        public void StartBeaconManager()
        {
            //StartScanner(0, 29, 29);
            var provider = new WindowsBluetoothPacketProvider();

            _beaconManager              = new BeaconManager(provider);
            _beaconManager.BeaconAdded += StartBeaconMonitoringAsync;
            _beaconManager.Start();
            if (provider.WatcherStatus == BLEAdvertisementWatcherStatusCodes.Started)
            {
                Analytics.TrackEvent("StartBeaconManager", new Dictionary <string, string> {
                    { "Category", "Beacon:" + provider.WatcherStatus + ", " + BLEAdvertisementWatcherStatusCodes.Started },
                    { "Function", "StartBeaconManager" }
                });
                //System.Diagnostics.Debug.WriteLine("WatchingForBeacons");
            }
        }