Detect barcode scanner with PointOfService on Windows 10 - c#

I would like to use a barcode scanner with Windows 10 (Build 15063) via the Windows.Devices.PointOfService namespace. The scanner is a Datalogic Quickscan QD2430 and I tried with all RS-232 and Keyboard mode.
I used the official sample application https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BarcodeScanner with no luck. It can detect a device but it's definitely the in-built webcam (HP laptop).
I tried to modify the source, the DeviceHelpers's GetFirstDeviceAsync function https://github.com/Microsoft/Windows-universal-samples/blob/master/SharedContent/cs/DeviceHelpers.cs.
The DeviceInformation.FindAllAsync also returns only the camera's info as result.
string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.All);
DeviceInformation.FindAllAsync(selector);
It returns nothing.
DeviceInformation.FindAllAsync(DeviceClass.ImageScanner);
It returns every connected and I think the previously connected but currently offline devices too. I tried to filter the scanner by name. There was a lot filterd result too, but the convertAsync function returned null for all excepts one, it thrown an Exception "A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)".
DeviceInformationCollection infos = await DeviceInformation.FindAllAsync(DeviceClass.All);
foreach(DeviceInformation info in infos)
{
if (info.Name.ToUpper().Contains("BARCODE"))
{
T scanner = await convertAsync(info.Id);
if (scanner != null)
{
return scanner;
}
}
}

Datalogic Quickscan QD2430 is not in the list of devices supported by Windows.Devices.PointOfService.
Ask Datalogic to provide a device driver that supports Windows.Devices.PointOfService, or change the scanner to the one described in the supported list.
Alternatively, create your own device driver according to the Point of Service (POS) of Windows Driver Kit.

Related

How to find USB HID DevicePath?

I've been working on USB HID Device in embedded system and C# for a while. I decided to use USBHid library in C#. I got the ideal result with this library. But I have a problem. While defining the USB in the USBHid library, the following code is sufficient in the project of the library that I found on the internet.
public UsbHidDevice Device;
Device = new UsbHidDevice(vvvv,pppp);
However, when I use the same library, it asks me for an expression in the following format.
public UsbHidDevice Device; string vidandpid =
"\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49}";
Device = new UsbHidDevice(vidandpid);
I adapted this format for my own HID device, but without success. How should this string expression be? I am open to your views. Thank you from now.
How to find USB HID DevicePath?
I gave up on USBHID library and found solution with HidLibrary library. As far as I understand, HidLibrary falls short on some issues.
Here I am sharing the C# code that I linked with HidLibrary. Thank you for all the replies.
device = HidDevices.Enumerate(VendorID, ProductID).FirstOrDefault();
if (device != null)
{
device.OpenDevice();
device.Inserted += DeviceAttachedHandler;
device.Removed += DeviceRemovedHandler;
device.MonitorDeviceEvents = true;
device.ReadReport(myfunction);
}
else { RtBox_Feedback.AppendText("NOT DEVICE!"); }
\\hid#vid_0000&pid_0000&mi_00#a&0&000000000&1&0000#{eeof37d0-1963-47k4-aa41-74476db7uf49} - is a device interface for the {eeof37d0-1963-47k4-aa41-74476db7uf49} interface. It is almost always unique and semi-random for each device. It also may change if yore put device in another USB slot. This string may be used as path to open this "file" with CreateFile Win32 API and talk with device by means of interface-specific IOCTLs. More info here.
For HID devices Windows have another device interface GUID - GUID_DEVINTERFACE_HID - {4D1E55B2-F16F-11CF-88CB-001111000030}
You can use CM_Get_Device_Interface_ListW or SetupDiGetClassDevs/SetupDiEnumDeviceInterfaces/SetupDiGetDeviceInterfaceDetail APIs to enumerate device interfaces by interface GUID or by device Instance ID.
Mentioned "USBHid library in C#" may already have code that doing such enumeration and filtering by HID deivce VID/PID but I cannot say it for sure since you haven't added link to the code of this library. :)

Finding paired Bluetooth device if its in range using 32Feet

I'm trying to make a simple application that will show the nearby Bluetooth devices (this one works fine)
Also I'm trying to find if the specific Bluetooth device (I have its MAC & Its already paired in windows) is in range. It's an android phone and by default its set to Invisible to nearby devices, But that wouldn't be a problem since it's already paired in windows, right?
Am using this code:
BluetoothClient BTClient = new BluetoothClient();
BluetoothDeviceInfo[] BTDeviceInfo = BTClient.DiscoverDevices();
which is working find for finding "Visible Devices", It will also show me Paired device weather its In-Range or Not!
How i can check if that paired devices is in-range? Without going to setting and make it "Visible" will be much better
It might be too late, but you can use BTClient.DiscoverDevicesInRange();
and look for your device in that list.
Another method would be to try reading the services in the device using its bluetooth address, you will get an exception if it isn't in range.
private static Boolean IsInRange(BluetoothDeviceInfo device)
{
Guid fakeUuid = new Guid("{F13F471D-47CB-41d6-9609-BAD0690BF891}");
try
{
ServiceRecord[] records = device.GetServiceRecords(fakeUuid);
return true;
}
catch(SocketException ex)
{
if (ex.ErrorCode == 10040) return true;
return false;
}
}

Search for devices in range of bluetooth UWP

Is it possible to scan for devices in range of bluetooth? I tried FindAll method but it returns all paired devices. Also tried deviceWatcher with the same result.
Is it possible to scan for devices in range of bluetooth?
This is possible, we can use either DeviceInformation.FindAllAsync method or DeviceWatcher class. But to get all Bluetooth devices, we need to note that currently Bluetooth APIs don't provide a selector to get ALL devices that are both paired and non-paired. BluetoothDevice.GetDeviceSelector method actually returns the same value as BluetoothDevice.GetDeviceSelectorFromPairingState(true). So when you use this selector (Advanced Query Syntax (AQS) string), you will always get all paired devices.
To get all devices, we can use this selector get paired devices first and then use BluetoothDevice.GetDeviceSelectorFromPairingState(false) to get the rest unpaired devices.
Or we can just specify the AQS string like following:
//The AQS string for getting all Bluetooth devices
var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\"";
//Using DeviceInformation.FindAllAsync method
var deviceInfoCollection = await DeviceInformation.FindAllAsync(BluetoothDeviceSelector);
//Using DeviceWatcher class
var deviceWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector);
PS: FindAllAsync method is more often used to look through the devices that are currently connected to or paired with the system. To get both paired and non-paired devices, it better to use DeviceWatcher class.
For more info about how to use FindAllAsync method or DeviceWatcher class, please see Enumerate devices and official Device enumeration and pairing sample.
Besides Bluetooth devices, there are also Bluetooth LE devices you might want to get. And for Bluetooth LE devices, the AQS string would be System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}".
GetDeviceSelector and friends return strings containing AQS queries. Have you ever looked at them?
Paired
System.Devices.DevObjectType:=5
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}"
AND (
System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True
OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False
)
Unpaired
System.Devices.DevObjectType:=5
AND System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}"
AND (
System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False
OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#True
)
What do you think is tested by the expression in parentheses?
What do you think might happen if you omit this clause from the string you supply to CreateWatcher?
Something less obvious that you may find helpful is the fact that there are two Bluetooth protocol identifiers. The one shown above is for classic Bluetooth and the query does not match BLE (Bluetooth Low Energy) devices.
The selector I suggest you use is
System.Devices.DevObjectType:=5
AND (
System.Devices.Aep.ProtocolId:="{BB7BB05E-5972-42B5-94FC-76EAA7084D49}"
OR System.Devices.Aep.ProtocolId:="{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}#"
)
I have put line breaks in all these queries to help you read them. Don't include them in your code. For convenience here's some code from one of my projects.
string BleSelector = "System.Devices.DevObjectType:=5 AND (System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" OR System.Devices.Aep.ProtocolId:=\"{E0CBF06C-CD8B-4647-BB8A-263B43F0F974}\")";
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.ProtocolId" };
deviceWatcher = DeviceInformation.CreateWatcher(BleSelector, requestedProperties, DeviceInformationKind.AssociationEndpoint);
Using a watcher has the advantage that the list becomes available progressively. FindAllAsync may be asynchronous but it takes ages to return.
I provide BluetoothLE Device Selector string in the following code. It works for me.
DeviceWatcher dWatcher = null;
var BluetoothDeviceSelector = "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\" AND ((System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False) OR System.Devices.Aep.Bluetooth.IssueInquiry:=System.StructuredQueryType.Boolean#False)";
dWatcher = DeviceInformation.CreateWatcher(BluetoothDeviceSelector);
dWatcher.Added += DeviceAdded;
dWatcher.Updated += DeviceUpdated;
dWatcher.Start();

USB device not valid in MonoLibUsb

I'm trying to open a USB device handle in MonoLibUsb (on Linux), but every time I open it I get IsInvalid == true on the device handle.
The USB device is definitely compatible with LibUsb as I've connected it to my Windows PC and can successfully use LibUsbDotNet to talk to it. If I try to use LibUsbDotNet in Mono the application hangs when trying to open it, so I figured LibUsbDotNet is for Windows and MonoLibUsb is for Mono (the name kind of gives it away). However, even MonoLibUsb fails to properly use the device.
So why is the device handle returned invalid?
Code
private void UsbInit() {
var sessionHandle = new MonoUsbSessionHandle();
var profileList = new MonoUsbProfileList();
profileList.Refresh(sessionHandle);
List<MonoUsbProfile> usbList = profileList.GetList().FindAll(MyVidPidPredicate);
foreach(MonoUsbProfile profile in usbList) {
var deviceHandle = profile.OpenDeviceHandle();
if (deviceHandle.IsInvalid) {
Console.WriteLine(string.Format("IsInvalid: {0} - {1}", MonoUsbSessionHandle.LastErrorCode, MonoUsbSessionHandle.LastErrorString));
}
}
}
private bool MyVidPidPredicate(MonoUsbProfile profile) {
if (profile.DeviceDescriptor.VendorID == 0xabcd && profile.DeviceDescriptor.ProductID == 0x1234)
return true;
return false;
}
Output
IsInvalid: Success -
This line in the documentation is very easy to overlook:
The user must have appropriate access permissions to the usb device before it can be used with linux.
If I'm starting the application as root (or through sudo) the device handle becomes valid.

UWP: BluetoothDevice.FromBluetoothAddressAsync throws 0x80070002 exception on non-discoverable & unpaired BT device

I'm trying to pair from a Universal Windows C# app to a Bluetooth - Serial converter, without user interaction.
Development is in Visual Studio 2015 under Windows 10 Pro, but app is intended to run in any Windows 10 based device with a Bluetooth adapter.
For security reasons, BT-serial converter isn't discoverable and is protected by a pin, so I'm not able to perform any enumeration procedure to detect and pair it.
My application only knows BT address (MAC) and PIN's device (Also it knows friendly Bluetooth name, but I never used it).
Previously I'd been able to perform this task under Windows Mobile 6 Pocket PC, using Windows Mobile SDK and C++, but unfortunately code isn't portable to Universal Windows Platform.
Playing with MS samples for Universal Windows(https://github.com/Microsoft/Windows-universal-samples) I achieved to write a code that achieves to pair device, creating from strings some objects that in source sample are derived from enumeration process.
But it only works if device is visible. This is the code:
using System;
using Windows.UI.Xaml.Controls;
using Windows.Networking;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace UWPBTTest
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DoPairing();
}
async public void DoPairing()
{
UInt64 targetMAC = 32217180653; //target MAC in decimal, this number corresponds to my device (00:07:80:4b:29:ed)
BluetoothDevice btDev = await BluetoothDevice.FromBluetoothAddressAsync(targetMAC); //We create target BT device object, here app throws 0x80070002 exception
DeviceInformation infDev = btDev.DeviceInformation; //We need this aux object to perform pairing
DevicePairingKinds ceremoniesSelected = DevicePairingKinds.ConfirmOnly | DevicePairingKinds.ProvidePin; //Only confirm pairing, we'll provide PIN from app
DevicePairingProtectionLevel protectionLevel = Windows.Devices.Enumeration.DevicePairingProtectionLevel.Encryption; //Encrypted connection
DeviceInformationCustomPairing customPairing = infDev.Pairing.Custom; //Our app takes control of pairing, not OS
customPairing.PairingRequested += PairingRequestedHandler; //Our pairing request handler
DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, protectionLevel); //launc pairing
customPairing.PairingRequested -= PairingRequestedHandler;
if ((result.Status == DevicePairingResultStatus.Paired) || (result.Status == DevicePairingResultStatus.AlreadyPaired))
{
//success, now we are able to open a socket
}
else
{
//pairing failed
}
}
//Adapted from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing , scenario 9
private async void PairingRequestedHandler(
DeviceInformationCustomPairing sender,
DevicePairingRequestedEventArgs args)
{
switch (args.PairingKind)
{
case DevicePairingKinds.ConfirmOnly:
// Windows itself will pop the confirmation dialog as part of "consent" if this is running on Desktop or Mobile
// If this is an App for 'Windows IoT Core' where there is no Windows Consent UX, you may want to provide your own confirmation.
args.Accept();
break;
case DevicePairingKinds.ProvidePin:
// As function must be asyn, we simulate a delay of 1 second on GetPinAsync
var collectPinDeferral = args.GetDeferral();
string pin = "1234"; //BT converter pin, currently is "1234" for testing purposes
args.Accept(pin);
collectPinDeferral.Complete();
break;
}
}
}
}
This prototype app uses a blank form, as all data are hardcoded.
Also in Package.appxmanifest -> Capabilities, I checked all fields to discard any permission lack.
Currently this code only can perform pairing operation if BT-serial converter is visible.
If BT device isn't visible, BluetoothDevice.FromBluetoothAddressAsync throws an exception (Resource not found: 0x80070002), instead of creating the BluetoothDevice object.
Is as if Windows "needed" to know something of BT device in order to perform operation, I suspect that when I call FromBluetoothAddressAsync, OS internally lists all devices in system (this includes detected BT devices in range) looking for and item with given address.
I've been looking for other methods to perform mac-based pairing against hidden bt devices, but without success (maybe some type of "pre-pairing"? I didn't find anything)
Thanks.

Categories

Resources