Read data via bluetooth using c# - c#

I am using Lecia Disto e7100i which basically measures distance and area using laser. This device has bluetooth and can be paired with windows.
I am trying to develop an wpf app that reads the mesaured data using c#
There is no sdk that comes along with the device.
I have tried to use 32feet.Net but since there is no proper documentation I don't know where to start.
Is there any way that I can do to solve my problem?

This is not a full response, instead its more of a guideline on how to resolve your issue:
Pair the device with your Computer
Run the included software that displays the data somehow
Use WireShark to analyze the traffic
see if it is a standard protocol type or something custom
understand the protocol and reimplement it using c# and BluetoothSockets

To get started, you can try:
var client = new BluetoothClient();
// Select the bluetooth device
var dlg = new SelectBluetoothDeviceDialog();
DialogResult result = dlg.ShowDialog(this);
if (result != DialogResult.OK)
{
return;
}
BluetoothDeviceInfo device = dlg.SelectedDevice;
BluetoothAddress addr = device.DeviceAddress;
Console.WriteLine(device.DeviceName);
BluetoothSecurity.PairRequest(addr, "PIN"); // set the pin here or take user input
device.SetServiceState(BluetoothService.HumanInterfaceDevice, true);
Thread.Sleep(100); // Precautionary
if (device.InstalledServices.Length == 0)
{
// handle appropriately
}
client.Connect(addr, BluetoothService.HumanInterfaceDevice);
Also make sure that
Device appears in "Bluetooth devices" in the "Control panel".
Device is HID or change code accordingly.
Hope it helps. Cheers!

Try this demo project, and the following articles after that one.
Try to follow this tutorial
Here you can see a direct answer by the mantainer of 32feet, with which you can get in touch
Check also this answer

Related

Can't receive data from c# hidlibrary and QR-Code Scanner

I have a QR-Code Scanner Device which is connected via USB with my Computer. The device itself is set to the HID Keyboard Interface. I tried to detect a new QR-Code with the hidlibrary, but unfortunately I can't get it to work. I am using WPF, C# and .NET Core. I tried something like this:
devices = HidDevices.Enumerate(0x05E0, 0x1200).ToList();
SelectedDevice = devices.FirstOrDefault();
SelectedDevice.OpenDevice();
SelectedDevice.Inserted += SelectedDevice_Inserted;
SelectedDevice.Removed += SelectedDevice_Removed;
SelectedDevice.ReadReport(OnReport);
private void OnReport(HidReport report)
{
if (!SelectedDevice.IsConnected) return;
var byteFromDevice = report.Data;
SelectedDevice.ReadReport(OnReport);
}
private void SelectedDevice_Removed()
{
logger.Info("Scanner device removed!");
}
private void SelectedDevice_Inserted()
{
logger.Info("Scanner device attached");
SelectedDevice.ReadReport(OnReport);
}
I am pretty sure that the VendorID(0x05E0) and the ProductID(0x1200) are correct. For safety I'll attach a screenshot of the device settings from windows. When I try to run this code I get this error message: Operation is not supported on this platform.
I searched a lot on google on how to get the scanned data from a QR-Code Scanner but can't find anything working. This hidlibrary was my last chance but somehow it does not work.
I saw things like getting the KeyDownEvent from the QR-Code Scanner, but the problem is, that the code where I receive the events is not in a Form or Window. Therefore I can't receive these events.
Change your device to "USB Serial" and then use SerialPort Class
See this post for how to read the data.

How to enable LightningProviders for GPIO?

I am trying to access the GPIO on my custom SBC using Windows 10 IoT Core. I have discovered that I must use LightningProviders to accomplish this . So I tried to follow this guide to use lightning providers properly.
I used very simple code:
if (LightningProvider.IsLightningEnabled)
{
LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
}
GpioStatus = "Initializing...";
var gpio = GpioController.GetDefault();
if (gpio == null)
{
GpioStatus = "There is no GPIO controller on this device.";
}
else
{
gpio.OpenPin(1).Write(GpioPinValue.High);
GpioStatus = gpio.OpenPin(1).Read().ToString();
}
Where GpioStatus is output text on a UI.
I discovered that if I run the LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider(); line outside of the enabled check, it picks up the GPIO controller and lets me detect how many pins I have and read them (All low). However I can't change the DriveMode or write to the pins without error. The error I get just says to Make sure the LightningProviders are enabled.
This brings me back to the guide I linked at the start. It suggests to enable DMAP drivers using the Device Portal for W10IoT or DMAPUtil.exe. I have tried both. In the Device Portal the area where it should be is just blank. And in the command line trying to use the DMAPUtil.exe only returns that dmaputil.exe is not available on this system.
Therefore I am asking if there is any other way to enable the LightningProviders or if there a way to know if they are incompatible with my board?
Thanks!
UPDATE
Also tried using the devcon.exe commands in the W10IoT Command line.
I am able to locate the Direct memory access controller but when i do devcon.exe enable *PNP0200 it says it is enabled but remains disabled when checked with devcon.exe status *PNP0200
Please confirm if you have added the IOT_DMAP_DRIVER feature in your OEMInput.xml, this feature will add the DMAP driver in the image. If IOT_DMAP_DRIVER is removed from the OEMInput.xml, the Default Driver Controller will be blank in device protal, and dmaputil will be not available on Windows IoT Core. Please see the IoT Core feature list.
Update:
You can download the source of Lighting Provider, and then deploy and debug in your custom image.

C# wpf how to stream webcam to ip address (possibly with Vlc)

I need to stream a webcam to an ip address.
After days of googling I decided that the easiest way was to embed Vlc.
That said I am also still open to other solutions.
Step1 - OK!: I can see my webcam in a form by using that code:
vlcPlayer.MediaPlayer.VlcLibDirectory = new DirectoryInfo(#"c:\Program Files (x86)\VideoLAN\VLC\");
vlcPlayer.MediaPlayer.EndInit();
vlcPlayer.MediaPlayer.Play(new Uri(#"dshow://");
Step2 - OK!: Now trying to make a step forward and using this tutorial I can send a video locally 127.0.0.1:5004 using 2 instances of Vlc: one to transmit and one to receive.
Step 3 - NOT OK: it should seem simple to configure vlc for changing the source from file to webcam and to transmit it via ip.
So what I do is to operate as before only changing the source:
1. Menu "Media" --> Stream. That opens the "open media" window.
Here I click the tab capture device --> video device name --> I choose "integrated webcam"
Then all the rest is as before. But nothing happens. The receiver is black with nothing inside. Also If I try to save the flow from the receiver I only get a few bytes.
The afore mentioned settings for the transmitter are the following:
Capture device tab --> integrated webcam. Then the stream button --> the window opens correctly with dshow:\ --> next --> new destination RTP,MPEG transport stream ---> add button --> address = 127.0.0.1 port = 5004 StreamNAme=test
So the strange is that when I stream a file with whatever name from the transmitter, it immediately is recognized by the receiver. Instead try as I might, nothing happens when the webcam is the source.
That being said the problem above is not crucial to the solution.
What I care for is not how to use vlc stand alone but how to use it from my wpf application in order to send the webcam stream.
Thanks for any help
Patrick
I believe this sample should help you achieve your goal:
static void Main()
{
var currentDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
// Default installation path of VideoLAN.LibVLC.Windows
var libDirectory =
new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
using (var mediaPlayer = new Vlc.DotNet.Core.VlcMediaPlayer(libDirectory))
{
var mediaOptions = new[]
{
":sout=#rtp{sdp=rtsp://127.0.0.1:554/}",
":sout-keep"
};
mediaPlayer.SetMedia(new Uri("http://hls1.addictradio.net/addictrock_aac_hls/playlist.m3u8"),
mediaOptions);
mediaPlayer.Play();
Console.WriteLine("Streaming on rtsp://127.0.0.1:554/");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
You will need 2 nuget packages: Vlc.DotNet (C# wrapper) and VideoLAN.LibVLC.Windows (LibVLC library for Windows).

Play sound on laptop speaker, even if additional speakers are connected (but not listed in device list)

I'd like to play a soundfile from the speakers integrated in the laptop, even if an additional speaker is connected via the headphone jack (but it's not listed in the sound device list).
I've had a look at How can I make the computer beep in C#? and How to beep using PC speaker? and have understood that the beep driver was removed in windows 7.
However, I do not want to use this extremely simple integrated speaker controlled by the removed driver, but the normal one present in most modern laptops.
Unfortunately, when I connect my external speakers, they do not show up as an additional device. When I run this code (using NAudio), I always get just one result, and it's the same whether the external speakers are connected or not:
var enumerator = new MMDeviceEnumerator();
foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
Console.WriteLine(endpoint.FriendlyName);
}
Is it possible to play a sound on the normal laptop speaker, while an external speaker is connected?
Edit:
I don't think my question is a duplicate of Play a sound in a specific device with C#
I don't see an additional device when I connect my external speakers, so I guess my laptop doesn't have multiple sound cards (how can I verify?).
The solution from the other question (waveOut.DeviceNumber = deviceNumber;) doesn't work for me neither. When I choose deviceNumber = 0;, the sound comes from my external speaker if it's connected. If I set deviceNumber = 1; (while it's connected) I get a MmException
I see I'm a bit late for the party but i was working on something similar and got to this solution.
Hope it helps someone in the future :)
var enumerator = new MMDeviceEnumerator();
IWavePlayer waveOut = new WasapiOut();
foreach (var endpoint in enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active))
{
if (endpoint.FriendlyName == "Name of the device you want")
{
waveOut = new WasapiOut(
endpoint, AudioClientShareMode.Exclusive, false, 0);
AudioFileReader wave = new AudioFileReader("Audio Name");
waveOut.Init(wave);
waveOut.Play();
break;
}
}

C# - Windows Mobile - Pairing with Zebra RW 420

Update: This may not be "Pairing". This may just need to have a service started and bound to a port. However, this code is not storing it either. I need the device to be stored even after the application is closed.
I am building a program specifically suited for Zebra RW 420's on a Windows Mobile 6 Handheld device. The application needs to allow a mobile device to pair with the printer on COM1. I believe I am very close to getting it, but I can't get the pair request to work.
I am able to communicate with the printer and even print by directly connecting and printing, but I can't get the mobile device to actually pair with it. I've tried a variation of pins to include null, "1", "0000", and "1234". No matter what, the method always returns false. Any suggestions or ideas why this might be failing? I can pair the device just find in the WM6 Bluetooth menu, but not in my application.
It might be important to note that the little light bulb icon on the printer comes on when the program says it is attempting to pair, but after about 5 to 10 seconds, it fails.
BluetoothSecurity.PairRequest(device, "1"))
Additional Information:
I've successfully paired with my Android phone using this code.
I then logged in and set a PIN on the Zebra printer. However, this code still fails to pair with the printer even when I know the pin is correct / set in the printer.
From https://km.zebra.com/kb/index?page=answeropen&type=open&searchid=1336682809706&answerid=16777216&iqaction=5&url=https%3A%2F%2Fkm.zebra.com%2Fkb%2Findex%3Fpage%3Dcontent%26id%3DSO8031%26actp%3Dsearch%26viewlocale%3Den_US&highlightinfo=6292341,26,43#
Zebra Bluetooth enabled mobile printers are 'slave' devices only. The printers will pair with any 'master' device that tries to make a valid connection. Since only a master device can initiate a connection, the printer does not store pairing data, that function is always done on the master device. The printer can only be connected to one master device at a time, but any number of master devices that have stored pairing information for the printer would be able to initiate a connection to the printer without having to rediscover it.
I'm guessing that this means the InTheHand.Net BluetoothSecurity.PairRequest might not work for this type of pairing?
In the Bluetooth section of the WM handheld, under the "Devices" tab, I can add the device. I need to essentially do that. I need to register the device in that list and then set it to use COM 1 in the "COM Ports" section. The application I am using doesn't actually print. It's sole purpose is to prepare the printer for other applications.
The quote from Zebra make it sounds as pairing is actually not required at all. Are you printing from your app? If so just connect to the SPP service and send the text.
BluetoothAddress addr = ...
Guid serviceClass;
serviceClass = BluetoothService.SerialPort;
var ep = new BluetoothEndPoint(addr, serviceClass);
var cli = new BluetoothClient();
cli.Connect(ep);
Stream peerStream = cli.GetStream();
peerStream.Write ...
(From General Bluetooth Data Connections)
The Zebra Mobile Printer needed to be properly configured before pairing with this method will work. Here is what I did:
First, I ran the following commands on the printer:
.
! U1 setvar "bluetooth.authentication" "setpin"
! U1 getvar "bluetooth.authentication"
! U1 getvar "bluetooth.enable"
! U1 getvar "bluetooth.discoverable"
! U1 setvar "bluetooth.bluetooth_pin" "0000"
! U1 getvar "bluetooth.bluetooth_pin"
Then, the application with this code ran successfully.
.
int pair_req = 0;
try
{
if (BluetoothSecurity.SetPin(device, "0000")) {
while (status == false && pair_req < 3)
{
++pair_req;
status_box.Text = status_box.Text + '\n' + "Attempt " + pair_req.ToString();
status_box.Update();
if (BluetoothSecurity.PairRequest(device, "0000"))
{
status = true;
client.Refresh();
status_box.Text = "Paired Successfully.";
status_box.Update();
Thread.Sleep(5000);
}
else
{
status = false;
}
}
}
}
catch (ArgumentNullException e)
{
status_box.Text = "Pair failed.";
status_box.Update();
Thread.Sleep(5000);
}
status_box.Update();
Thread.Sleep(400);

Categories