gpt4 book ai didi

c# - 如果我每次都没有取消配对我的蓝牙 LE 设备,为什么我不能发送消息? (UWP)

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:59 25 4
gpt4 key购买 nike

我目前正在开发一个应用程序,它会向蓝牙 LE 设备发送消息。第一次启动时一切正常,但第二次启动时出现异常。

应用代码

getDevices() 函数

 public async Task<List<string>> getDevices()
{
Debug.WriteLine("C");
foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("0000ffe0-0000-1000-8000-00805f9b34fb"))))
{
Debug.WriteLine("D");
Debug.WriteLine(di.Name);
Debug.WriteLine(di.Id);
Debug.WriteLine(di.IsEnabled);
GattDeviceService bleService = await GattDeviceService.FromIdAsync(di.Id);
Debug.WriteLine(bleService.Device.ConnectionStatus.ToString());
Debug.WriteLine("E");
Debug.WriteLine(GattCharacteristic.ConvertShortIdToUuid(0xFFE1).ToString());
// TODO: Throws exception if not paired before startup because pairing info is not stored on device
accConfig = bleService.GetCharacteristics(GattCharacteristic.ConvertShortIdToUuid(0xFFE1))[0];
Debug.WriteLine("F");
}
return deviceList;
}

发送消息()函数

 public async void sendMessage(string messageToSend)
{
// TODO: implement
await accConfig.WriteValueAsync((Encoding.UTF8.GetBytes(messageToSend)).AsBuffer());
Debug.WriteLine("G");
}

错误

在 await accConfig 行我得到一个异常:

An exception of type 'System.Exception' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: [...] (Exception from HRESULT: 0x80070572)

另一件事是,在“设置”选项卡上,我的蓝牙设备一直从“已连接”切换到“已配对”。我不知道是什么原因造成的。

更新 2017/04/23设备代码和信息

我将代码包含在 Arduino 上。我使用的是原装 HM-10,on this page第一个版本在上面。我的是无品牌的(因此它没有 Keyes Logo )但除此之外应该没有任何区别。 (虽然它可能是一个克隆,但据我所知我对此表示怀疑)。

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(9, 10); // RX, TX
char commandbuffer[50];
int j = 0;
void setup()
{

memset(commandbuffer, 0, sizeof(commandbuffer));
analogWrite(12, 255);
analogWrite(11, 0);
// Start the hardware serial port
Serial.begin(19200);
bluetooth.begin(9600);
// un REM this to set up a Master and connect to a Slave

Serial.println("BLE CC41A Bluetooth");
Serial.println("----------------------------------");
Serial.println("");
Serial.println("Trying to connect to Slave Bluetooth");
delay(1000);
bluetooth.print("AT"); // just a check
delay(2000);

delay(2000);
bluetooth.print("AT+ROLE0");
delay(2000);
/*
bluetooth.println("AT+HELP");
delay(2000);
bluetooth.println("AT+CONA0xE4F89CF8AB87");*/
/*bluetooth.println("AT+CONE4F89CF8AB87");
delay(2000);*/
// discover

/*
bluetooth.println("AT+INQ"); // look for nearby Slave
delay(5000);
bluetooth.println("AT+CONN1"); // connect to it */
}
void loop()
{
bluetooth.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
while (bluetooth.available() > 0) {
char inByte = bluetooth.read();
Serial.write(inByte);
}
// Read user input if available.
if (Serial.available()) {
delay(10); // The DELAY!
char temp = Serial.read();
if (temp == '\n')
{
bluetooth.print(commandbuffer);
Serial.print('\n'); // Because it needs it even if Bluetooth received command happened earlier
Serial.println(commandbuffer);
memset(commandbuffer, 0, sizeof(commandbuffer));
j = 0; // Reset
}
else
{
commandbuffer[j++] = temp;
}
delay(500);
}

}

最佳答案

如果配对信息未存储在设备上,则总是会发生这种情况。每次重新连接时,您都必须取消配对并重新配对,因为在 Windows 10 上,您的设备已配对,但您的设备未配对。

关于c# - 如果我每次都没有取消配对我的蓝牙 LE 设备,为什么我不能发送消息? (UWP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com