Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过蓝牙BLE发送和接收数据?_C#_Arduino_Bluetooth Lowenergy - Fatal编程技术网

C# 如何通过蓝牙BLE发送和接收数据?

C# 如何通过蓝牙BLE发送和接收数据?,c#,arduino,bluetooth-lowenergy,C#,Arduino,Bluetooth Lowenergy,我刚刚开始使用arduino/bluetooth,现在我希望使用它,通过应用程序发送和接收命令 我目前正在使用一款蓝牙BLE设备,我想将其连接到iOS和android,但对于如何通过蓝牙BLE正确地向设备发送和接收数据(字节[]),我有些挠头 为了在应用程序和蓝牙/arduino之间发送和接收信息,我使用了ICharacteristic(我认为这是通过BLE发送数据的正确接口),但我不确定如何将其连接到我找到的设备 我会显示我的代码,这样你就可以清楚地看到我的意思 public class bl

我刚刚开始使用arduino/bluetooth,现在我希望使用它,通过应用程序发送和接收命令

我目前正在使用一款蓝牙BLE设备,我想将其连接到iOS和android,但对于如何通过蓝牙BLE正确地向设备发送和接收数据(字节[]),我有些挠头

为了在应用程序和蓝牙/arduino之间发送和接收信息,我使用了
ICharacteristic
(我认为这是通过BLE发送数据的正确接口),但我不确定如何将其连接到我找到的设备

我会显示我的代码,这样你就可以清楚地看到我的意思

public class bluetoothConnection
{
    public IAdapter thisAdapter { get; set; }
    public ICharacteristic thisCharacteristic {get; set;} 
}
我的连接函数,其中我通过设备名称和UUID连接到确切的设备。如果我发现了什么,那么我尝试发送数据的按钮将被启用并可用

public async void connect()
{
    await myConnection.thisAdapter.StartScanningForDevicesAsync();

    myConnection.thisAdapter.DeviceDiscovered += async (sender, e) =>
    {
            if (e.Device.Id.ToString().Equals ("00001101 - 0000 - 1000 - 8000 - 00805f9b34fb" && e.Device.Name == "HC-05"))
            {
                await myConnection.thisAdapter.ConnectToDeviceAsync(e.Device);
                sendCommandButton.IsEnabled = true; //so my button is enabled and that function is below
            }
    };
}
因此,如果我从我的arduino中找到我的蓝牙设备,并且现在我尝试向我的arduino发送信息,那么下面的这个按钮将被启用,但是我如何将
这个特性
连接到我刚才在上面找到的设备

byte[] byteText = Encoding.UTF8.GetBytes("send this textline");

void sendCommandToArduino(object s, EventArgs e)
{
    myConnection.thisCharacteristic.WriteAsync(byteText);
}
以下是我阅读arduino是否向应用程序发送任何信息的方式:

var info = myConnection.thisCharacteristic.ReadAsync();
var result = info.Result;
string textresult = Encoding.UTF8.GetString(result);
我当然会把它放在一个while循环中,不断地寻找数据


所以我的问题是:为了通过蓝牙(应用程序和BLE设备)发送数据,我是否使用
ICharacteristic
(使用我当前使用的nuget),如果是,我如何将
ICharacteristic
连接到我找到的设备,以便通过蓝牙BLE发送和接收数据?

如果您尚未这样做,查看您的Arduino设备文档,查看您需要写入/读取的特征。特征将是特定服务的“子项”。服务和特征都有UUID,可以用来引用它们

在您的移动应用程序上,当您的BLE设备连接时,您应该启动服务发现阶段。当您获得服务后,您可以搜索并获得对您的特征的引用。大致如下:

var service = await connectedDevice.GetServiceAsync(Guid.Parse("<service-uuid-here>"));
var characteristic = await service.GetCharacteristicAsync(Guid.Parse("<characteristic-uuid-here>"));

// ...

characteristic.WriteAsync(message);
var service=await connectedDevice.GetServiceAsync(Guid.Parse(“”);
var characteristic=await service.GetCharacteristicAsync(Guid.Parse(“”);
// ...
特征.WriteAsync(消息);

您是否在该特性上尝试了
StartUpdatesAsync()
方法?这应该把你与阅读和阅读的特点联系起来Writing@skar啊,好的,但我不需要以某种方式将我的ICharacteristics与我的设备连接起来吗?
ICharacteristics
如何知道将数据发送到哪个设备?或者当你连接到蓝牙时,它会自动识别吗?好的!非常感谢你的回答。解决了我的很多问题。我正在使用此模块:我必须阅读有关发送到此特定设备的内容!但我想这是一个特点。