我在网上搜索过但我是新手,我觉得很难理解一切。
以下是我到目前为止所获得的代码:
在这部分中,我存储了我希望在变量中发送的文本文件,我正在存储每个设备的每个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++;
}
但是,设备无法连接到计算机,它表示失败。 我还需要手动配对设备吗? 谢谢
答案 0 :(得分:0)
好的,如果有人需要它,我已经弄清楚它为什么会失败。在启动之前,设备需要已与计算机配对。这已经解决了。但是,将文本文件发送到设备的部分仍无法正常工作