C# connecting to an already paired device using 32feet.NET - c#

I am trying to connect to a Blood Pressure Machine via bluetooth. The device is already paired with my laptop. When I use nuget pacage inTheNet. I am able to get a list of devices near by, but am unable to get the device that is already paired
string macAddress = FindMACAddress();
_blueToothEndPoint = new BluetoothEndPoint(BluetoothAddress.Parse(macAddress), BluetoothService.BluetoothBase);
_blueToothClient = new BluetoothClient(_blueToothEndPoint);
BluetoothDeviceInfo[] devices = _blueToothClient.DiscoverDevices();
foreach (BluetoothDeviceInfo device in devices)
{
Console.WriteLine(device.DeviceAddress);
}

If anyone need more fast solution use:
public BluetoothDeviceInfo[] DiscoverDevices(int maxDevices, bool authenticated, bool remembered, bool unknown);
Where:
maxDevices - The number of in-range devices to find before the inquiry may be stopped early. The result can contain more than this number of devices.
authenticated - True to return previously authenticated/paired devices.
remembered - True to return remembered devices.
unknown-True to return previously unknown devices.
Example:
var devices = bluetoothClient.DiscoverDevices(10, true, true, false);
foreach (var device in devices)
{
var blueToothInfo =
string.Format(
"- DeviceName: {0}{1} Connected: {2}{1} Address: {3}{1} Last seen: {4}{1} Last used: {5}{1}",
device.DeviceName, Environment.NewLine, device.Connected, device.DeviceAddress, device.LastSeen,
device.LastUsed);
blueToothInfo += string.Format(" Class of device{0} Device: {1}{0} Major Device: {2}{0} Service: {3}",
Environment.NewLine, device.ClassOfDevice.Device, device.ClassOfDevice.MajorDevice,
device.ClassOfDevice.Service);
Console.WriteLine(blueToothInfo);
Console.WriteLine();
devicesList.Items.Add(new BluetoothDeviceInfoContainer(device));
}

This seems to work with the device.Remembered property.
BluetoothDeviceInfo[] devices;
foreach (var device in devices)
{
if (device.Remembered == true) return "Already Paired";
else return "Not Paired";
}
Hope this helps!
Chris

Related

Connect an already paired bluetooth headphone with .NET Core on Windows 11

I'm trying to write a small streamdeck plugin to connect my paired headphones after they have been paired to another device and haven't connected to my PC automatically. I basically want to kick off the same process as clicking Connect in the Windows 11 device list.
I'm able to enumerate my devices, find the correct device based on the device ID, and identify if it is already connected, but I'm not sure how to enable the music and chat connections if it's disconnected.
Here's basically what I have
string[] requestedProperties = {
"System.Devices.Aep.IsConnected",
};
var devices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector(), requestedProperties);
foreach (DeviceInformation di in devices) {
if (!di.Properties.ContainsKey("System.Devices.Aep.IsConnected")) {
continue;
}
if (di.Properties[ConnectedKey] is true) {
Debug.WriteLine($"Connected Device Name {di.Name}");
Debug.WriteLine($"Connected Device Id {di.Id}");
continue;
}
if ("<myHeadphonesBluetoothId>" == di.Id) {
// Not sure where to go at this point to initiate those connections
}
}
Been googling for hours without any success, and given some attempts at the AudioPlaybackConnection, BluetoothDevice, and RfcommDeviceService classes without success.
My big issue was a misunderstanding in the architecture involved in the way that devices, services, etc work.
I was able to get a small sample app working targeting net6.0-windows10.0.22000.0
const string targetDeviceId = "Bluetooth#Bluetooth00:00:00:00:00:00-00:00:00:0:0:00";
const int serialPortShortId = 0x1101;
using var streamSocket = new StreamSocket();
await EstablishConnection();
async ValueTask EstablishConnection() {
RfcommServiceId targetServiceId = RfcommServiceId.FromShortId(serialPortShortId);
BluetoothAdapter adapter = await BluetoothAdapter.GetDefaultAsync();
if (adapter is not null)
{
Radio btRadio = await adapter.GetRadioAsync();
if (btRadio.State == RadioState.Off)
{
await btRadio.SetStateAsync(RadioState.On);
Console.WriteLine("Bluetooth adapter was off. Turned it on.");
}
}
using BluetoothDevice btDevice = await BluetoothDevice.FromIdAsync(targetDeviceId);
btDevice.ConnectionStatusChanged += (sender, args) => Console.WriteLine("Bluetooth device connected");
RfcommDeviceServicesResult services = await btDevice.GetRfcommServicesForIdAsync(targetServiceId);
RfcommDeviceService service = services.Services.Single();
if (btDevice.ConnectionStatus != BluetoothConnectionStatus.Connected && service.DeviceAccessInformation.CurrentStatus == DeviceAccessStatus.Allowed) {
try {
await streamSocket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
}
catch (COMException e) when ((uint)e.HResult == 0x8007277C) {
Console.WriteLine("Bluetooth device is not on or in range.");
}
}
}
After running this the device would connect and windows would initialize the mic and speaker devices attached to it.

Getting the COM port name for a known Bluetooth device in UWP

I'm using a DeviceWatcher to get the DeviceInformation for a paired Bluetooth device in a UWP app. I set the DeviceWatcher up like this
var requestedProperties = new string[] { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
var deviceWatcher = DeviceInformation.CreateWatcher("(System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\")", requestedProperties, DeviceInformationKind.AssociationEndpoint); // ClassGuid = {e0cbf06c-cd8b-4647-bb8a-263b43f0f974} includes all Bluetooth devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Start();
When the DeviceWatcher_Added event handler is called I check to see if the device is the one I am interested in by checking its name and that it offers the RfcommServiceId.SerialPort.Uuid service.
Once I have the DeviceInformation for the bluetooth device I am interested in how do I get the COM port for it? I can see it in the Device Manager, where it is listed as "Standard Serial over Bluetooth link (COM8)", but I cannot see how to get that "COM8" in UWP programmatically.
I've tried making the DeviceInformation into a SerialDevice, whereby I could then get SerialDevice.PortName (c.f. this answer) but my call to SerialDevice.FromIdAsync(deviceInfo.Id) fails with a System.Exception: The data is invalid.
(N.B. Some tantalizing answers, like this and this, use the Windows Management Intrumentation WMI functions but these are not available in UWP.)
On another question Rita suggested looking at the Serial UART sample which helped me see a way to do this. I won't mark this as the answer for a while as it seems too indirect to be the canonical way.
Although I have the the DeviceInformation for the paired Bluetooth device in my UWP app I also need the list of SerialDevices so that I can match them up. Here's the resulting code.
public async Task<string> ComPort(DeviceInformation deviceInfo)
{
var serialDevices = new Dictionary<string, SerialDevice>();
var serialSelector = SerialDevice.GetDeviceSelector();
var serialDeviceInformations = (await DeviceInformation.FindAllAsync(serialSelector)).ToList();
var hostNames = NetworkInformation.GetHostNames().Select(hostName => hostName.DisplayName.ToUpper()).ToList(); // So we can ignore inbuilt ports
foreach (var serialDeviceInformation in serialDeviceInformations)
{
if (hostNames.FirstOrDefault(hostName => hostName.StartsWith(serialDeviceInformation.Name.ToUpper())) == null)
{
try
{
var serialDevice = await SerialDevice.FromIdAsync(serialDeviceInformation.Id);
if (serialDevice != null)
{
serialDevices.Add(deviceInfo.Id, serialDevice);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
}
// Example Bluetooth DeviceInfo.Id: "Bluetooth#Bluetooth9c:b6:d0:d6:d7:56-00:07:80:cb:56:6d"
// from device with Association Endpoint Address: "00:07:80:cb:56:6d"
var lengthOfTrailingAssociationEndpointAddresss = (2 * 6) + 5;
var bluetoothDeviceAddress = deviceInfo.Id.Substring(deviceInfo.Id.Length - lengthOfTrailingAssociationEndpointAddresss, lengthOfTrailingAssociationEndpointAddresss).Replace(":", "").ToUpper();
var matchingKey = serialDevices.Keys.FirstOrDefault(id => id.Contains(bluetoothDeviceAddress));
if (matchingKey != null)
{
return serialDevices[matchingKey].PortName;
}
return "";
}

Exception while accessing RfcommDeviceService from a wearable in UWP

Here is what I have,
A bluetooth wearable (MyoArm band).
Windows Mobile 10 with anniversary update.
Both of them are paired properly.
Now, here is what I am trying to do,
I am trying to enumerate the list of all services exposed by the bluetooth device connected to my windows mobile.
I would then like to read input streams, if the service provides one.
I went though MSDN documentation and here is what I have done so far.
P.S. I have added Bluetooth access to the capabilities in the application manifest.
private async void OnReceiveClick(object sender, RoutedEventArgs e)
{
var devices = await DeviceInformation.FindAllAsync();
IList<DeviceInformation> myBluetoothDevices = new List<DeviceInformation>();
foreach (var device in devices)
{
if (device.Name.Contains("myo"))
{
var trace = string.Format("Name: {2} \t Paired: {3} \t Kind: {1} \t Id: {0}", device.Id, device.Kind, device.Name, device.Pairing?.IsPaired);
builder.AppendLine(trace);
myBluetoothDevices.Add(device);
}
}
foreach (var myBluetoothDevice in myBluetoothDevices)
{
try
{
if (myBluetoothDevice != null)
{
var service = await RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id);
// TODO: Read input stream somehow here!!!
log.Text = builder.AppendLine(string.Format("Name: {0} \t Id: {1} \t Device Info Name: {2} \t Connection Host Name: {3} \t Service Id: {4}", service.Device.Name, service.Device.DeviceId, service.Device.DeviceInformation.Name, service.ConnectionHostName, service.ServiceId.Uuid)).ToString();
}
}
catch (Exception ex)
{
builder.AppendLine(ex.Message);
}
finally
{
log.Text = builder.ToString();
}
}
}
When I run the code and click the "Receive" button, I get an exception while calling the RfcommDeviceService.FromIdAsync method.
Exception: Element not found. (Exception from HRESULT: 0x80070490)
Am I missing something here? I am new to programming with bluetooth devices, so am I approaching the problem correctly?
Firstly, please ensure the devices queried out by device name are Bluetooth devices since you find all devices not only Bluetooth devices for query. For find Bluetooth devices, DeviceWatcher is recommended and sample code please reference StartUnpairedDeviceWatcher() method in this file.
Secondly, I'm not sure why RfcommDeviceService.FromIdAsync(myBluetoothDevice.Id); cannot get a RfcommDeviceService instance but the official sample is not using this method for getting the service. It got the BluetoothDeivce firstly and then GetRfcommServices from the device.
var bluetoothDevice = await BluetoothDevice.FromIdAsync(myBluetoothDevice.Id);
var rfcommServices = await bluetoothDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(Constants.RfcommChatServiceUuid));
if (rfcommServices.Services.Count > 0)
{
service = rfcommServices.Services[0];
}
The RfcommServiceId is same as RfcommServiceProvider created. Details please reference the official sample which I have tested can run and find RfcommDeviceService instance successfully.

Communication with HID device hangs on read/write (AS3992 RFID reader)

I'm trying to communicate with UHF RFID reader based on AS3992 chip.
This device is detected by Windows as standard HID and it works with 3rd party app (I found some UHF RFID Reader GUI by LinkSprite which works, but it seems like some older C++ application).
So I'm trying to integrate this device support into my .NET application. After some research I tried HidLibrary, but when I'm trying to write something to this device (initial sequence in this sample), it hangs on "write".
Does anybody know what I'm doing wrong?
Thank you!
My OS is Win 8.1 x64.
Here's the sample application:
using HidLibrary;
namespace HidTest2
{
class Program
{
static void Main(string[] args)
{
var devices = HidDevices.Enumerate(0x1325);
var rfid = devices.First();
rfid.OpenDevice();
rfid.Write(new byte[] { 0x31, 0x03, 0x01 }); // Application hangs here
while (true) // I can't get here
{
Thread.Sleep(50);
var result = rfid.Read();
Console.Write(result.Data);
}
}
}
}
PS: I also tried HidSharp, but I got same result. HID device detected, but I can't write into it.
PSS: This is the device: Link to ebay
I can't find a datasheet for the AS3229 chip that you mentioned, so I'm guessing here...
The device is probably presenting as a USB keyboard, so you would typically only be able to write LED status bits to it (Caps lock, Num lock, Shift). Is that what you are trying to write to it?
Try removing the write and just wait for the scanned RFID string to come in.
Edit: It looks like this device is presenting as a serial device over USB...I found a description closely matching it here:
https://s3.amazonaws.com/linksprite/cuttonwood/datasheet.pdf
If it's the same device you are testing then I would try communicating to it over a COM port API rather than using the relatively lower level HID APIs you have been using.
Because time by time I get an email how and if I solved this issue, here's an answer:
I had to replace original firmware for HID communication by firmware for serial communication (search for "as399x uart 115200 hex" or "as399x uart 9600 hex" on the internet) and then it worked like a sharm. Of course you need proper programmer for C8051Fxxx (about 20$ from China), USB-Serial converter and be familiar with some soldering (You'll have to solder pins on board for JTAG and Serial port).
As mentioned above, the device may not actually be a Hid device. Have you tried enumerating through USB devices instead of Hid devices? Here is some code to enumerate USB or Hid devices. The code is here.
For Hid devices use a ClassGuid of : 4D1E55B2-F16F-11CF-88CB-001111000030
and for Win USB devices use: dee824ef-729b-4a0e-9c14-b7117d33a817
https://github.com/MelbourneDeveloper/Device.Net/blob/master/src/Device.Net/Windows/WindowsDeviceConstants.cs
public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(uint? vendorId, uint? productId)
{
return await Task.Run<IEnumerable<DeviceDefinition>>(() =>
{
var deviceDefinitions = new Collection<DeviceDefinition>();
var spDeviceInterfaceData = new SpDeviceInterfaceData();
var spDeviceInfoData = new SpDeviceInfoData();
var spDeviceInterfaceDetailData = new SpDeviceInterfaceDetailData();
spDeviceInterfaceData.CbSize = (uint)Marshal.SizeOf(spDeviceInterfaceData);
spDeviceInfoData.CbSize = (uint)Marshal.SizeOf(spDeviceInfoData);
var guidString = ClassGuid.ToString();
var copyOfClassGuid = new Guid(guidString);
var i = APICalls.SetupDiGetClassDevs(ref copyOfClassGuid, IntPtr.Zero, IntPtr.Zero, APICalls.DigcfDeviceinterface | APICalls.DigcfPresent);
if (IntPtr.Size == 8)
{
spDeviceInterfaceDetailData.CbSize = 8;
}
else
{
spDeviceInterfaceDetailData.CbSize = 4 + Marshal.SystemDefaultCharSize;
}
var x = -1;
var productIdHex = GetHex(productId);
var vendorHex = GetHex(vendorId);
while (true)
{
x++;
var isSuccess = APICalls.SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref copyOfClassGuid, (uint)x, ref spDeviceInterfaceData);
if (!isSuccess)
{
var errorCode = Marshal.GetLastWin32Error();
if (errorCode == APICalls.ERROR_NO_MORE_ITEMS)
{
break;
}
throw new Exception($"Could not enumerate devices. Error code: {errorCode}");
}
isSuccess = APICalls.SetupDiGetDeviceInterfaceDetail(i, ref spDeviceInterfaceData, ref spDeviceInterfaceDetailData, 256, out _, ref spDeviceInfoData);
WindowsDeviceBase.HandleError(isSuccess, "Could not get device interface detail");
//Note this is a bit nasty but we can filter Vid and Pid this way I think...
if (vendorId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(vendorHex)) continue;
if (productId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(productIdHex)) continue;
deviceDefinitions.Add(GetDeviceDefinition(spDeviceInterfaceDetailData.DevicePath));
}
APICalls.SetupDiDestroyDeviceInfoList(i);
return deviceDefinitions;
});
}

Pair bluetooth devices to a computer with 32feet .NET Bluetooth library

If you want to know how to use 32feet.NET library to communicate with bluetooth devices, read the solution
I am currently trying to communicate via bluetooth between a computer and a self-built .NET Gadgeteer prototype.
The Gadgeteer prototype consists of the mainboard, a power supply and a bluetooth module. The module is in discoverable mode.
On the computer a custom bluetooth program based on 32feet .NET Bluetooth is running. The program detects all bluetooth devices in range and tries to pair with them. However, this is not done automatically at the moment, I have to enter a pairing code for the device.
How can I pair devices without entering the pairing code?
Devices are found, the problem is the pairing part. I experimented a lot, but didn't find a solution...
foreach (BluetoothDeviceInfo device in this.deviceList)
{
try
{
//BluetoothClient client = new BluetoothClient(this.CreateNewEndpoint(localAddress));
//BluetoothEndPoint ep = this.CreateNewEndpoint(device.DeviceAddress);
EventHandler<BluetoothWin32AuthenticationEventArgs> handler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(HandleRequests);
BluetoothWin32Authentication auth = new BluetoothWin32Authentication(handler);
BluetoothSecurity.PairRequest(device.DeviceAddress, null);
}
}
This code block initiates the pairing and it works, but Windows is asking me to enter the pairing code for the device. I read about the BluetoothWin32Authentication to prevent this case but I don't get it right.
private void HandleRequests(object that, BluetoothWin32AuthenticationEventArgs e)
{
e.Confirm = true;
}
This is the code of the event handler (http://32feet.codeplex.com/wikipage?title=BluetoothWin32Authentication)
If you simply want to allow the pairing to go ahead when to SSP devices are connecting then handling the callback and setting e.Confirm=True will be enough -- but that is a little insecure...
I am confused -.- The goal is that the application and the gadgeteer module can send data in both directions without any user interference.
Is it true that I can't pair devices automatically without user interaction?
Is it true that if two device were already paired they can exchange data without user interaction?
I figured out how to solve my problems and my knowledge about Bluetooth connections is a bit bigger now. If someone else has problems with that, I provide my solution. The code examples represent the C# implementation of a bluetooth controller with the 32feet Bluetooth library.
Scanning
This means that devices in range are detected. My code:
// mac is mac address of local bluetooth device
BluetoothEndPoint localEndpoint = new BluetoothEndPoint(mac, BluetoothService.SerialPort);
// client is used to manage connections
BluetoothClient localClient = new BluetoothClient(localEndpoint);
// component is used to manage device discovery
BluetoothComponent localComponent = new BluetoothComponent(localClient);
// async methods, can be done synchronously too
localComponent.DiscoverDevicesAsync(255, true, true, true, true, null);
localComponent.DiscoverDevicesProgress += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesProgress);
localComponent.DiscoverDevicesComplete += new EventHandler<DiscoverDevicesEventArgs>(component_DiscoverDevicesComplete);
private void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
// log and save all found devices
for (int i = 0; i < e.Devices.Length; i++)
{
if (e.Devices[i].Remembered)
{
Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is known");
}
else
{
Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is unknown");
}
this.deviceList.Add(e.Devices[i]);
}
}
private void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
// log some stuff
}
Pairing
This means that devices get coupled with the local bluetooth device. This needs to be done once by entering a code of both sides. Can be done via code so that the user doesn't even notice that a device was added. My code for this purpose:
// get a list of all paired devices
BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);
// check every discovered device if it is already paired
foreach (BluetoothDeviceInfo device in this.deviceList)
{
bool isPaired = false;
for (int i = 0; i < paired.Length; i++)
{
if (device.Equals(paired[i]))
{
isPaired = true;
break;
}
}
// if the device is not paired, pair it!
if (!isPaired)
{
// replace DEVICE_PIN here, synchronous method, but fast
isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, DEVICE_PIN);
if (isPaired)
{
// now it is paired
}
else
{
// pairing failed
}
}
}
Connecting
This means establishing a connection and exchanging of data. Again some code:
// check if device is paired
if (device.Authenticated)
{
// set pin of device to connect with
localClient.SetPin(DEVICE_PIN);
// async connection method
localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);
}
// callback
private void Connect(IAsyncResult result)
{
if (result.IsCompleted)
{
// client is connected now :)
}
}
If you keep the order scan, pair, connect, everything should work fine. To send or receive data, use the GetStream() method of the BluetoothClient. It provides a network stream that can be manipulated.
Receiving a connection
If you want another device to connect with your device you need to listen to incoming connection requests. This only works if the device have already been paired before. My code:
BluetoothListener l = new BluetoothListener(LOCAL_MAC, BluetoothService.SerialPort);
l.Start(10);
l.BeginAcceptBluetoothClient(new AsyncCallback(AcceptConnection), l);
void AcceptConnection(IAsyncResult result){
if (result.IsCompleted){
BluetoothClient remoteDevice = ((BluetoothListener)result.AsyncState).EndAcceptBluetoothClient(result);
}
}
Replace LOCAL_MAC with a valid BluetoothAddress (e.g. by using BluetoothAddress.Parse();). After the devices are connected they can exchange messages via the underlying stream. If the connection does not work there might be authentication issues, so try setting the local device pin in the listener (l.SetPin(LOCAL_MAC, MY_PASSWORD);

Categories