The Parameter is incorrect.

chairmanPC

Active member
Joined
Apr 19, 2021
Messages
38
Programming Experience
10+
The WIndows .NET Framework form version of the BLE Watcher is running fine, so I am making a ASP.NET version of the BLE Watcher, but I kept getting this error:

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)).

Note that the code in Form_Load is straight from the Windows Form version of the Watcher. In fact, all of the code came from this site: Bluetooth GATT Client - UWP applications.

Here are the codes below:

Webpage file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="BLE_Device_Watcher.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <div>
        </div>
    </form>
</body>
</html>

And this is my C# code.

WebForm1.aspx.cs:
using System;

namespace BLE_Device_Watcher
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //Device Data
        BluetoothLEDevice bluetoothLeDevice;
        GattDeviceServicesResult result;
        static DeviceInformation device = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            

            // Query for extra properties you want returned
            string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };

            DeviceWatcher deviceWatcher =
                        DeviceInformation.CreateWatcher(
                                BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                                requestedProperties,
                                DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            // Added, Updated and Removed are required to get all nearby devices
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;

            // EnumerationCompleted and Stopped are optional to implement.
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start the watcher.
            deviceWatcher.Start();
        }                <<---- The offending line

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        //Other
        private static void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        {
            if (args.Name == "myDevice")
            {
                device = args;
            }
        }

        private static void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
        {
            //throw new NotImplementedException();
        }

        private static void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        {
            //throw new NotImplementedException();
        }
    }
}

Did I do something wrong here?

It's a ASP.NET project (non-Core), running .NET Framework 4.7.2.
 
Solution
I haven't looked closely at your code because the first question is whether what you're trying to do makes any sense at all. That Page_Load method gets executed on the web server, not on the client machine. The client machine only sees the HTML and JavaScript code sent to it by your application running on the web server. There's absolutely nothing in that HTML or JavaScript that has anything to do with Bluetooth devices on the client and, even if there was, a page loaded into a browser will generally not have sufficient privileges to access such information. All you can do is get information about Bluetooth devices connected to the web server, of which there will almost certainly not be any.

You need to do a bit of research to...
I haven't looked closely at your code because the first question is whether what you're trying to do makes any sense at all. That Page_Load method gets executed on the web server, not on the client machine. The client machine only sees the HTML and JavaScript code sent to it by your application running on the web server. There's absolutely nothing in that HTML or JavaScript that has anything to do with Bluetooth devices on the client and, even if there was, a page loaded into a browser will generally not have sufficient privileges to access such information. All you can do is get information about Bluetooth devices connected to the web server, of which there will almost certainly not be any.

You need to do a bit of research to learn the differences between local applications and web applications. You might be able to do something like what you want if you create a PWA, but certainly not with a regular web app. Think about it: would you want any old web site to be able to access local resources on your computer like that?
 
Solution
Back
Top Bottom