搜了很多贴都需要这行代码
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
程序抄来的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
using System.Threading;
using InTheHand.Net;
public class bule : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
BluetoothListener bluetoothListener;//蓝牙监听
Thread listenThread;//蓝牙监听线程
BluetoothClient bluetoothClient = new BluetoothClient();//创建新的蓝牙客户端
bool isConnected;//是否连接标识
/// <summary>
/// 打开端口
/// </summary>
/// <returns></returns>
public bool OpenPort()
{
//InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Connectable;//不包含PrimaryRadui
InTheHand.Net.Sockets.BluetoothClient cli = new InTheHand.Net.Sockets.BluetoothClient();//创建新的蓝牙客户端连接
Dictionary<string, BluetoothAddress> deviceAddresses = new Dictionary<string, BluetoothAddress>();//用一个键值对保存收索到的蓝牙地址
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
IReadOnlyCollection<BluetoothDeviceInfo> devices = cli.DiscoverDevices();//获得发现蓝牙的地址和名字
foreach (InTheHand.Net.Sockets.BluetoothClient. device in devices) //设备搜寻
{
device.Update();
device.Refresh();
if (device.DeviceName == "BF10-A")
{
this.textBox2.Text = "设备已找到";
break;
}
}
Thread ReceiveThread = new Thread(ReceiveData);
ReceiveThread.Start();
return true;
}
public void ReceiveData()
{
#region
try
{
Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805f9b34fb");
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
isConnected = true;
}
catch (Exception ex)
{
isConnected = false;
MessageBox.Show(ex.ToString());
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
Stream peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
textBox1.Text = receive;
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
#endregion
}
}