Pairing device that require number comparison

Topics: Bluetooth - Microsoft, Bluetooth - Widcomm/Broadcom
Jan 28, 2012 at 11:07 AM

Hi!

I'm an italian user and I'm developing a WPF application that uses 32feet.Net for Bluetooth connection with smartphones. My application searches for bluetooth devices using BluetoothClient.DiscoverDevices(). After that the user must select the device he wants to connect with and then the application try to authenticate with the device, in order to send it a photo.  

I try to authenticate using BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, myPin), setting myPin as "1234", and it work with some devices, such as Nokia or LG phones, but most of devices, like IPhone, Android, Blackbarry etc., require a number comparison an I don't know how to do it.

So my question is: how can my application request an authentication to whatever device (blackberry, nokia, iphone) and intercept the sequence number displayed on device's screen, in order to authenticate my application to it? 

Sorry for my english, I hope that post is clear to understand.

Thank you!

Developer
Jan 29, 2012 at 6:14 PM

What you are seeing is Bliuetooth v2.1's Secure Simple Pairing (SSP).  You need to create an instance of BluetoothWin32Authentication and handle the callback method.  If you simply want to accept all authentication attempts then just set e.Confirm=true and return.  That's of course not the most safe so you should consider asking for user confirmation etc...

See e.g. http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication&referringTitle=Bluetooth%20Security

Jan 30, 2012 at 8:25 AM
Edited Jan 30, 2012 at 9:07 AM

Thank you for your answer. 

I sensed that the problem was SSP protocol. 

Can you tell me if my code is right now? Unfortunately I can test it only with Nokia Smartphone and Samsung Galaxy Note, which don't use SSP protocol and allow my application to send photo without authentication! 

In this code I tried to insert something like 

bc.Connect(new BluetoothEndPoint(selectedDevice.DeviceAddress, BluetoothService.ObexFileTransfer));

 where bc in a BluetoothClient, but with GalaxyNote it raise a "System.Net.Sockets.SocketException" so I remove it.

{
BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] devicesInfo =    bc.DiscoverDevices();
BluetoothDeviceInfo selectedDevice;

 ... in this piece of code I select my device ....
          
     if (!selectedDevice.Authenticated)
     {
         BluetoothWin32Authentication oBluetoothWin32Authentication = new BluetoothWin32Authentication(Win32AuthCallbackHandler);

         try
          {                            

                            string gPath = AppDomain.CurrentDomain.BaseDirectory;

                            string filename = System.IO.Path.GetFileName(gPath + @"Media\fotoLogo.jpg");

                            Uri u = new Uri("obex-push://" + selectedDevice.DeviceAddress.ToString() + "/" + filename);
                            ObexWebRequest owr = new ObexWebRequest(u);
                            owr.ReadFile(gPath + @"Media\" + filename);
                            ObexWebResponse response = (ObexWebResponse)owr.GetResponse();
                            response.Close();

                        }
                        catch (Exception e1)
                        {
                          
                            Console.WriteLine("Exception: {0}", e1.GetType());
                            Console.WriteLine("Exception message: {0}",e1.Message);
                        }
                    }
                    else 
                    {
                        Console.WriteLine("Just authenticated");
                    }
                    break;
                }
            }

            bc.Close();
	}
	
        /* and then authentication handler */
        void Win32AuthCallbackHandler(object sender, BluetoothWin32AuthenticationEventArgs e)
        {
            try{
                if (e.AuthenticationMethod == BluetoothAuthenticationMethod.NumericComparison)
                {
                    Console.WriteLine("Numeric comparison");
                    string number = e.NumberOrPasskeyAsString;
                    string messageBoxText = "Device" + e.Device.DeviceName + " is waiting for authentication.\n" + "Confirm that pin is: '" + number + " ?";
                    MessageBoxButton button = MessageBoxButton.YesNo;
                    MessageBoxImage icon = MessageBoxImage.Warning;
                    MessageBoxResult result = MessageBox.Show(messageBoxText, "", button, icon);

                    // Process message box results
                    switch (result)
                    {
                        case MessageBoxResult.Yes:
                    
                            e.Confirm = true;
                            break;
                        case MessageBoxResult.No:
                            e.Confirm = false;
                            break;
                    }
                }
                else
                {
                    if (e.AuthenticationMethod == BluetoothAuthenticationMethod.Passkey)
                    {
                        Console.WriteLine("Passkey");		
                        InputDialog input = new InputDialog(sender, e);
                    }
                    else
                    {
                        if (e.JustWorksNumericComparison == true)
                        {
                            Console.WriteLine("JustWorksNumericComparison");
                            string messageBoxText = "Allow device " + e.Device.DeviceName + " to connect?";
                            MessageBoxButton button = MessageBoxButton.YesNo;
                            MessageBoxImage icon = MessageBoxImage.Warning;
                            MessageBoxResult result = MessageBox.Show(messageBoxText, "", button, icon);

                            // Process message box results
                            switch (result)
                            {
                                case MessageBoxResult.Yes:
                                    e.Confirm = true;
                                    break;
                                case MessageBoxResult.No:
                                    e.Confirm = false;
                                    break;
                            }
                        }
                        else
                            if (e.AuthenticationMethod == BluetoothAuthenticationMethod.Legacy)
                            {
                                Console.WriteLine("Legacy");
                                e.Pin = "1234";
                            }
                    }
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine("Eccezione: {0}", e1.GetType());             
            }
        }
       

Thanks a lot!

Sorry but in my reply code is split in 3 pieces, the second and the third one are single method.