Ejemplo n.º 1
0
        public ScanResponseEventArgs Execute()
        {
            // Send discover command
            byte[] scanParams   = Ble.Lib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1);
            byte[] scanDiscover = Ble.Lib.BLECommandGAPDiscover(1);

            ScanResponseEventHandler handler = this.FindService;

            Ble.Lib.BLEEventGAPScanResponse += handler;

            Ble.SendCommand(this.Port, scanParams);
            Ble.SendCommand(this.Port, scanDiscover);

            this.WaitEvent(() => _scanResponse != null);

            Ble.Lib.BLEEventGAPScanResponse -= handler;             // not needed anymore
            return(_scanResponse);
        }
Ejemplo n.º 2
0
        public override string[] GetAvailableDevices(int scanTime, bool detectBlueGiga)
        {
            List <string>  names          = new List <string>();
            bool           commandStarted = false;
            AutoResetEvent resp           = new AutoResetEvent(false);

            discoveredDevices.Clear();

            if (detectBlueGiga)
            {
                if (/*port == null ||*/ !getBlueGigaStatus())                 //only try to find a BlueGiga dongle if one is not already assigned.
                {
                    if (!ConnectToBluegiga())
                    {
                        return(names.ToArray());
                    }
                }
            }


            DiscoverEventHandler discoverResp = delegate(object sender, DiscoverEventArgs e)
            {
                if (e.result == 0)
                {
                    commandStarted = true;
                }
                resp.Set();
            };

            ScanResponseEventHandler deviceFound = delegate(object sender, ScanResponseEventArgs e)
            {
                if (e.data.Length < 8)
                {
                    return;
                }

                string name = Encoding.UTF8.GetString(e.data, e.data.Length - 8, 8);

                if (!name.Contains("TAPPY"))
                {
                    return;
                }

                foreach (BluetoothDevice device in discoveredDevices)
                {
                    if (device.Name.Equals(name))
                    {
                        return;
                    }
                }

                discoveredDevices.Add(new BluetoothDevice(e.sender, e.bond, e.rssi, name));
            };

            bluetooth.BLEResponseGAPDiscover += discoverResp;

            bluetooth.SendCommand(port, bluetooth.BLECommandGAPDiscover(2));

            resp.WaitOne(200);

            bluetooth.BLEEventGAPScanResponse += deviceFound;
            bluetooth.BLEResponseGAPDiscover  -= discoverResp;

            if (!commandStarted)
            {
                return(names.ToArray());
            }

            Thread.Sleep(scanTime);
            bluetooth.SendCommand(port, bluetooth.BLECommandGAPEndProcedure());

            bluetooth.BLEEventGAPScanResponse -= deviceFound;

            foreach (BluetoothDevice device in this.discoveredDevices)
            {
                names.Add(device.Name);
            }

            return(names.ToArray());
        }