Example #1
0
        public bool PairDevice(BluetoothDeviceInfo device)
        {
            //device.Update();
            if (device.Authenticated)
            {
                return(true);
            }
            // Check if PIN has been stored
            if (_bluetoothConfiguration.StoredDevicePins.ContainsKey(device.DeviceAddress))
            {
                if (BluetoothSecurity.PairRequest(device.DeviceAddress, _bluetoothConfiguration.StoredDevicePins[device.DeviceAddress]))
                {
                    device.Update();
                    return(device.Authenticated);
                }
            }

            // loop through common PIN numbers to see if they pair
            foreach (string devicePin in CommonDevicePins)
            {
                var isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, devicePin);
                if (isPaired)
                {
                    _bluetoothConfiguration.StoredDevicePins[device.DeviceAddress] = devicePin;
                    StoreSettings();
                    break;
                }
            }

            device.Update();
            return(device.Authenticated);
        }
Example #2
0
 private void button2_Click(object sender, EventArgs e)
 {
     if (dataGridView1.Rows.Count == 0)
     {
         MessageBox.Show("Nie skanowano w poszukiwaniu urządzeń!", "BŁĄD!");
         return;
     }
     if (selectedDevice[0] == "<NULL>")
     {
         MessageBox.Show("Brak urzadzeń do sparowania!", "BŁĄD!");
         return;
     }
     if (isPaired)
     {
         BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
         if (paired[0].DeviceName == selectedDevice[1])
         {
             return;
         }
         BluetoothSecurity.RemoveDevice(paired[0].DeviceAddress);
     }
     selected.Update();
     selected.Refresh();
     selected.SetServiceState(BluetoothService.ObexObjectPush, true);
     //localClient.Connect(new BluetoothEndPoint(selected.DeviceAddress, BluetoothService.ObexFileTransfer));
     BluetoothSecurity.PairRequest(selected.DeviceAddress, null);
     isPaired = true;
 }
        private void sendFileButton_Click(object sender, EventArgs e)
        {
            if (device == null)
            {
                MessageBox.Show("Nie wybrano urządzenia!!!");
                return;
            }

            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                device.Update();
                device.Refresh();
                device.SetServiceState(BluetoothService.ObexObjectPush, true);

                var file    = ofd.FileName;
                var uri     = new Uri("obex://" + device.DeviceAddress + "/" + file);
                var request = new ObexWebRequest(uri);
                request.ReadFile(file);

                ObexWebResponse response = null;
                try
                {
                    response = (ObexWebResponse)request.GetResponse();
                    MessageBox.Show(response.StatusCode.ToString());
                    response.Close();
                }catch (Exception)
                {
                    // check response.StatusCode
                    MessageBox.Show("Urządzenie nie odpowiedziło");
                }
            }
        }
Example #4
0
        private void buttonPair_Click(object sender, EventArgs e)
        {
            //wybieramy urządzenie z listy
            foreach (BluetoothDeviceInfo device in devices)
            {
                if (device.DeviceName.Equals(listBoxDevices.SelectedItem))
                {
                    deviceToPair = device;
                }
            }

            //nawiżaywanie połączneia z wybranym urządzeniem
            deviceToPair.Update();
            deviceToPair.Refresh();
            deviceToPair.SetServiceState(BluetoothService.ObexObjectPush, true);

            //wysłanie podłączenia
            string pin = "1234";

            isPaired = BluetoothSecurity.PairRequest(deviceToPair.DeviceAddress, pin);

            //wypisanie wszystkich sparowanych urządzeń
            if (isPaired)
            {
                connected.Add(deviceToPair);
                listBoxConnected.Items.Clear();
                foreach (var device in connected)
                {
                    listBoxConnected.Items.Add(device.DeviceName);
                }
            }
        }
Example #5
0
 private void button_Click(object sender, EventArgs e)
 {
     if (selectedDevice != null)
     {
         selectedDevice.Update();
         selectedDevice.Refresh();
         if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, "0000"))
         {
             bluetoothClient.BeginConnect(selectedDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), selectedDevice);
         }
     }
 }
Example #6
0
        private void PairWithClient()
        {
            //sprawdzamy ktory wybrany z list boxa
            foreach (var client in clients)
            {
                if (client.DeviceName == (string)listBox1.SelectedItem)
                {
                    deviceToPair = client;
                }
            }

            if (deviceToPair != null)
            {
                textBox1.Text += ("Urzadzenie, z ktorym sie parujemy:") + Environment.NewLine;
                var blueToothInfo =
                    string.Format(
                        "- DeviceName: {0}{1}  Connected: {2}{1}  Address: {3}{1}  Last seen: {4}{1}  Last used: {5}{1}",
                        deviceToPair.DeviceName, Environment.NewLine, deviceToPair.Connected, deviceToPair.DeviceAddress, deviceToPair.LastSeen,
                        deviceToPair.LastUsed);
                blueToothInfo += string.Format("  Class of device{0}   Device: {1}{0}   Major Device: {2}{0}   Service: {3}",
                                               Environment.NewLine, deviceToPair.ClassOfDevice.Device, deviceToPair.ClassOfDevice.MajorDevice, deviceToPair.ClassOfDevice.Service);
                textBox1.Text += (blueToothInfo) + Environment.NewLine;

                deviceToPair.Update();
                deviceToPair.Refresh();
                deviceToPair.SetServiceState(BluetoothService.ObexObjectPush, true);
                bool isPaired = BluetoothSecurity.PairRequest(deviceToPair.DeviceAddress, DEVICE_PIN);
                if (isPaired)
                {
                    textBox1.Text += ("Urzadzenie sparowane!") + Environment.NewLine;
                    listBox2.Items.Add(deviceToPair.DeviceName);

                    czySparowano = true;

                    // jezeli sparowano to aktywujemy tryb nasluchu plikow

                    Thread thread = new Thread(ReceiveFile);
                    thread.Priority     = ThreadPriority.Normal;
                    thread.IsBackground = true;
                    thread.Start();
                }
                else
                {
                    textBox1.Text += ("Urzadzenie niesparowane!") + Environment.NewLine;
                }
            }
            else
            {
                MessageBox.Show("Nie wybrano urzadzenia do sparowania!");
            }
            button2.Enabled = true;
        }
Example #7
0
        public static bool PairDevice(BluetoothDeviceInfo device)
        {
            if (device.Authenticated)
            {
                return(true);
            }
            // loop through common PIN numbers to see if they pair
            foreach (var devicePin in CommonDevicePins)
            {
                var isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, devicePin);
                if (isPaired)
                {
                    break;
                }
            }

            device.Update();
            return(device.Authenticated);
        }
Example #8
0
        // Funkcja która łączy adapter z urządzeniem bluetooth
        public void Connect(ListBox devices)
        {
            // Pobranie informacji o wybranym urządzeniu
            BluetoothDeviceInfo device = info[devices.SelectedIndex];

            // Aktualizacja nazwy urządzenia
            device.Update();
            // Odświeżenie informacji o urządzeniu
            device.Refresh();
            // Przygotowanie urządzenia do wysyłania danych binarnych z wykorzystaniem protokołu OBEX
            device.SetServiceState(BluetoothService.ObexObjectPush, true);

            EventHandler <BluetoothWin32AuthenticationEventArgs> handler = new EventHandler <BluetoothWin32AuthenticationEventArgs>(HandleRequests);
            BluetoothWin32Authentication auth = new BluetoothWin32Authentication(handler);

            // Jeżeli urządzenie nie jest sparowane zostaje wysłana prośba o sparowanie, musi zostać ona potwierdzona podaniem pinu - 123456
            if (!device.Authenticated)
            {
                BluetoothSecurity.PairRequest(device.DeviceAddress, "123456");
            }
        }
Example #9
0
        private void PairWithDevice()
        {
            foreach (var device in devices)
            {
                if (device.DeviceName == (string)listBoxDevices.SelectedItem)
                {
                    deviceToPair = device;
                }
            }

            if (deviceToPair == null)
            {
                textBoxConsole.Text += "Nie wybrano urządzenia do sparowania!\r\n";
            }
            else
            {
                textBoxConsole.Text += "Parowanie z urządzeniem " + (string)deviceToPair.DeviceName + "...\r\n";
                deviceToPair.Update();
                deviceToPair.Refresh();
                deviceToPair.SetServiceState(BluetoothService.ObexObjectPush, true);
                isPaired = BluetoothSecurity.PairRequest(deviceToPair.DeviceAddress, DEVICE_PIN);
                if (isPaired)
                {
                    textBoxConsole.Text += "Urządzenie sparowane pomyślnie.\r\n";
                    listBoxConnected.Items.Add(deviceToPair.DeviceName);
                    buttonPairWithDevice.Enabled = false;
                    buttonUnpair.Enabled         = true;

                    Thread receiveFiles = new Thread(ReceiveFiles);
                    receiveFiles.Priority     = ThreadPriority.Highest;
                    receiveFiles.IsBackground = true;
                    receiveFiles.Start();
                }
                else
                {
                    textBoxConsole.Text += "Nie udało się sparować urządzenia.\r\n";
                }
            }
        }
Example #10
0
        // Funkcja ta wysyła plik do urządzenia bluetooth
        public void SendFile(ListBox devices)
        {
            // Pobranie informacji o wybranym urządzeniu
            BluetoothDeviceInfo device = info[devices.SelectedIndex];

            // Aktualizacja nazwy urządzenia
            device.Update();
            // Odświeżenie informacji o urządzeniu
            device.Refresh();
            // Przygotowanie urządzenia do wysyłania danych binarnych z wykorzystaniem protokołu OBEX
            device.SetServiceState(BluetoothService.ObexObjectPush, true);

            OpenFileDialog window = new OpenFileDialog();

            window.ShowDialog(); //wyświetlenie okienka do wyboru pliku
            var file = window.FileName;
            // Ustalenie ścieżki pliku
            var uri = new Uri("obex://" + device.DeviceAddress + "/" + file);
            // Stworzenie zapytania ze ścieżką pliku
            var request = new ObexWebRequest(uri);

            // Przeczytanie zawartości pliku
            request.ReadFile(file);
            // Wysłanie zapytania oraz pobranie odpowiedzi
            var response = request.GetResponse() as ObexWebResponse;

            // Wyświetlenie komunikatu czy przesyłanie pliku przebiegło pomyślnie
            if (response.StatusCode.ToString() == "OK, Final")
            {
                MessageBox.Show("Plik przesłany pomyślnie");
            }
            else
            {
                MessageBox.Show("Błąd przesyłania pliku");
            }
            // Zakończenie wymiany informacji z urządzeniem
            response.Close();
        }
        public static bool PairDevice(BluetoothDeviceInfo device)
        {
            if (device.Authenticated) return true;
            // loop through common PIN numbers to see if they pair
            foreach (var devicePin in CommonDevicePins)
            {
                var isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, devicePin);
                if (isPaired) break;
            }

            device.Update();
            return device.Authenticated;
        }