Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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# 在C语言中通过蓝牙从PC向手机发送任何文字信息#_C# - Fatal编程技术网

C# 在C语言中通过蓝牙从PC向手机发送任何文字信息#

C# 在C语言中通过蓝牙从PC向手机发送任何文字信息#,c#,C#,我正试图通过蓝牙连接将文字信息从系统发送到任何手机,但我无法发送 我也接受了这个链接的帮助,但是这个代码会产生错误 通过蓝牙接入c-sharp public async Task<bool> Send(Device device, string content) { if (device == null) { throw new ArgumentNullException("device"

我正试图通过蓝牙连接将文字信息从系统发送到任何手机,但我无法发送

我也接受了这个链接的帮助,但是这个代码会产生错误 通过蓝牙接入c-sharp

public async Task<bool> Send(Device device, string content)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

        // for not block the UI it will run in a different threat
            var task = Task.Run(() =>
            {
                using (var bluetoothClient = new BluetoothClient())
                {
                    try
                    {
                        var ep = new       BluetoothEndPoint(device.DeviceInfo.DeviceAddress, _serviceClassId);

                    // connecting
                        bluetoothClient.Connect(ep);

                        // get stream for send the data
                        var bluetoothStream = bluetoothClient.GetStream();

                    // if all is ok to send
                        if (bluetoothClient.Connected && bluetoothStream != null)
                        {
                        // write the data in the stream
                            var buffer = System.Text.Encoding.UTF8.GetBytes(content);
                            bluetoothStream.Write(buffer, 0, buffer.Length);
                            bluetoothStream.Flush();
                            bluetoothStream.Close();
                            return true;
                        }
                        return false;
                    }
                    catch
                    {
                    // the error will be ignored and the send data will report as not sent
                    // for understood the type of the error, handle the exception
                    }
                }
                 return false;
            });
            return await task;
        }
    }
公共异步任务发送(设备、字符串内容)
{
如果(设备==null)
{
抛出新的异常(“设备”);
}
if(string.IsNullOrEmpty(content))
{
抛出新的异常(“内容”);
}
//如果不阻止UI,它将以不同的威胁运行
var task=task.Run(()=>
{
使用(var bluetoothClient=new bluetoothClient())
{
尝试
{
var ep=新的Bluetooth端点(device.DeviceInfo.DeviceAddress,_serviceClassId);
//连接
bluetoothClient.Connect(ep);
//获取用于发送数据的流
var bluetoothStream=bluetoothClient.GetStream();
//如果一切都可以发送
if(bluetoothClient.Connected&&bluetoothStream!=null)
{
//在流中写入数据
var buffer=System.Text.Encoding.UTF8.GetBytes(内容);
写入(缓冲区,0,缓冲区,长度);
bluetoothStream.Flush();
bluetoothStream.Close();
返回true;
}
返回false;
}
抓住
{
//错误将被忽略,发送数据将报告为未发送
//要了解错误的类型,请处理异常
}
}
返回false;
});
返回等待任务;
}
}

如果你能在这里发布你所做的事情,并发布你的代码,那就太好了。如果你说的“发送短信”是指短信,那么它就不能通过蓝牙实现(或者至少,没有通用的方式)。如果您的意思是“通过蓝牙串行通信套接字发送文本流”,那么如果需要帮助,您需要显示您尝试了什么以及错误在哪里。堆栈溢出不是为您生成代码的。我发布了一个用于发送文本消息的代码,请检查它。您能澄清此代码的问题吗?目前,我们了解到它并没有达到您的预期-您是否有任何错误?你认为我们应该看看什么日志吗?你怎么知道它不起作用呢?如果你能在这里发布你所做的事情并发布你的代码就好了。如果“发送短信”你指的是短信,那么它就不能通过蓝牙实现(或者至少,没有通用的方式)。如果您的意思是“通过蓝牙串行通信套接字发送文本流”,那么如果需要帮助,您需要显示您尝试了什么以及错误在哪里。堆栈溢出不是为您生成代码的。我发布了一个用于发送文本消息的代码,请检查它。您能澄清此代码的问题吗?目前,我们了解到它并没有达到您的预期-您是否有任何错误?你认为我们应该看看什么日志吗?你怎么知道它不起作用?