Exemplo n.º 1
0
        public static void Init()
        {
            if (adapter != null)
            {
                return;
            }

            adapter                     = BluetoothAdapter.DefaultAdapter;
            gattCallback                = new GattCallback();
            gattCallback.Connecting    += OnDeviceConnecting;
            gattCallback.Connected     += OnDeviceConnected;
            gattCallback.Disconnecting += OnDeviceDisconnecting;
            gattCallback.Disconnected  += OnDeviceDisconencted;

            if (adapter == null)
            {
                Console.WriteLine("Device does not support Bluetooth");
                return;
            }

            if (!adapter.IsEnabled)
            {
                Console.WriteLine("Bluetooth is not enabled");
                return;
            }

            Scanning?.Invoke(null, EventArgs.Empty);

            ICollection <BluetoothDevice> pairedDevices = adapter.BondedDevices;

            if (pairedDevices.Count > 0)
            {
                // There are paired devices. Get the name and address of each paired device.
                foreach (BluetoothDevice d in pairedDevices)
                {
                    string deviceName            = d.Name;
                    string deviceHardwareAddress = d.Address;

                    Console.WriteLine(deviceName + " - " + deviceHardwareAddress);

                    if (deviceHardwareAddress == "20:16:02:30:52:56" || deviceName == "aFloat")
                    {
                        Device = d;

                        Found?.Invoke(null, EventArgs.Empty);

                        Console.WriteLine("Device found. BondState = {0}", Device.BondState);
                    }
                }
            }
            else
            {
                Console.WriteLine("No paired devices");
            }

            if (Device == null)
            {
                NotFound?.Invoke(null, EventArgs.Empty);

                return;
            }
        }
Exemplo n.º 2
0
 private void OnScanning(string dir)
 {
     Scanning?.Invoke(this, new ScanEventArgs(dir));
 }
Exemplo n.º 3
0
 private void OnScanning(string webPage)
 {
     Scanning?.Invoke(webPage);
 }