问题
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.
回答1:
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);
}
回答2:
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.)
来源:https://stackoverflow.com/questions/27120882/list-and-connect-to-bluetooth-devices