使用 InTheHand 在 Windows Mobile 5.0 中进行蓝牙连接 - C#






2.91/5 (6投票s)
Windows Mobile 5.0 中的蓝牙连接
引言
本文讨论了 Windows Mobile 5.0 设备如何使用 C# 中的 InTheHand
DLL 通过蓝牙连接。
在 Windows Mobile 中设置蓝牙可发现模式
我们需要将移动设备设置为蓝牙可发现模式。在蓝牙设置中,您将勾选“可发现”选项。
InTheHand
在 Compact Framework 1.0 中,我们没有处理蓝牙套接字连接的任何托管类。InTheHand
是一个第三方组件,为 Windows Mobile 提供蓝牙服务的功能。您可以从 这里 下载 InTheHand
。
Using the Code
源代码可以从本文顶部的链接下载。当我们点击名为“搜索设备”的第一个按钮时,设备将显示在一个组合框中。然后,我们可以通过点击名为“连接”的按钮从组合框中连接选定的设备。
以下是项目中使用的代码:
//
// Directives.
//
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
//
//Declaration
//
private BluetoothClient bluetoothClient;
private Guid service = BluetoothService.DialupNetworking;
//
//Search the devices and displaying in combobox.
//
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
bluetoothClient = new BluetoothClient();
Cursor.Current = Cursors.WaitCursor;
BluetoothDeviceInfo[] bluetoothDeviceInfo = { };
bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
comboBox1.DataSource = bluetoothDeviceInfo;
comboBox1.DisplayMember = "DeviceName";
comboBox1.ValueMember = "DeviceAddress";
comboBox1.Focus();
Cursor.Current = Cursors.Default;
//
//Connect the selected device.
//
bluetoothClient.Connect(new BluetoothEndPoint
((BluetoothAddress)comboBox1.SelectedValue, service));
结论
这对于使用 C# 在 Windows Mobile 中工作将很有帮助。此后,我们可以通过蓝牙在两台 Windows Mobile 设备之间完成一些操作。