[英]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.