Код: Выделить всё
BluetoothClient client;
try
{
client = new();
}
catch (Exception e)
{
Console.WriteLine("Turn your bluetooth on and try again.");
return;
}
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
Console.WriteLine("Bluetooth Devices: ");
for (int i = 0; i < devices.Count(); i++)
{
Console.WriteLine(i + " - " + devices[i].DeviceName);
}
Console.Write("Choose the device to connect: ");
int input = Convert.ToInt32(Console.ReadLine());
BluetoothDeviceInfo device = devices[input];
Console.WriteLine("Device adress: " + device.DeviceAddress.ToString());
BluetoothAddress bluetoothAdress = new(device.DeviceAddress.ToByteArray());
BluetoothEndPoint endPoint = new(bluetoothAdress, BluetoothService.HumanInterfaceDevice);
try
{
client.Connect(endPoint);
if (client.Connected)
{
Console.WriteLine("Connected successfully to " + device.DeviceName);
}else
{
Console.WriteLine("Connection failed.");
}
}
catch (Exception e)
{
Console.WriteLine("Could not connect: \n" + e.ToString());
}
Не удалось подключиться:
System.Net.Sockets.SocketException (10049): запрошенный адрес недействительно в своем контексте. 684DB6B68F13:0000112400001000800000805f9b34fb
в System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddressocketAddress)
в System.Net.Sockets.Socket.Connect(EndPoint RemoteEP)
at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.Connect(BluetoothEndPoint RemoteEP)
в InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothEndPoint RemoteEP)
Подробнее здесь: https://stackoverflow.com/questions/790 ... ts-context