我正在尝试使用 C# 通过蓝牙连接我的手机(小米)和我的电脑(华硕)。我收到此错误,它表示地址无效,这是完整的代码
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, SocketAddress 套接字地址) 在 System.Net.Sockets.Socket.Connect(端点远程EP) 在InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.Connect(BluetoothEndPoint RemoteEP) 在InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothEndPoint RemoteEP)
虽然
BluetoothAddress
是 unsigned long
,但您正在从 BD_ADDR 字符串创建 byte array
,这是完全不同的东西。
您应该使用 32Feet 的
BluetoothAddress.Parse()
,这应该很有效。