private bool Callback(IntPtr pvParam, ref BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
        {
            switch (pAuthCallbackParams.authenticationMethod)
            {
            case BluetoothAuthenticationMethod.Passkey:
            case BluetoothAuthenticationMethod.NumericComparison:
                BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO nresponse = new BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO();
                nresponse.authMethod          = pAuthCallbackParams.authenticationMethod;
                nresponse.bthAddressRemote    = pAuthCallbackParams.deviceInfo.Address;
                nresponse.numericComp_passkey = pAuthCallbackParams.Numeric_Value_Passkey;
                return(NativeMethods.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref nresponse) == 0);

            case BluetoothAuthenticationMethod.Legacy:
                BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO response = new BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO();
                response.authMethod       = pAuthCallbackParams.authenticationMethod;
                response.bthAddressRemote = pAuthCallbackParams.deviceInfo.Address;
                response.pinInfo.pin      = new byte[16];
                System.Text.Encoding.ASCII.GetBytes(_pin).CopyTo(response.pinInfo.pin, 0);
                response.pinInfo.pinLength = _pin.Length;

                return(NativeMethods.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref response) == 0);
            }

            return(false);
        }
        private bool Callback(IntPtr pvParam, ref BLUETOOTH_AUTHENTICATION_CALLBACK_PARAMS pAuthCallbackParams)
        {
            try
            {
                switch (pAuthCallbackParams.authenticationMethod)
                {
                case BluetoothAuthenticationMethod.Passkey:
                case BluetoothAuthenticationMethod.NumericComparison:
                    BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO nresponse = new BLUETOOTH_AUTHENTICATE_RESPONSE__NUMERIC_COMPARISON_PASSKEY_INFO
                    {
                        authMethod          = pAuthCallbackParams.authenticationMethod,
                        bthAddressRemote    = pAuthCallbackParams.deviceInfo.Address,
                        numericComp_passkey = pAuthCallbackParams.Numeric_Value_Passkey
                    };

                    int result = NativeMethods.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref nresponse);
                    System.Diagnostics.Debug.WriteLine($"{result} {nresponse.negativeResponse}");
                    return(result == 0);

                case BluetoothAuthenticationMethod.Legacy:
                    BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO response = new BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO
                    {
                        authMethod       = pAuthCallbackParams.authenticationMethod,
                        bthAddressRemote = pAuthCallbackParams.deviceInfo.Address
                    };
                    response.pinInfo.pin = new byte[16];
                    System.Text.Encoding.ASCII.GetBytes(_pin).CopyTo(response.pinInfo.pin, 0);
                    response.pinInfo.pinLength = _pin.Length;

                    int authResult = NativeMethods.BluetoothSendAuthenticationResponseEx(IntPtr.Zero, ref response);
                    System.Diagnostics.Debug.WriteLine($"BluetoothSendAuthenticationResponseEx: {authResult}");
                    return(authResult == 0);
                }

                return(false);
            }
            finally
            {
                _waitHandle.Set();
                // after one call remove the registration
                BluetoothSecurity.RemoveRedundantAuthHandler(Address);
            }
        }
Exemple #3
0
 internal static extern int BluetoothSendAuthenticationResponseEx(IntPtr hRadio, ref BLUETOOTH_AUTHENTICATE_RESPONSE__PIN_INFO pauthResponse);