想编写C#的客户端,在网上查到大家都用 InTheHand库,我在WInform中照着编写下面这段时,出现了三个报错。
源代码:
BluetoothClient client = new BluetoothClient(); //处理蓝牙的对象
BluetoothRadio radio = BluetoothRadio.PrimaryRadio; //获取电脑蓝牙
radio.Mode = RadioMode.Connectable; //设置电脑蓝牙可被搜索到
BluetoothAddress blueAddress ; //需要连接的蓝牙模块的唯一标识符
BluetoothDeviceInfo[] devices = client.DiscoverDevices(); //搜索蓝牙设备,10秒
//从搜索到的所有蓝牙设备中选择需要的那个
foreach (var item in devices)
{
if(item.DeviceName.Equals("需要连接的蓝牙模块名字")) //根据蓝牙名字找
{
Console.WriteLine(item.DeviceAddress);
Console.WriteLine(item.DeviceName);
blueAddress = item.DeviceAddress; //获得蓝牙模块的唯一标识符
break;
}
Console.WriteLine(item.DeviceAddress);
Console.WriteLine(item.DeviceName);
}
我照着写时遇到,遇到以下报错
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;这一句。我的BluetoothRadio类后只有Default这个属性,没有PrimaryRadio。
BluetoothDeviceInfo[] devices = client.DiscoverDevices(); 这一句。报错为:
无法将类型“System.Collections.Generic.IReadOnlyCollection<InTheHand.Net.Sockets.BluetoothDeviceInfo>”隐式转换为“InTheHand.Net.Sockets.BluetoothDeviceInfo[]”。存在一个显式转换(是否缺少强制转换?) Bluetooth_console_Test
强制写成BluetoothDeviceInfo[] devices = (BluetoothDeviceInfo[])client.DiscoverDevices();运行时还是报错,显示无法强转。
报错:Must use PackageReference,没有运行前就报错。这个报错在Console里测试也有。
请问是怎么回事?C#新学者,恳请指教。