我正在 Windows-10 笔记本电脑上的 VisualStudio 2015 中使用C#for BluetoothLE创建客户端应用。
我使用 Windows.Devices.Bluetooth.GenericAttributeProfile 时遇到问题,问题是我的代码有编译错误,说 GattDeviceServicesResult 无法找到。
- >我已经添加了Valdimir Postel的包 UwpDesktop 10.0.14393.3 ...(在安装此版本之前"使用Wndows.Devices.Bluetooth"无效)
- >然后,当我尝试打开其中一个示例时,我添加了VisualStudio推荐的SDK,Windows工具包(所以我接受了构建该项目的建议和VS安装的大约9GB的包)
- &GT;现在我可以使用一些蓝牙API,我可以扫描并连接到BLE设备,但我不能使用类来处理服务和特性,因为 GattDeviceServicesResult 和<找不到强> GattCharacteristicsResult 类型。虽然这些在MSDN网站上提到
- &GT;在论坛中搜索我才知道我需要添加一个参考 System.Runtime.WindowsRuntime.dll ,我通过添加VS的参考实用程序浏览到正确的文件夹,我试图添加这个并且它确实什么都没有,在我选择了dll后点击“添加”#39;没有任何反应。 (添加引用不是添加此dll)。
例如,如果我选择其他dll并尝试添加,那就可以了!
有人可以帮我解决这个问题,
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Bluetooth.Advertisement;
Int16 uuid_count = 0;
BluetoothLEAdvertisement[] ble_adv = new BluetoothLEAdvertisement[5];
BluetoothLEAdvertisementReceivedEventArgs[] ble_received_adv = new BluetoothLEAdvertisementReceivedEventArgs[5];
BluetoothLEDevice bluetooth_LE_Device;
GattDeviceServicesResult result_service;// This line does not compile
//Error: CS0246 the type name 'GattDeviceServicesResult ' could not be found
// I am adding reference to "System.Runtime.WindowsRuntime" as mentioned in some solutions
// the reference does not seems to be added at first when I click add button, but I can see the reference dll being mentioned in solution explorer (assuming it's been added)
// using this to scan available devices
private void scann_ble()
{
var watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.AdvertisementFilter.Advertisement.ServiceUuids.Clear();
watcher.Start();
while (true)
{
Thread.Sleep(10000);
break;
}
watcher.Stop();
}
// receiver event to collect addresses of available devices
private void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
bool update_adv = true;
Int16 i = 0;
if(uuid_count < 5)
{
if (uuid_count > 0)
{
while (i < uuid_count)
{
if (ble_received_adv[i].BluetoothAddress == args.BluetoothAddress)
update_adv = false;
i++;
}
}
if(update_adv != false)
ble_received_adv[uuid_count++] = args;
}
}
// now connecting and checking available services
// as per "https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/gatt-client"
private async void BLE_connect_button_Click(object sender, EventArgs e)
{
int i = 0;
i = BLE_device_grid_view.CurrentCell.RowIndex; // getting index from item selected in gridView
bluetooth_LE_Device = await BluetoothLEDevice.FromBluetoothAddressAsync(ble_received_adv[i].BluetoothAddress);
// Connection works fine, I can see it on my peripheral device
//get services - This is not working
result_service = bluetooth_LE_Device.GetGattServicesAsync();
if (result_service.Status == await GattCommunicationStatus.Success)
{
var services = result_service.Services;
// ...
}
}
我正在使用UwpDesktop包。