c#读蓝牙数据_c# – 将蓝牙设备连接到具有32feet .NET蓝牙库的计算机

我想出了如何解决我的问题,我的蓝牙连接知识现在有点大。如果有人有问题,我提供我的解决方案。代码示例代表使用32feet蓝牙库的蓝牙控制器的C#实现。

扫描

这意味着检测到范围内的设备。我的代码:

// mac is mac address of local bluetooth device

BluetoothEndPoint localEndpoint = new BluetoothEndPoint(mac, BluetoothService.SerialPort);

// client is used to manage connections

BluetoothClient localClient = new BluetoothClient(localEndpoint);

// component is used to manage device discovery

BluetoothComponent localComponent = new BluetoothComponent(localClient);

// async methods, can be done synchronously too

localComponent.DiscoverDevicesAsync(255, true, true, true, true, null);

localComponent.DiscoverDevicesProgress += new EventHandler(component_DiscoverDevicesProgress);

localComponent.DiscoverDevicesComplete += new EventHandler(component_DiscoverDevicesComplete);

private void component_DiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)

{

// log and save all found devices

for (int i = 0; i < e.Devices.Length; i++)

{

if (e.Devices[i].Remembered)

{

Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is known");

}

else

{

Print(e.Devices[i].DeviceName + " (" + e.Devices[i].DeviceAddress + "): Device is unknown");

}

this.deviceList.Add(e.Devices[i]);

}

}

private void component_DiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)

{

// log some stuff

}

配对

这意味着设备与本地蓝牙设备耦合。这需要通过输入双方的代码进行一次。可以通过代码完成,以便用户甚至不会注意到设备被添加。我的代码为此目的:

// get a list of all paired devices

BluetoothDeviceInfo[] paired = localClient.DiscoverDevices(255, false, true, false, false);

// check every discovered device if it is already paired

foreach (BluetoothDeviceInfo device in this.deviceList)

{

bool isPaired = false;

for (int i = 0; i < paired.Length; i++)

{

if (device.Equals(paired[i]))

{

isPaired = true;

break;

}

}

// if the device is not paired, pair it!

if (!isPaired)

{

// replace DEVICE_PIN here, synchronous method, but fast

isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, DEVICE_PIN);

if (isPaired)

{

// now it is paired

}

else

{

// pairing failed

}

}

}

这意味着建立连接和数据交换。再一次代码:

// check if device is paired

if (device.Authenticated)

{

// set pin of device to connect with

localClient.SetPin(DEVICE_PIN);

// async connection method

localClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), device);

}

// callback

private void Connect(IAsyncResult result)

{

if (result.IsCompleted)

{

// client is connected now :)

}

}

如果你保持订单扫描,对,连接,一切都应该正常。要发送或接收数据,请使用BluetoothClient的GetStream()方法。它提供可以被操纵的网络流。

接收连接

如果您希望其他设备与设备连接,则需要收听传入的连接请求。这只有在设备已经配对之前才有效。我的代码:

BluetoothListener l = new BluetoothListener(LOCAL_MAC, BluetoothService.SerialPort);

l.Start(10);

l.BeginAcceptBluetoothClient(new AsyncCallback(AcceptConnection), l);

void AcceptConnection(IAsyncResult result){

if (result.IsCompleted){

BluetoothClient remoteDevice = ((BluetoothListener)result.AsyncState).EndAcceptBluetoothClient(result);

}

}

用有效的蓝牙地址替换LOCAL_MAC(例如使用BluetoothAddress.Parse();)。连接设备后,他们可以通过底层流来交换消息。如果连接不起作用,可能会出现身份验证问题,请尝试在侦听器中设置本地设备引脚(l.SetPin(LOCAL_MAC,MY_PASSWORD);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值