I'm tryiing to make my Windows tablet app communicate with an other device via Bluetooth.
First I want to scan for devices, then i want to connect to the choosen device.
I've made a simple test app: Blank front page and added a button and a listboxto it. Then I've tried the following code witch I've forund else where here on SO:
ListBox1.Items.Clear();
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
foreach (var device in devices)
{
ListBox1.Items.Add(device);
}
But the list is just empty
Then I've tried to just enum devices and filtered out unwanted devices:
var list = await DeviceInformation.FindAllAsync();
var uniqueList = new HashSet<string>();
var terminators = new List<string>() { "Audio", "Mixer", "Mic", "Realtek", "Usb", "Gmail,", "Line in", "Lyd", "Display", "surface", "#" };
foreach (var element in list)
{
var strToken = element.Name.ToUpper();
if (!uniqueList.Add(strToken))
continue;
var contains = false;
foreach (var word in terminators)
if (strToken.Contains(word.ToUpper()))
contains = true;
if (!contains)
ListBox1.Items.Add(element.Name);
}
But that doesn't give any meanfull list.
I have a feeling I'm doing ti wrong. Please helt me back on track.
I've just got a little wiser, ive forund an other SO question telling me that it is not posible.
Search and Connect to Bluetooth device in Windows 8/8.1 Store apps?
So the solution for listing is:
1) Pair your devices
2) List them :
ListBox1.Items.Clear();
var devices = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
foreach (var device in devices)
{
ListBox1.Items.Add(device);
}
Have you set the device capabilities yet? You have to define the Id and Function type yourself.
Useful link: How to set device capabilities.
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>
</m2:Device>
</m2:DeviceCapability>
Furthermore, it is true that you cannot connect unpaired devices. (Windows appears to not support it.)
Related
I'm fairly new to C#. In my program, I can measure the masterpeek value of the computer. But what I need right now is only the masterpeek value of a particular application, rather than the masterpeek value of the computer. For example chrome.exe masterpeek value.
I'm importing audio output devices into ComboBox.
var deviceEnum = new MMDeviceEnumerator();
var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
foreach (var dev in devices)
{
comboDevices.Items.Add(dev.FriendlyName);
}
comboDevices.SelectedIndex = 0;
deviceEnum.Dispose();
I choose the output device.
using (MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator())
{
var deviceList = deviceEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active).ToList();
device = deviceList[comboDevices.SelectedIndex];
}
I can show the masterpeek value with the progressbar.
if (comboDevices.SelectedItem != null)
{
var mpValue = Math.Round(device.AudioMeterInformation.MasterPeakValue * 100);
progressAudioMeter.Value = Convert.ToInt32(mpValue);
}
But now what I want to do exactly is the volume value of a particular application instead of the total volume value. For example chrome.exe
An example image from the audio mixer
I did some research and couldn't find exactly what I wanted. Can you help me? I don't know how I can do this.
Good day,
in android phone -> virtual keyboard, system list all the enabled keyboards in this page, how can i get all enabled keyboards type with C# in my Xamarin.forms APP?
Thanks!
Roll
In Android, you could use InputDevice.
InputDevice: https://developer.android.com/reference/android/view/InputDevice
You could try the code below:
int[] devicesIds = InputDevice.GetDeviceIds();
foreach (var item in devicesIds)
{
//Check the device you want
InputDevice device = InputDevice.GetDevice(item);
//device.getName must to have virtual
var s = device.Name;
var b = device.KeyboardType;
}
You could use DependencyService to call this in Xamarin.Forms.
DependencyService: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
thanks your reply, i try the way you suggested, but the returned value was not i want...
i found another post here, use that way and finally get the things i want,
here is my code to share:
InputMethodManager manager = (InputMethodManager)context.GetSystemService(Context.InputMethodService);
IList<InputMethodInfo> mInputMethodProperties = manager.EnabledInputMethodList;
IEnumerator<InputMethodInfo> imi = mInputMethodProperties.GetEnumerator();
while (imi.MoveNext())
{
InputMethodInfo inputInfo = imi.Current;
var iN = inputInfo.ServiceName;
}
Best Regards & thanks
Roll
I was trying to print to an usb printer using usbmanager, the App can detect the printer device but when i run it doesnt print. there are no errors and all passing data is ok.
Printer : Bixolon SRP 275III
Type: USB
private async void printReciept()
{
UsbManager m_usbManager;
m_usbManager = (UsbManager)Application.Context.GetSystemService(Context.UsbService);
var deviceList = m_usbManager.DeviceList;
IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
UsbDevice m_usbdevice = null;
if (deviceIterator.Count() > 0)
{
var device = deviceIterator.ElementAt(0);
m_usbdevice = device;
string ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
var mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
m_usbManager.RequestPermission(m_usbdevice, mPermissionIntent);
UsbDeviceConnection deviceConnection = null;
try
{
using (var usbInterface = m_usbdevice.GetInterface(0))
{
using (var usbEndpoint = usbInterface.GetEndpoint(0))
{
mEndPoint = usbEndpoint;
deviceConnection = m_usbManager.OpenDevice(m_usbdevice);
byte[] bytesHello = Encoding.UTF8.GetBytes("Hello");
deviceConnection.BulkTransfer(usbEndpoint, bytesHello, bytesHello.Length, 0);
}
}
}
catch
{
}
}
}
You are sending the string to be printed directly to the bulk endpoint, or actually you are doing bulk transfer to the first endpoint found without knowing any of it's characteristics? I think it is a bit more complex than that.
First try to find out whether your printer supports USB printing class or some proprietary implementation. You can do this easily e.g. by connecting the printer to Windows PC and looking from the device manager, usbdeview or some other similar application.
If it supports printing class, read this document and implement your driver based on that (or use the one you may already have in Android). If it only supports proprietary implementation, you need to get the specifications for it or do some reverse engineering.
You may need to learn about PCL which may also be needed.
Is there a way to find out whether a microphone/recording device is currently being used by an application in Windows?
I am aware that by using NAudio one can easily get a list of all recording devices as follows:
public void getAudioDevices()
{
DeviceNameByID = new Dictionary<string, string>();
var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();
// Allows you to enumerate rendering devices in certain states
var endpoints = enumerator.EnumerateAudioEndPoints(
DataFlow.Capture,
DeviceState.Active);
foreach (var endpoint in endpoints)
{
float volumeLevel = endpoint.AudioEndpointVolume.MasterVolumeLevelScalar;
Debug.WriteLine($"Volume: {volumeLevel}");
DeviceNameByID[endpoint.ID] = endpoint.DeviceFriendlyName;
Debug.WriteLine($"{endpoint.ID}: {endpoint.DeviceFriendlyName} - {endpoint.State}");
}
NotificationClient nClient = new NotificationClient();
// Aswell as hook to the actual event
enumerator.RegisterEndpointNotificationCallback(nClient);
}
but I am not sure as to how/if one can get any knowledge on their current usage.
I am trying to get all usb devices(including portable devices) on Windows 7
now I searched all over and didnt find a good answer.
I tried this code:
static void Main(string[] args)
{
//
// Get an instance of the device manager
//
PortableDeviceApiLib.PortableDeviceManagerClass devMgr
= new PortableDeviceApiLib.PortableDeviceManagerClass();
//
// Probe for number of devices
//
uint cDevices = 1;
devMgr.GetDevices(null, ref cDevices);
//
// Re-allocate if needed
//
if (cDevices > 0)
{
string[] deviceIDs = new string[cDevices];
devMgr.GetDevices(deviceIDs, ref cDevices);
for (int ndxDevices = 0; ndxDevices < cDevices; ndxDevices++)
{
Console.WriteLine("Device[{0}]: {1}",
ndxDevices + 1, deviceIDs[ndxDevices]);
}
}
else
{
Console.WriteLine("No WPD devices are present!");
}
}
but i get this error:
interop type 'portabledeviceapilib.portabledevicemanagerclass' Cannot
be embedded
Now im pretty stuck.
If you could help me with this code/ give me an idea what should i try, ill be happy
all I need is to get which type of USB got connected,
if a phone is connected, or a mouse. i want to know what is connected.
Thanx Ahead
I am using the NuGet package PortableDevices (which is based on the tutorial by Christophe Geers).
Derived from part one of the tutorial:
public void ListDevices()
{
var devices = new PortableDeviceCollection();
devices.Refresh();
foreach (var device in devices)
{
device.Connect();
Console.WriteLine(#"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
device.Disconnect();
}
}
To expand on #CodeFox's answer, and in order to make his code ListDevices() work:
Download the NuGet package PortableDevices
Add references to these 4 COM libraries:
PortableDeviceClassExtension
PortableDeviceConnectApi
PortableDeviceTypes
PortableDeviceApi
Take the dll's under obj\Debug and put them into bin\Debug:
Interop.PortableDeviceClassExtension.dll
Interop.PortableDeviceConnectApiLib.dll
Interop.PortableDeviceTypesLib.dll
Interop.PortableDeviceApiLib.dll
Now you can use this function, although FriendlyName does not seem to be working (it returns an empty string):
private IDictionary<string, string> GetDeviceIds()
{
var deviceIds = new Dictionary<string, string>();
var devices = new PortableDeviceCollection();
devices.Refresh();
foreach (var device in devices)
{
device.Connect();
deviceIds.Add(device.FriendlyName, device.DeviceId);
Console.WriteLine(#"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
device.Disconnect();
}
return deviceIds;
}
The next step for me is getting the contents from the device, which is done like so:
var contents = device.GetContents();