Async HFP discovery with 32feet - c#

I am in the process of writing a bluetooth scanner to monitor traffic outside my house. I understand that car BT devices generally have 3 states, visible (always on), limited, and invisible.
I'm using asynchronous device discovery with the Bluesoleil drivers, and a Class 1 Bluetooth dongle.
My problem is the device name and device class isn't being discovered. It is discovering the device address, and the range, but not the name of the device.
I have tried using System.Threading.Thread.Sleep(1000) then calling device.Refresh() to no avail.
It is picking up the name of a few things that are in range, like TVs, and Fitbit Flexes, but it's important that I can find the name and CoD of the car bluetooth devices, not just the MAC address and the range.
public void SetupListener()
{
var client = new BluetoothClient();
var component = new BluetoothComponent(client);
component.DiscoverDevicesAsync(255, false, false, true, false, null);
component.DiscoverDevicesProgress += component_DiscoverDevicesProgress;
component.DiscoverDevicesComplete += component_DiscoverDevicesComplete;
}
void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
SetupListener();
}
void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
BluetoothDeviceInfo[] availableDevices = e.Devices as BluetoothDeviceInfo[];
foreach (BluetoothDeviceInfo device in availableDevices)
{
device.Refresh();
var name = device.DeviceName; // This mostly returns the MAC address
var cod = device.ClassOfDevice.Device; // This mostly returns Miscellaneous
}
}
The application I've built is a simple WPF form that dumps the found devices to a datagrid (which I've removed from the code for simplicity's sake). This is generally what the results look like.
When I sit outside closer to the road, it is definitely picking up more and more devices as cars drive by, but I can't tell what type of device it's detecting.
Any help would be greatly appreciated.

Related

HidSharp Never Receives Input Report

I'm attempting to use HidSharp to read reports from a Vendor USB HID device using the below code, but I never manage to poll any input.
var device = DeviceList.Local.GetHidDevices().Where(d => /* My Vendor stuff */).First();
var deviceDescriptor = device.GetReportDescriptor();
if (device.TryOpen(out var inputStream))
{
var inputReport = deviceDescriptor.InputReports.First();
var inputReportParser = inputReport.DeviceItem.CreateDeviceItemInputParser();
var inputReportReciever = deviceDescriptor.CreateHidDeviceInputReceiver();
var inputReportBuffer = new byte[inputReport.Length];
while (inputReportReciever.IsRunning)
{
if (inputReportReciever.WaitHandle.WaitOne(10000))
{
while (inputReportReciever.TryRead(inputReportBuffer, 0, out var report))
{
if (inputReportParser.TryParseReport(inputReportBuffer, 0, report))
{
if (inputReportParser.HasChanged)
{
var data = inputReportParser.GetValue(inputReportParser.GetNextChangedIndex());
Console.WriteLine("Data: " + data.ToString());
}
}
}
}
}
}
I can see in the device manager that the device is recognized as an HID USB device, and Wireshark shows that I am sending periodic reports like so:
(I'm a java developer so I'm really struggling with Visual Studio) I can't step in to the library to see what's happening, but there are no errors being thrown and the 'test' included with the project parses reports from at least one other USB device (a headset) so the library seems ok. I'm not at all sure how to proceed with debugging further. Any direction on this library or USB is appreciated.

Filter for serial ports

I have an application in which I have to use the serial ports, so alright, it finds all right, but I thought and if the client has more than 1 COM port connected on the PC? How do I do when I checkbox and dial, when updating only my device appears? (My USB converter is no longer generic, it contains your own name and address).
Here is the function where I update the COM ports
public void AtualizarPortas()
{
PortasDisponiveis.Clear();
var portas = SerialPort.GetPortNames().OrderBy(x => x).ToList();
foreach (var porta in portas)
{
PortasDisponiveis.Add(porta);
}
PortaSelecionada = PortasDisponiveis.FirstOrDefault();
}
What could I do to get only my device to appear?

Detect when ethernet cable is plugged

I'm trying to detect when an Ethernet cable is plugged-in or unplugged but i have some probleme and i don't know if i'm doing this good or not.
I'm using NetworkChange.NetworkAddressChanged
to detect when the network change
and then NetworkInterface.GetAllNetworkInterfaces() for checking if Ethernet connexion is available or not with the property .OperationalStatus.
But when i search for the Ethernet connexion in all the network interfaces, it return me what i'm looking for, but it always return me the Bluetooth connection with it.
Here is the code :
public Form1()
{
InitializeComponent();
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
Console.ReadLine();
}
static void AddressChangedCallback(object sender, EventArgs e)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface myInterface in adapters)
{
//if (n.Description.ToLower().Contains("ethernet")){
//if (n.NetworkInterfaceType.ToString().ToLower().Contains("ethernet")){
IPInterfaceProperties properties = n.GetIPProperties();
if (myInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(myInterface.Description + " ........... : " + myInterface.OperationalStatus);
Console.WriteLine("NetworkInterfaceType : " + myInterface.NetworkInterfaceType);
}
}
}
At the beginning, i was trying to check the name of the connection and looks if contains the "Ethernet" word, but it appears (if i'm not wrong) sometimes the connection name does not contain "Ethernet".
Do you have some tips for always bring the good connection (without the bluetooth)?
Am i wrong in my approach?
I'm testing it on a Surface Pro 3... but maybe i have the Bluetooth problem because of that?
Despite that, i need it to work even on device like this.
This can be done by checking its operational status as:
foreach (System.Net.NetworkInformation.NetworkInterface net in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
{
if (net.OperationalStatus ==
System.Net.NetworkInformation.OperationalStatus.Up)
Console.WriteLine("N/w connected");
else
Console.WriteLine("N/w not connected");
}
This links shows how to do it with Powershell, but one of the cases uses WMI.
http://www.powershellmagazine.com/2013/04/04/pstip-detecting-wi-fi-adapters/
And this links shows a interesting property that can help sometimes:
https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx
GatewayCostMetric
Data type: uint16 array
Access type: Read-only Array
of integer cost metric values (ranging from 1 to 9999) to be used in
calculating the fastest, most reliable, or least resource-intensive
routes. This argument has a one-to-one correspondence with the
DefaultIPGateway property.

How to detect Windows Mobile 5 Device Serial Number? (.NET CF 3.5)

We have several devices where I work (mostly Datalogic 4420 Falcon), and someone is always leaving one off the base. The battery runs dry, then they bring them back to get setup all over. (There's supposed to be a way to configure a file on the SD card to reload upon such an error, but it doesn't work very well)
When someone saves changes on the device (using my app that writes data to the SQL Server), the Serial Number is sent along with it so we can track what devices are in use where.
Each device has a Serial Number, and I have to physically (i.e. manually) write that into the Device name field, which I can read. Working code here if anyone wants to know how:
static string deviceId = null;
public static string DeviceName {
get {
if (String.IsNullOrEmpty(deviceId)) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Ident", true)) {
try {
deviceId = key.GetValue("Name", "[Unnamed]").ToString();
} catch (Exception e) {
ErrorWrapper("GetDeviceName", e);
deviceId = Dns.GetHostName();
} finally {
key.Flush();
key.Close();
}
}
}
return deviceId;
}
}
I do not like the manual (i.e. Fat Finger prone) Serial Number entry. Is there some call to query the device's Serial Number, or is that vendor specific?
Datamax does make an SDK that is specific to their devices, but we don't want our applications tied down to any one manufacturer (we are already tied down to VS2008).
I'd start by trying to P/Invoke to get the device ID (KerneIoControl with IOCTL_HAL_GET_DEVICEID) and see if it matches the serial number you're after. Here's an example.
I don't know about your Datalogic 4420 Falcon device, but I work with Intermec CK30 & CK60 and I have their itc50.dll file.
Here is snippet:
[DllImport("itc50.dll")]public static extern int ITCGetSerialNumber(StringBuilder Snumber, int buffSize);
StringBuilder hwSN = new StringBuilder(12);
if (ITCGetSerialNumber(hwSN, hwSN.Capacity) >= 0)
{
;
;
}

detecting presence of bluetooth printer

I'm working on a mobile application (C#/WPF on a tablet PC) that prints to a bluetooth connected printer. Right now I just fire off a print job, and if the printer is not present the printer subsystem reports an error to the user. I'm not doing anything programatically with Bluetooth, just using PrintDialog().
I'd like to modify this process to detect the printer first - if it is not available, then I'll just store the document without printing. Is there a way in code for me to detect if the bluetooth device is connected/active/available?
If I look at the device in the Bluetooth panel under Control Panel, it doesn't seem to have any sort of status that reflects whether or not the device is available, so maybe this isn't possible.
I'm assuming the printer has already been setup and configured in Windows - all I need to do is detect if it is actually present at a given point in time.
Perhaps use the 32feet.NET library (of which I am the maintainer) and check if the printer is present before submitting the job. You'd need to know the Bluetooth address of the printer; can one get that from the system, or maybe you always know it.
Discovery on the MSFT Bluetooth stack always returns all known devices in amongst those in range :-( but we can use other means to detect the device's presence/absence. Perhaps using BluetoothDeviceInfo.GetServiceRecords in its BeginGetServiceRecords form. e.g. something like (not tested/compiled):
bool IsPresent(BluetoothAddress addr) // address from config somehow
{
BluetoothDeviceInfo bdi = new BluetoothDeviceInfo(addr);
if (bdi.Connected) {
return true;
}
Guid arbitraryClass = BluetoothService.Headset;
AsyncResult<bool> ourAr = new AsyncResult<bool>(); // Jeffrey Richter's impl
IAsyncResult ar = bdi.BeginGetService(arbitraryClass, IsPresent_GsrCallback, ourAr);
bool signalled = ourAr.AsyncWaitHandle.WaitOne(Timeout);
if (!signalled) {
return false; // Taken too long, so not in range
} else {
return ourAr.Result;
}
}
void IsPresent_GsrCallback(IAsyncResult ar)
{
AsyncResult<bool> ourAr = (AsyncResult<bool>)ar.AsyncState;
const bool IsInRange = true;
const bool completedSyncFalse = true;
try {
bdi.EndGetServiceResult(ar);
ourAr.SetAsCompleted(IsInRange, completedSyncFalse);
} catch {
// If this returns quickly, then it is in range and
// if slowly then out of range but caller will have
// moved on by then... So set true in both cases...
// TODO check what error codes we get here. SocketException(10108) iirc
ourAr.SetAsCompleted(IsInrange, completedSyncFalse);
}
}

Categories

Resources