C# - Force windows around 20s delay to realize bluetooth not connected - c#

I'm currently using the 32feet library for bluetooth in my application. I'm trying to check whether the device is still alive before it takes windows the 20s to change the status from "Connected" --> "Paired".
"True" --> "False"
I'm forcing a device refresh with:
holders.storedDevice.Refresh();
but it seems that the refresh isn't actually forcing windows to do anything. For reference storedDevice is of type
BluetoothDeviceInfo name {get; set;}
Timer code:
private void refreshDevice(object Sender, EventArgs e)
{
holders.StoredDevice.Refresh();
if(!holders.StoredDevice.Connected)
{
deviceRefreshTimer.Enabled = false;
updateTextBox( connectButton ,"Disconnecting...");
ExecuteSecure(() => connectButton.Enabled = false);
updateTextBox(statusTextBox, "Lost connection");
readBackgroundWorker.CancelAsync();
holders.localClient.Close();
storedStream = null;
}
}

Related

Xamarin Forms Bluetooth LE not showing nearby discoverable devices

I am making an app that utilizes bluetooth function such as scanning devices etc. I checked the scan flag and returns true but not showing the discoverable device that I am testing.
I am using Samsung J7 Pro as my app test device and Samsung J7 as the device I want to see in the list of discovered devices.
J7 already set as discoverable and with bluetooth ON.
I based my codes in Monkey.BluetoothLE
Here is what I have:
Declarations
ObservableCollection<BluetoothViewModel> vm = new ObservableCollection<BluetoothViewModel>();
Android.Bluetooth.BluetoothManager _blManager;
Android.Bluetooth.BluetoothManager _blManager;
Robotics.Mobile.Core.Bluetooth.LE.Adapter _bleAdapter;
Functions
public BluetoothPage()
{
InitializeComponent();
lvInfo.ItemsSource = vm;
var appContext = Android.App.Application.Context;
_blManager = (Android.Bluetooth.BluetoothManager)appContext.GetSystemService("bluetooth");
_blAdapter = _blManager.Adapter;
_bleAdapter = new Robotics.Mobile.Core.Bluetooth.LE.Adapter();
_bleAdapter.DeviceDiscovered += _bleAdapter_DeviceDiscovered;
_bleAdapter.ScanTimeoutElapsed += _bleAdapter_ScanTimeoutElapsed;
}
private void btnScanStopBluetooth_Click(object sender, EventArgs e)
{
if (!_bleAdapter.IsScanning)
{
if (!_blAdapter.IsEnabled)
{
_blAdapter.Enable();
DisplayInformation("Turning on bluetooth...");
while (!_blAdapter.IsEnabled)
{
//do nothing until enabled
}
}
vm.Clear();
btnScan.Text = "Stop Scan";
_bleAdapter.StartScanningForDevices();
}
else
{
btnScan.Text = "Start Scan";
_bleAdapter.StopScanningForDevices();
}
}
private void _bleAdapter_DeviceDiscovered(object sender, Robotics.Mobile.Core.Bluetooth.LE.DeviceDiscoveredEventArgs e)
{
count++;
vm.Add(new BluetoothViewModel
{
Name = e.Device.Name,
ID = e.Device.ID.ToString(),
RSSI = e.Device.Rssi.ToString()
});
}
private void _bleAdapter_ScanTimeoutElapsed(object sender, EventArgs e)
{
DisplayInformation("Scan Timeout");
_bleAdapter.StopScanningForDevices();
btnScan.Text = "Start Scan";
}
private void DisplayInformation(string line)
{
lblStatus.Text = line;
}
A listview is bound to "vm" that will display the discovered device.
It does not show anything, and count is always zero but I checked the scan flag using _bleAdapter.IsScanning, it returns true.
EDIT:
I tried other open-source sample programs for Bluetooth such as
xamarin-bluetooth-le (BLE Explorer)
Bluetooth-Xamarin.Forms (DemoBluetooth)
None of them seem to list the device. When I use my built-in bluetooth app under settings, it lists the device. What am I missing here?
Have you granted permission for bluetooth and location?
You have to grant permission in the Manifest/or Settings and depending on the sdk (23+) also asking the user for extra permission.
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/permissions?tabs=windows

c# with dotras - Status Box not updating automatically after dial connect/disconnect

Greeting,
OS: Windows 7 /64bit
Application: Visual Studio 2012 / C# and DotRas 1.3 Library
I am very new to c# or VS thing so please bear with me. After doing many hours R&D, I have finaly made a pppoe dialer program in C# / Dotras. This program have 3 main buttons
Create /Add PPPoE Internet Dialer Connection in network connections , working fine
Dial button, which connects the newly created dialer , working Fine
Disconnect working fine
I have added StatuBox where dialup events should appear as showed on dotras youtube video tutorial (which was for vpn, but my project is for pppoe dialer)
StatusBox Not updating the dialup events like connecting/password error/connected etc. This is the part where I am finally confused.
Following is my Code.
// Dial Button Action
private void button2_Click_1(object sender, EventArgs e)
{
using (RasDialer dialer = new RasDialer())
{
// I had to add below line to update statusTextBox Manualy , want to get rid of it by adding auto status update
this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connection in progress ...", "{0}\r\n\r\n"));
dialer.EntryName = ("pppoe2");
string username = textBox1.Text;
string passwd = textBox2.Text;
// If username is empty dont connect
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Cancelled. Cannot continue with username/password.", "{0}\r\n"));
MessageBox.Show("Enter username.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
dialer.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User);
dialer.Timeout = 1000;
dialer.AllowUseStoredCredentials = true;
// start dialing,
dialer.Dial();
// If dialer connects successfully update StatuTextbox
this.StatusTextBox.AppendText(string.Format("{0}\r\n\r\n", "Connected."));
}
}
private void rasDialer1_StateChanged(object sender, StateChangedEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "Status Changed"));
}
private void rasDialer1_Error(object sender, System.IO.ErrorEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}
private void rasDialer1_DialCompleted(object sender, DialCompletedEventArgs e)
{
this.StatusTextBox.AppendText(string.Format("{0}\r\n", "STATUS UPDATE TEXT XYZ"));
}
Any help would be highly appreciable.
Ok I have managed to enable status box text append work fine.
this.Invoke((MethodInvoker)delegate
{
this.StatusTextBox.AppendText(string.Format(e.State.ToString() + "\r\n"));
});
}
Depending on how you were using it, if the background thread from the OS isn’t marshaled back to the UI thread it won’t update. Similarly to what you had done, the SynchronizingObject property on the RasDialer can be set to your form and the thread synchronization will occur automatically.

How to store data through web service when data coming to my serial port will be asynchronous?

I have one device "installed" on a users desk (a desk is nothing but a chair or table on which user will sit), and I will be supporting thousands of desks.
A user will have one "chip" and the user will scan this chip on the device which is installed on their desk.
The device will read the data off the chip and will send it to my laptop which will also have one of the devices installed, except this device is the main device responsible for collecting all user scan chip data.
All the data will be routed to my device via a wifi router and I will listen to this from my Main device and read data from this device from my laptop via serial port connection.
This data sending will happen as each user number scans his/her chip.
I have created a windows form application which will continuously run in the background on my laptop, and will be listening to my serial port on which main device is connected.
This is my code taken from here: Source Code Reference:
public partial class MainUI : Form
{
SerialPortManager _spManager;
public MainUI()
{
InitializeComponent();
UserInitialization();
}
}
private void UserInitialization()
{
_spManager = new SerialPortManager();
_spManager.NewSerialDataRecieved += new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved);
this.FormClosing += new FormClosingEventHandler(MainUI_FormClosing);
}
private void MainUI_Load(object sender, EventArgs e)
{
_spManager.StartListening()
}
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
{
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
}
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
//data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
if (!string.IsNullOrEmpty(str))
{
//Here i will store that data in to my database through web service.
//What i should use whether WCF service or Web Api because data will be continuos like at a
//time more than 10 or 100 user can scan data at the same time so this event will be fired continuously.
//I am using entity framework to store data in to my database and how to ansynchornously call web service to store my data
//so that my call doesnt block incoming data to serial port
}
}
My main concern is I will have numerous users who will scan data at the same time and how I will handle when more than 10 or 100 user scan the data at the same time.
How can I mitigate this potential issue?
Ok, if i got the question right you need to do something like this ...
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to api
string str = Encoding.ASCII.GetString(e.Data);
if (!string.IsNullOrEmpty(str))
{
var api = new HttpClient();
api.BaseUrl("http://somewhere.com");
api.PostAsJsonAsync("api/Something", str)
}
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port,
// and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(
_spManager_NewSerialDataRecieved), new object[] { sender, e
});
return;
}
}
// i think this can go completely ...
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
That posts the data to webapi but whilst that post is taking place on another thread the serial port can carry on receiving data
close your serial port and load every some-amount-of-time. After that some-amount-of-time open the port and scan all devices, then close it again.
public void MainUI.Load(Object sender, Eventargs e)
{
if (_spmanager != null && !_spManager.IsOpen)
//*write the code here where it opens and starts listening
_spmanager.StartListening();
//*write the code here where it waits a little bit then
_spmanager.Close();
}
Therefore everytime it loads it starts when the port is closed, it opens for a little bit, scans whatever values are true and then closes again.
I am not very sure about this but it is just an idea of how to handle it. The code might not be accurate or currect I just wrote it quickly. Take the idea from this

Enumerating IPP Printers

I'm looking into DNS based service discovery in Windows 10 and found this video from Build. The only change I made to discovery was changing the service name to "_ipp._tcp". However I don't get any hits even though I know I have > 15 IPP enabled printers on the network (I can successfully identify these using the ipptool, IOS and Android code).
I've checked and double checked for typos. I've included all networking capabilities in the appxmanifest file.
Here's my code, pretty straight forward:
public sealed partial class MainPage : Page
{
static Guid DnsSdProtocol = new Guid("{4526e8c1-8aac-4153-9b16-55e86ada0e54}");
string queryString = "System.Devices.AepService.ProtocolId:={" + DnsSdProtocol + "} AND " +
"System.Devices.Dnssd.Domain:=\"local\" AND System.Devices.Dnssd.ServiceName:=\"_ipp._tcp\"";
DeviceWatcher watcher;
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
watcher = DeviceInformation.CreateWatcher(queryString,
new String[]
{
"System.Devices.Dnssd.HostName",
"System.Devices.Dnssd.ServiceName",
"System.Devices.Dnssd.TextAttributes",
"System.Devices.IpAddress" },
DeviceInformationKind.AssociationEndpointService
);
watcher.Added += Watcher_Added;
watcher.Start();
}
private void Watcher_Added(DeviceWatcher sender, DeviceInformation args)
{
System.Diagnostics.Debug.WriteLine("found device");
}
}
Does anybody else have experience with this and can help figure out why no devices are found? Is my query correct? Do I need to do anything else when setting up the DeviceWatcher?
update
I've verified the requests are being created since they are showing up in Wireshark. They look to be identical to other mdns requests being created. I've also verified I can create SSDP requests that return discovered devices so I doubt it's an issue with networking permissions via app capabilities.
Try using the watcher.Updated handler. Even if it is empty, the presence of an Updated handler can cause the Added handler to be triggered.

serialport connection for unknown device

I've coin machine acceptor set and I wanna connect this machine using serialport. My main problem is, I did try almost every setting to connect that machine. The pin numbers are written on the cable as 3th and 7th. So I try
private void Form1_Load(object sender, EventArgs e)
{
// SerialPort paraPort defined at designer's generated code
paraport = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
//I wanna access to windows controls from the thread
System.Windows.Forms.Form.CheckForIllegalCrossThreadCalls = false;
}
private void button2_Click(object sender, EventArgs e)
{
//paraPort is the name of serialport class
paraPort.ReadBufferSize = 1024;
paraPort.WriteBufferSize = 1024;
paraPort.ReadTimeout = 1000;
paraPort.WriteTimeout = 1000;
paraPort.NewLine = "\n";
//Because 7th pin is for RTS which means request 2 send
paraPort.Handshake = Handshake.RequestToSend;
//Data Terminal Ready Enable
paraPort.DtrEnable = true;
paraPort.RtsEnable = true;
paraPort.Open();
//Then Thread check the procedure inside of try - catch block
try{
// Thread money defined at designer's generated code
money = new Thread(new ThreadStart(CheckTheMachineState));
money.Start();
}catch(Exception e){
MessageBox.Show("thread cannot be created"+e.Message);
}
}
private void CheckTheMachineState()
{
richTextBox1.AppendText("Thread is running\n");
//I wanna get the value of IOCTL_SERIAL_WAIT_ON_MASK
//But I still don't know how
}
}
The machine is working well. But when I use paraPort.ReadBufferSize property, it gives me 0 when the coin accept :S. When I use the paraPort.Read method it throws an timeout exception :\
So What can I do for this stuff ? I'm using portmon tools to catch the coin machine signal.
IOCTL_SERIAL_WAIT_ON_MASK value is changed as SUCESS when I put the coin. How can I catch this value ?
After several days of work, I have figured out how to connect a coin acceptor/validator machine to a PC via serial port using VB6, such that every coin inserted in the slot will trigger a signal that will be caught by the PC.

Categories

Resources