Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

ASP.NET
<pre><%@ 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>



Here are the codes below:

And this is my C# code.
C#
<pre>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.

What I have tried:

I referred to this site: Bluetooth GATT Client - UWP applications | Microsoft Docs[^] ,
to no avail. It only works for the WIndows form version.
Posted
Updated 15-Nov-21 19:41pm
v2
Comments
CHill60 16-Nov-21 6:30am    
Which line is giving that exception?
Master PC 16-Nov-21 21:49pm    
// Start the watcher.
deviceWatcher.Start();
} <<---- The offending line
Richard Deeming 17-Nov-21 5:29am    
NB: Your code is running on the server. You will be attempting to monitor devices connected to the server. And your event handlers will fire on an instance of your page class which has already sent its response to the user and been discarded; it will never be able to display anything to the user.

If you're trying to monitor devices connected to the server, you'll need to find a different approach.

If you're trying to monitor devices connected to the client, you can't. Server-side code has no access to client-side hardware. And client-side (Javascript) code has extremely limited access to the client's hardware.
Master PC 17-Nov-21 21:38pm    
Ok. So you mean that if I just want to read the data from a sensor device, and it's literraly impossible to do on ASP.NET, instead should do JavaScript instead?
Richard Deeming 18-Nov-21 3:39am    
If you want to monitor devices connected to the client computer, you can't do it from server-side code.

You may be able to use the Bluetooth API from Javascript, if the browser supports it and the user enables it:
Web Bluetooth API - Web APIs | MDN[^]

But it's currently an experimental technology, so browser support is limited, and it may change significantly in the future, or even be scrapped.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900