none
bluetooth pairing issues with auto microsoft code RRS feed

  • Question

  • hello experts,

    I am new using developing for Bluetooth using C#, I've have read a lot of information about how Bluetooth works with 32feet.net, however, my big issue is that when I try to pair my PC with my mobile (android) always the PC Bluetooth sent a 6 random code which does not let me see my own code and which I need to use. 

    Does anybody knows how can I change or eliminate that auto pin-code?

    here is my code 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using InTheHand;
    using InTheHand.Net;
    using InTheHand.Net.Ports;
    using InTheHand.Net.Sockets;
    using InTheHand.Net.Bluetooth;
    using System.Threading;
    
    
    namespace bluetooth_correccion
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            BluetoothClient pc = new BluetoothClient();
            BluetoothDeviceInfo[] info = null;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                if (!BluetoothRadio.IsSupported)
                {
                    MessageBox.Show("no hay bluetooth");
                }
                if (BluetoothRadio.PrimaryRadio.Mode == RadioMode.PowerOff)
                {//descubrir como apagar y encender el bluetooth
                    BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
                    MessageBox.Show(BluetoothRadio.PrimaryRadio.Name.ToString());
                    MessageBox.Show(BluetoothRadio.PrimaryRadio.Mode.ToString());
                }
                else
                {
                    MessageBox.Show(BluetoothRadio.PrimaryRadio.Name.ToString());
                    MessageBox.Show(BluetoothRadio.PrimaryRadio.Mode.ToString());
                }
    
                info = pc.DiscoverDevices(10);
                foreach (BluetoothDeviceInfo device in info)
                {
                    listBox1.Items.Add(device.DeviceName + " - " + device.DeviceAddress);
                    device.Update();
                    device.Refresh();
                    device.SetServiceState(BluetoothService.ObexObjectPush, true);
                }
            }
    
            BluetoothDeviceInfo deviceinfo;
    
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                deviceinfo = info.ElementAt(listBox1.SelectedIndex);
                MessageBox.Show(deviceinfo.DeviceName);
                if (!deviceinfo.Authenticated)
                {
                    if (!BluetoothSecurity.PairRequest(deviceinfo.DeviceAddress, "0000"))
                    {
                        MessageBox.Show("error de autenticacion");
                    }
                    else
                    {
                        MessageBox.Show("autenticado");
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var bluetoothClient = new BluetoothClient();
                var devices = bluetoothClient.DiscoverDevices();
                foreach(var device in devices)
                {
                    listBox2.Items.Add(device.DeviceName);
                    listBox2.Items.Add(device.Connected);
                    listBox2.Items.Add(device.DeviceAddress);
                    listBox2.Items.Add(device.LastSeen);
                    listBox2.Items.Add(device.LastUsed);
                }
            }
        }
    }
    please help, thank you!!!
    Friday, September 21, 2018 3:15 PM

Answers

  • Hi RALD_85,

    I am glad to know that you resolve the issue and thank for sharing the solution to us, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RALD_85 Friday, September 28, 2018 5:35 PM
    Friday, September 28, 2018 6:12 AM
    Moderator

All replies

  • Hi RALD_85,

    Do you mean that PC will auto send a 6 random code to mobile after paired? 

    In addition, the following link about Bluetooth dial with 32feet.net and c# for your reference.

    https://stackoverflow.com/questions/5960208/bluetooth-dial-with-32feet-net-and-c-sharp

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 27, 2018 9:31 AM
    Moderator
  • Hi, 

    thanks for you reply, after searching and searching and almost get lost... I found a solution in just one line

    private void BtConectar_Click(object sender, EventArgs e)
            {
                BluetoothWin32Authentication bluetoothWin32 = new BluetoothWin32Authentication(codigo); // this line
    
            }
    
    private void codigo(object sender, BluetoothWin32AuthenticationEventArgs e)
            {
                if (!obexListener.Authenticate)
                {
                    switch (MessageBox.Show("PIN CODE" + e.NumberOrPasskeyAsString, "EMPAREJAMINETO", MessageBoxButtons.YesNo))
                    {
                        case DialogResult.Yes:
                            e.Confirm = true;
                            break;
                        case DialogResult.No:
                            e.Confirm = false;
                            break;
                    }
                }
                else
                {
                    switch (MessageBox.Show("PIN CODE" + e.NumberOrPasskeyAsString, "EMPAREJAMINETO", MessageBoxButtons.YesNo))
                    {
                        case DialogResult.Yes:
                            e.Confirm = true;
                            MessageBox.Show("Bienvenido Nuevamente!");
                            break;
                        case DialogResult.No:
                            e.Confirm = false;
                            break;
                    }
                }
            }

    with this code the system shows the 6 random number in both devices and can be paired with no issue

    thanks.

    Thursday, September 27, 2018 5:12 PM
  • Hi RALD_85,

    I am glad to know that you resolve the issue and thank for sharing the solution to us, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by RALD_85 Friday, September 28, 2018 5:35 PM
    Friday, September 28, 2018 6:12 AM
    Moderator