C# Detect button press on bluetooth device in Windows Phone Project - c#

I have a Bluetooth stylus device. It has three buttons on it: power/call, volume up, volume down. In a C# project running on my windows phone paired with the stylus, how can I detect when one of those buttons is pressed?
Note: I already can see the device in code:
BluetoothDevice currentDevice { get; set; }
string deviceName = "P1";
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelector()))
{
BluetoothDevice bdDevice = await BluetoothDevice.FromIdAsync(di.Id);
if (bdDevice.Name == deviceName)
{
currentDevice = bdDevice;
//OK, so now what do I do to listen for the button clicks???
}
}
}

Related

EventHandler to get HDMI connectivity in windows application

I am looking for event handler to get the information when HDMI is connected or disconnected using c#/WPF like how myNetworkAvailabilityChangeHandler is used to detect when internet gots disconnected or connected.
There is no direct answer to this but you can use the SystemEvents to achieve this.
Code in C# :
main()
{
SystemEvents.DisplaySettingsChanged += new
EventHandler(SystemEvents_DisplaySettingsChanged);
}
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
{
int HDMI_Monitors = 0;
ManagementClass mClass = new ManagementClass(#"\\localhost\ROOT\WMI:WmiMonitorConnectionParams");
foreach (ManagementObject mObject in mClass.GetInstances())
{
var ss = mObject["VideoOutputTechnology"];
if(ss.ToString().StartsWith("5"))
{
int HDMIport = Convert.ToInt32(ss);
if (HDMIport == 5)
{
HDMI_Monitors += 1;
}
}
}
}
You can use the model class to keep on updating the HDMI status.
So every time your HDMI will connect or disconnect SystemEvents_DisplaySettingsChanged will trigger and then it will check the HDMI connection.

Bluetooth Radio.StateChanged Event fires twice

I want to listen when bluetooth radio turn on or turn off.
For this i simply got the default radio and subscribed to StateChanged.
var bluetoothAdapter = await BluetoothAdapter.GetDefaultAsync();
var radio = await bluetoothAdapter.GetRadioAsync();
radio.StateChanged += Radio_StateChanged;
private void Radio_StateChanged(Windows.Devices.Radios.Radio sender, object args)
{
Console.WriteLine(String.Format("Radio state changed {0} {1}", sender.Kind, sender.Name));
}
When i change bluetooth state via Windows Bluetooth settings i got StateChanged event fires twice.
Output:
Radio state changed Bluetooth Bluetooth
Radio state changed Bluetooth Bluetooth
I have the module with Bluetooth and Wifi together. I thought maybe it is cause. Then i get all radios Radio.GetRadiosAsync
radios = await Radio.GetRadiosAsync();
foreach (var r in radios)
{
r.StateChanged += Radio_StateChanged;
}
Output:
Radio state changed Bluetooth Bluetooth
Radio state changed Bluetooth Bluetooth
Radio state changed WiFi Wi-Fi
Radio state changed WiFi Wi-Fi
is this the expected behavior?
Update. I added state value to output
Output:
Radio state changed Bluetooth Bluetooth Off
Radio state changed Bluetooth Bluetooth Off
This is probably a bug of Radio.StateChanged API. I have also faced the same problem. You can minimize the problem by checking the Radio State like bellow :
public Radio BluetoothAdapter { get; set; }
public RadioState OldBluetoothState;
public RadioState NewBluetoothState;
IReadOnlyList<Radio> radios = await Radio.GetRadiosAsync();
BluetoothAdapter = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
if (BluetoothAdapter != null)
{
OldBluetoothState = BluetoothAdapter.State;
BluetoothAdapter.StateChanged += RadioBluetoothAdapter_StateChanged;
}
private void RadioBluetoothAdapter_StateChanged(Radio sender, object args)
{
try
{
NewBluetoothState = sender.State;
if (NewBluetoothState != OldBluetoothState)
{
OldBluetoothState = NewBluetoothState;
if (sender.State == RadioState.Off)
{
//Application logic will goes here.
Debug.WriteLine("Bluetooth has been turned off.");
}
else
{
//Application logic will goes here.
Debug.WriteLine("Bluetooth has been turned on.");
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception has been occured: {ex.Message.ToString()}");
}
}

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

Working with MIDI in Windows Store App (Win 8.1)

My goal is to receive MIDI messages in Windows Store Apps.
Microsoft has delivered an API called Microsoft.WindowsPreview.MidiRT (as a nuget package).
I managed to get a midi port, but MessageReceived event is not arised, although I'm pressing keys on my MIDI keyboard, and other MIDI programs show me that PC receives these messages.
Here is my code:
public sealed partial class MainPage : Page
{
private MidiInPort port;
public MainPage()
{
this.InitializeComponent();
DeviceWatcher watcher = DeviceInformation.CreateWatcher();
watcher.Updated += watcher_Updated;
watcher.Start();
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
port.Dispose();
}
async void watcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(MidiInPort.GetDeviceSelector());
foreach (var item in deviceCollection)
{
Debug.WriteLine(item.Name);
if (port == null)
{
port = await MidiInPort.FromIdAsync(item.Id);
port.MessageReceived += port_MessageReceived;
}
}
}
void port_MessageReceived(MidiInPort sender, MidiMessageReceivedEventArgs args)
{
Debug.WriteLine(args.Message.Type);
}
}
Any ideas?
Possibly-related: Your device watcher code is not following the normal pattern. Here is what you need to do:
DeviceWatcher midiWatcher;
void MonitorMidiChanges()
{
if (midiWatcher != null)
return;
var selector = MidiInPort.GetDeviceSelector();
midiWatcher = DeviceInformation.CreateWatcher(selector);
midiWatcher.Added += (s, a) => Debug.WriteLine("Midi Port named '{0}' with Id {1} was added", a.Name, a.Id);
midiWatcher.Updated += (s, a) => Debug.WriteLine("Midi Port with Id {1} was updated", a.Id);
midiWatcher.Removed += (s, a) => Debug.WriteLine("Midi Port with Id {1} was removed", a.Id);
midiWatcher.EnumerationCompleted += (s, a) => Debug.WriteLine("Initial enumeration complete; watching for changes...");
midiWatcher.Start();
}
I managed to make it work. I've changed the platfrom to x64, and now it works (I used to build it for x86). There is still a problem though (and it is even bigger): I want to integrate this with Unity3d, but Unity3d doesn't allow to build x64 windows apps, on the other hand x86 MIDI build doesn't work on x64 machines.
Added:
Although this API depends on your architecture, a new Windows 10 api reportedly does not, so it should be simpler, if you target Win10.

In-app search in Windows Phone 8.1

I am new to Windows Phone 8.1. I have to implement "Search" functionality wherein I have to search the text file and update list view in my app. Also I have noticed the searchbox and searchresultspage in windows Store 8.1 apps but it's not available on windows phone 8.1. Any idea how shall I do it?
Update: (added code)
public List<DataArticle> matchedarticles { get; set; }
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
var queryText = e.NavigationParameter as String;
var folders = await MHDataSource.GetFoldersAsync();
foreach(var folder in folders)
{
var art = folder.Articles.Where(
article => article.Title.IndexOf(
queryText,StringComparison.CurrentCultureIgnoreCase) > -1);
matchedarticles = art.ToList();
}
this.defaultViewModel["Results"] = matchedarticles;
}
How can I retrieve the articles and assign it to the list(matchedarticles) based on the querytext??

Categories

Resources