我想问问:unity 用C# InTheHand.Net.Bluetooth库连接蓝牙时,报错
-SocketException: 由于套接字没有连接并且(当使用一个 sendto调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受-
该怎么解决并连接蓝牙设备哇
public IEnumerator GetScan(Button button)
{
State.text = "当前状态:扫描中" ;
button.GetComponentInChildren<Text>().text = "扫描中";
button.interactable = false;
Task<IReadOnlyCollection<BluetoothDeviceInfo>> Scan = Task.Run(() =>
{
return client.DiscoverDevices();
});
while (!Scan.IsCompleted)
{
yield return null;
}
ListUpdate(Scan.Result, Parent);
button.GetComponentInChildren<Text>().text = "刷新";
button.interactable = true;
}
public void ListUpdate(IReadOnlyCollection<BluetoothDeviceInfo> Scanlist,GameObject Parent)
{
for (int i = 0; i < transform.childCount; i++)
{
Destroy(transform.GetChild(i).gameObject);
}
foreach (var item in Scanlist)
{
GameObject obj = Instantiate(CloneObj, Parent.transform);
obj.transform.Find("DeviceName").GetComponent<Text>().text = item.DeviceName;
obj.transform.Find("DeviceAddress").GetComponent<Text>().text = item.DeviceAddress.ToString();
obj.GetComponent<Button>().onClick.AddListener(() =>
{
obj.SetActive(false);
Debug.Log(item.DeviceAddress);
obj.SetActive(!Connect(item.DeviceAddress));
});
obj.SetActive(true);
}
}
public void Searchequipment()
{
IReadOnlyCollection<BluetoothDeviceInfo> devices = client.DiscoverDevices();
foreach (var item in devices)
{
Debug.Log(item.DeviceAddress);
Debug.Log(item.DeviceName);
}
}
public bool Connect(BluetoothAddress address)
{
Debug.Log("正在连接!");
BluetoothClient cli = new BluetoothClient();
cli.Connect(address, BluetoothService.SerialPort);
State.text = cli.Connected ? "当前状态:已连接" : "当前状态:未连接";
return cli.Connected;
}
蓝牙本质上相当于网卡,检查下配对是否正常,是否获得了地址,以及是否可以连接
检查下电脑的防火墙是否阻止了连接。程序里的端口、地址、协议是否正确。