I have searched the web but I am a newbie and I find it hard to understand everything clearly.
Here is the code I have reached so far:
In this part, I am storing the text files I desire to send in variables, the I am storing each mac address of each device along with the text file to be sent to this device in a 2D array. an array cdevices is created to store the detected devices mac addresses in the range.
//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
Here is a foreach loop that detects each device, and once the device is detected, a text file is supposed to be sent to that device.
//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++;
}
However,the devices do not connect to the computer, and it says failure. Also do I have to manually pair the devices? Thank you
Okay so in case someone needs it , I have figured out why it gives a failure. The device needs to be already paired with the computer before starting. This has fixed it. However the part for sending the text file to the device is still not working
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.