Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/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# 32ft.NET向蓝牙设备写入和读取字节_C#_Bluetooth_32feet - Fatal编程技术网

C# 32ft.NET向蓝牙设备写入和读取字节

C# 32ft.NET向蓝牙设备写入和读取字节,c#,bluetooth,32feet,C#,Bluetooth,32feet,我在手动库中使用时遇到问题,无法写入字节并从蓝牙设备读取数据 下面是我使用的简单代码 我发现设备,我配对设备,我创建本地客户端连接,我通过 BluetoothEndPoint endPoint = new BluetoothEndPoint(addr, BluetoothService.SerialPort,1); localClient.Connect(endPoint); 在我将字节写入设备之前,一切都正常,当我尝试这样做时,红色led应该会打开设备,但什么都没有发生……因此我认为字节没有

我在手动库中使用时遇到问题,无法写入字节并从蓝牙设备读取数据 下面是我使用的简单代码

我发现设备,我配对设备,我创建本地客户端连接,我通过

BluetoothEndPoint endPoint = new BluetoothEndPoint(addr, BluetoothService.SerialPort,1);
localClient.Connect(endPoint);
在我将字节写入设备之前,一切都正常,当我尝试这样做时,红色led应该会打开设备,但什么都没有发生……因此我认为字节没有被写入

无论如何,代码将继续运行,直到

int n=stream.Read(newdata[kn],numBytesRead,1)

在这里,代码停止并等待无限长的时间,没有任何错误

我做错了什么?为什么我发送的字节没有被写入? 谢谢你的帮助

BluetoothClient localClient = new BluetoothClient();
BluetoothDeviceInfo[] dev;
BluetoothDeviceInfo td = null;

addr = BluetoothAddress.Parse(Addr);

dev = localClient.DiscoverDevices();
foreach (BluetoothDeviceInfo d in dev)
{                      
    if (d.DeviceName == "Device_name")//my device name
    {
        td = d;
        break;
    }
}

try
{
    addr = td.DeviceAddress;

    if (!BluetoothSecurity.PairRequest(addr, null)) 
    {
        Console.WriteLine("Request failed");
    }
}
catch (Exception e)
{
    Console.WriteLine("Exception : " + e.Message);
    Console.Read();
}

BluetoothEndPoint endPoint = new BluetoothEndPoint(addr, BluetoothService.SerialPort,1);
localClient.Connect(endPoint);

Console.WriteLine(localClient.Connected);
NetworkStream stream = localClient.GetStream();

string config = "";
config = "240";

if (stream.CanWrite)
{
     byte[] data_conf = System.Text.Encoding.ASCII.GetBytes(config);              
     stream.Write(data_conf , 0, data_conf .Length);
     stream.Flush();
 }

 System.Threading.Thread.Sleep(100);

 int numBytesToRead = newdata[0].Length;
 int numBytesRead = 0;
 int nBytesRead = 0; // number of bytes read on each cycle

 //stream.ReadTimeout = 5;
 try
 {
      ////////read incoming bytes
      while (numBytesToRead > 0)
      {                        
          int n = stream.Read(newdata[kn], numBytesRead, 1);
          numBytesRead += n;
          numBytesToRead -= n;
      }
 }
 catch (Exception ex)
 {
       Console.Write(ex.Message);
 }