繁体   English   中英

我正在尝试使用C#通过蓝牙将文本文件发送到移动设备

[英]I am trying to send a text file to a mobile device via bluetooth using C#

我已经在网上搜索过,但是我是新手,因此很难清楚地理解所有内容。

这是我到目前为止已到达的代码:

在这一部分中,我将要发送的文本文件存储在变量中,将每个设备的每个mac地址以及要发送到该设备的文本文件存储在2D数组中。 创建一个数组cdevices来存储该范围内检测到的设备的mac地址。

//Storing data.

            var Sfile = @"C:\Users\Noha\Desktop\Shenawy.txt";
            var Mfile = @"C:\Users\Noha\Desktop\Moustafa.txt";
            var Nfile = @"C:\Users\Noha\Desktop\Noha.txt";

            string[,] array = new string[3, 2]
          {
                {"7840E4FA48EB", Sfile},
                {"502E5CBB1A4A", Mfile},
                {"0017AB39CAD3", Nfile}
          };

            string[] cdevices = new string[3];
            int x = 0;

           //end of storing data

这是一个检测每个设备的foreach循环,一旦检测到设备,就应该将文本文件发送到该设备。

 //Check if the laptop's bluetooth i connectable
                if (!BluetoothRadio.IsSupported)
                    System.Console.WriteLine("No Bluetooth device detected.");
                if (BluetoothRadio.PrimaryRadio.Mode == RadioMode.PowerOff)
                    BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable;
                System.Console.WriteLine(BluetoothRadio.PrimaryRadio.Name.ToString());
                System.Console.WriteLine(BluetoothRadio.PrimaryRadio.Mode.ToString());
                BluetoothClient bc = new BluetoothClient();
                //BluetoothDeviceInfo[] info = null;
                //info = bc.DiscoverDevices(999);
                BluetoothDeviceInfo[] devs = bc.DiscoverDevicesInRange();


                foreach (BluetoothDeviceInfo device in devs)
                {
                    //lstDevices.Items.Add(device.DeviceName + " - " + device.DeviceAddress);
                    cdevices[x] = device.DeviceAddress.ToString();
                    System.Console.WriteLine(device.DeviceName + "   " + device.DeviceAddress.ToString());
                    device.Update();
                    device.Refresh();
                    device.SetServiceState(BluetoothService.ObexObjectPush, true);

                   if (!device.Authenticated)
                    {
                        // Use pin "0000" for authentication
                        if (!BluetoothSecurity.PairRequest(device.DeviceAddress, "0000"))
                        {
                            // MessageBox.Show("Request failed");
                            System.Console.WriteLine("Request failed");
                        }
                    }

                    for (int countOr = 0; countOr < 3; countOr++)
                     {
                         for (int countOc = 0; countOc < 3; countOc++){
                             if (cdevices[x] == array[countOr, countOc])
                             {
                                 var uri = new Uri("obex://" + device.DeviceAddress + "/" + array[countOr,countOc+1]);
                                 //ObexWebRequest req = new ObexWebRequest(uri);
                                 var request = new ObexWebRequest(uri);
                                 request.ReadFile(array[countOr, 1]);
                                 var response = (ObexWebResponse)request.GetResponse();
                                 System.Console.WriteLine(response.StatusCode.ToString());
                                 //MessageBox.Show(response.StatusCode.ToString());
                                 // check response.StatusCode
                                 response.Close();
                                 break;
                             }
                         }
                     }

                    x++;
                }

但是,设备未连接到计算机,并显示故障。 我还必须手动配对设备吗? 谢谢

好的,如果有人需要它,我已经弄清楚了为什么它会失败。 在启动之前,设备必须已经与计算机配对。 这已经解决了。 但是,将文本文件发送到设备的部分仍然无法正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM