How to connect the WiiMote internally in a C# app? - c#

I want to connect a single wiiMote to my app using WiiMoteLib, but I want the connection done internally ie. the user has only to run the app (and should have no need to connect the wiiMote with windows wizard or bluesoleil). The app is in C# and the language cannot be changed.
Thanks!

Does 32feet.NET do the job?
BluetoothAddress addr = ... address from discovery or known address...
var dev = new BluetoothDeviceInfo(addr);
dev.SetServiceState(BluetoothService.HumanInterfaceDevice, true); // this line!
You may also need to handle pairing, perhaps using BluetoothWin32Authentication and handling its callback, see http://32feet.codeplex.com/wikipage?title=Bluetooth%20Security and http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication

There is an example of how to do this here using the Microsoft Bluetooth API and the HID API:
http://www.richlynch.com/code/wiipair
It works quire well.
Its in C++, but its pretty easy to convert to C#.

Related

Is it possible to connect to a BTLE device in a WPF application?

So I am trying to connect to a Polar H7 Heart Rate Monitor, and I need to use a WPF application to do it. I'm using Windows 10.
Now I have done this with a UWP application already and it works perfectly, but I'd like to use WPF (if it is possible) instead to do the same.
I found this post on how to use Windows 10 APIs in WPF/winforms/etc and thought perfect, that's what I need. I successfully added the Bluetooth API to my WPF project and threw in the code that was working for my UWP project, but it doesn't work.
Here's a snippet of my code up to where it stops working:
DeviceInformation _devicePolar = null;
string StatusInformation;
string StatusInformation2;
var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.HeartRate));
foreach(var d in devices)
{
Debug.WriteLine(d.Name + " " + d.Id);
}
if (null == devices || devices.Count <= 0) return;
foreach (var device in devices.Where(device => device.Name == "Polar H7 DCB16C16"))
{
_devicePolar = device;
StatusInformation = string.Format("Found {0}", _devicePolar.Name);
break;
}
if (_devicePolar == null) return;
var service = await GattDeviceService.FromIdAsync(_devicePolar.Id);
if (service == null) return;
Now, this call
await GattDeviceService.FromIdAsync(_devicePolar.Id)
is returning null. I was actually having the same problem when I made the UWP application and I found out it was because I forgot to throw in this
<Capabilities>
<Capability Name="internetClient" />
<DeviceCapability Name="bluetooth" />
</Capabilities>
Into my manifest file. Once I put that in (on my UWP application) it worked fine. But there seems to be no equivalent place for this piece in a WPF application. Now going back to the blog post on how to add Win 10 libraries to WPF, this bit seems to be telling me that was the case all along:
The second set of APIs that you can’t use are ones that depend on an app’s package identity. UWP apps have package identities while PC software does not. Package identity information can be found in the app manifest file.
And looking further into the Microsoft topic linked in the post it does specify about Bluetooth that "Not all APIs are currently supported for packaged apps." Which is terribly non-specific.
Also should mention, before trying this I tried to use the 32feet library, but my device was not showing up at all while other devices (not LE) were, so I am assuming that 32feet just doesn't support BTLE. I found this asking the same about 32feet and that is what led me to try what I have detailed above instead, but I'm still not 100% clear on whether or not I can simply use 32feet to connect to BTLE devices.
So my question is, am I right that what am I trying to do (WPF application using Windows 10 API to connect to Bluetooth device) can't currently be done? If not, what am I doing wrong?
Update after two years the question posted: I think there's still no library supporting WPF Bluetooth LE. A pretty straight forward proof is that, Windows settings, which is under UWP, supports Bluetooth LE devices, while Control Panel on Windows 10 doesn't.
The only solution I can come up with now is buying a Bluetooth dongle which has virtual COM port feature, so ideally I can talk to the BLE device like using serial connection. Haven't tested yet, will update later if this works.

Can't instantiate a new GattDeviceService

I'm new to Bluetooth LE, and I'm trying to write to a Bluetooth HID device.
I'm using Visual Studios 2015 and am building a WPF application.
currently my code looks like this:
var hidDevices = await DeviceInformation
.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(
GattServiceUuids.HumanInterfaceDevice));
GattDeviceService bleDevice = await GattDeviceService.FromIdAsync(hidDevices[0].Id);
I have some code after, but my code stops right after I try to create a new GattDeviceService.
I know hidDevices[0] is occupied, but the ID generated shows up like this:
\\?/BTHLEDEVICE#{00001812-xxxx-xxxx-xxxx-xxxxxxxxxxxx}_Dev_VID&.........
I'm curious if that is normal.
GATT API does not allow you access HID Services.
I found information about your problems:
Bluetooth GATT—Windows.Device.Bluetooth.Gatt
The API lets Windows Store app developers implement GATT client
profiles for collecting data from low energy (LE) sensors.
A Bluetooth 4.0 radio is required to use the GATT API.
The GATT API prevents access to the following in-box and invalid
services:
(Bluetooth SIG name) 0x1812 HID Over GATT Service
For more informations: https://msdn.microsoft.com/en-us/library/windows/apps/bg182882.aspx

32Feet.Net connecting to Bluetooth speakers

So I am trying to connect a bluetooth speakers from a script. I am using 32feet.net and I have successfully found the device but it doesn't work when I try to pair and connect to it.
This is the code im using to pair to device, this always fails not sure why:
private static void connected(BluetoothDeviceInfo[] dev)
{
// dev[foundIndex];
bool paired=false;
paired = BluetoothSecurity.PairRequest(dev[foundIndex].DeviceAddress, "1166");
if (paired)
Console.WriteLine("Passed, Device is connected.");
else
Console.WriteLine("Failed....");
}
Here is the code called after connected to actually connect to the device: bc is my bluetooth client var.
bc.BeginConnect(devInfo[foundIndex].DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), devInfo[foundIndex]);
private static void Connect(IAsyncResult result)
{
if (result.IsCompleted)
{
Console.Write("Connected... ");
}
}
Any help would be appreciated. I am new to 32feet.net so i dont know much about this, i tried following code online to get where im at.
Try BluetoothDeviceInfo.SetServiceState. That will ask Windows to connect to the audio service on the device -- hopefully that'll do the job.
See https://32feet.codeplex.com/wikipage?title=Connecting%20to%20Bluetooth%20Services
Sometimes we don’t want our application to itself send data to/from a remote service but we want instead the local operating system to do so. This is the case for keyboard/mouse/etc with HID, networking with DUN/NAP/PAN/etc, Headset/Handsfree etc.
and then
The short answer in this case is to use BluetoothDeviceInfo.SetServiceState. This is the API equivalent to manually checking the respective checkbox on the “Services” tab of the Device dialog in Bluetooth Control panel.
Also, in these days of Secure Simple Pairing, using PairRequest is fine only if all peer devices will use old style PIN code authentication, otherwise instantiate a BluetoothWin32Authentication and then do the connect (here indirectly via SetServiceState) and handle the authentication in the authentication callback.

How can I connect to a Server Side USB (HID) Device from within an ASP.NET Application?

I'm trying to write my own controller for a USB device instead of using the SDK that comes with the product (I feel the sdk is sub-par).
The USB Device is plugged into the SAME SERVER that this application is running on.
So I decided to head over to Nuget and grab the HidLibrary
PM> Install-Package hidlibrary
and I proceeded to follow the example found on GitHub.
First I went into my control panel to verify the VendorID and the ProductID
And I dropped it into my code.
Then I set a breakpoint on the line that grabs the device, but unfortunately it always comes back null.
using HidLibrary;
public class MyController : ApiController
{
private const int VendorId = 0x0BC7;
private const int ProductId = 0x0001;
private static HidDevice _device;
// POST api/<controller>
public string Post(CommandModel command)
{
_device = HidDevices.Enumerate(VendorId, ProductId).FirstOrDefault();
if (_device != null)
{
// getting here means the device exists
}
else
{
// ending up here means the device doesn't exist
throw new Exception("device not connected");
}
return null;
}
I'm hoping it's something silly, and not some deal-breaking permissions issue regarding connecting to a USB device directly from an IIS worker.
Despite your hopes to be something silly, it is not. You have some deal-breaking permission issues. If you will browse Mike O'Brien's code from GitHub of the Hid Library you will see that it calls Win32 API functions located in: kernel32.dll, setupapi.dll, user32.dll, hid.dll (Native.cs).
The enumeration itself it's done through setupapi.dll functions. It browse all the installed devices and filters what it's need it.
So... I think it's a security issue to execute kernel32.dll code directly from a web-app in IIS with anonymous authentication, don't you?
If you really need to communicate with that HID (who knows maybe it's a temperature sensor, or something else) I would do a separate Windows service and the IIS hosted web app would communication through WCF with this service. This service would like a proxy.
Put the same code in a console application and run it. That will help you verify if it's your code or environment.
If it's environment, try using Process Monitor to see if there are any hidden access errors. Also try enumerating all devices, not just looking for the one device you're after, just to see if you can do it in ASP.NET.
#Chase, unless this is an experiment - it is best not to attempt connecting to a device from IIS process. [It's a Pandora's box if you start down this path].
Best way to do this is to have another (WCF) service as proxy to the device and expose just what you need out of the service, hook it up with your app. Feel free to ask for an example if you think that would help.
I +1 #garzanti.

Jabber-net integration

I'd like to ask your help regarding having a Google Talk Bot that will communicate with my code on my server.
I have downloaded Jabber-Net from code.google.com, but the examples there are not enough... I am new to these technologies, and have no clue about:
How will client arrive to my server? where should I change [if any] DNS to my server?
Which server side library should I use?
From the examples I understood that I need to have a Desktop-app running in the background constantly, which doesn't make sense to me.
Does anyone has an example of some better references to understand this better?
[Sorry for my ignorance...]
I'm not sure if I understand what you ask correctly. If you're asking how to connect to chosen server, console sample shows how to do it simply, you basically fill out JID class.
Sample from Jabber-Net
JabberClient jc = new JabberClient();
JID j = new JID(jid);
jc.User = j.User;
jc.Server = j.Server;
jc.NetworkHost = networkHost;
jc.Port = port;
jc.Resource = "Jabber.Net Console Client";
jc.Password = pass;
jc.AutoStartTLS = TLS;
jc.AutoPresence = initialPresence;
If you want to create your own server, there's a library (also running under .NET) called agsxmpp, it allows to create both, server and client, it's open source on MIT/GPL license afair. I don't know if jabber-net enables this feature. On the other hand, there are plenty of free jabber-server if you don't want to just use one of "public" ones, so it may be worth to consider just using something that is ready to be launched.
There's a console sample in the project, you don't need desktop-app (if this is what you were asking?), so you can write service, console app or anything else.
Here's a recent post that shows an example of replying to incoming messages on Gtalk using .NET

Categories

Resources