如何使用C#启用和禁用蓝牙

4

我目前正在处理蓝牙激活问题(在Windows CE 6版本中,只需自动启用和禁用蓝牙即可完成某个操作后)。我使用的是SmartDeviceFramework,即CAB文件,然后将其安装在Windows CE上。

以下是我使用的方法(使用InTheHand.Net.Personal.dll文件进行蓝牙操作):

  private static void setBluetoothConnection()
    {
     try
       {
           if (BluetoothRadio.IsSupported == true)
           {
               MessageBox.Show("Bluetooth Supported", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
               MessageBox.Show(radio.Mode.ToString(), "Before Bluetooth Connection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               radio.Mode = RadioMode.Discoverable;
               // here radio.Mode works only if the Windows Device has Bluetooth enabled otherwise gives error
               MessageBox.Show(radio.Mode.ToString(), "RadioMode Discover", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               bluetoothClient = new BluetoothClient();
               //Cursor.Current = Cursors.WaitCursor;
               BluetoothDeviceInfo[] bluetoothDeviceInfo = bluetoothClient.DiscoverDevices();
               MessageBox.Show(bluetoothDeviceInfo.Length.ToString(), "Device Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
               foreach(BluetoothDeviceInfo device in bluetoothDeviceInfo)
               {
                 Cursor.Current = Cursors.Default;
                 MessageBox.Show(device.DeviceName, "Device Name", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 bluetoothClient.Connect(new BluetoothEndPoint(device.DeviceAddress, service));
                 MessageBox.Show("Bluetooth Connected...", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                 break;
               }
             }
          else
          {
               MessageBox.Show("Bluetooth Not Supported", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
          }
       }
        catch (Exception ex)
        {
           log.Error("[Bluetooth] Connection failed", ex);
           MessageBox.Show(ex.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
        }
    }

所以我在这里遇到了错误:
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
radio.Mode = RadioMode.Discoverable; // gives error here 

错误

Error setting BluetoothRadio.Mode

当设备蓝牙关闭并执行上述行关闭应用程序时,会出现上述错误,但在应用程序关闭时,并且当我进入移动设备的蓝牙管理器时,蓝牙已启用。
我的问题是:
我必须点击两次按钮才能启用按钮(第一次应用程序因错误而关闭(但蓝牙已设置为ON),第二次搜索范围内的设备),而不是一次点击。
我的假设:
我认为当程序尝试将移动设备的蓝牙从关闭状态切换到可发现状态时,可能存在某些安全问题。
因此,在WindowsMobile CE中,有没有任何(System.Diagnostics; dll)可以通过C#自动将蓝牙打开和关闭?
我已经尝试过,但是没有得到结果,所以有人可以帮助我解决这个问题或建议任何蓝牙连接的文件。

物联网、蓝牙和32feet.net?我点赞了;)。 - Kenneth Ito
谢谢回复。我尝试使用32feet.net(InTheHand.Net.Personal.dll),但在启用蓝牙时出现错误。您知道我可以在哪里找到Microsoft.WindowsMobile.SharedSource.Bluetooth dll吗?这个dll会起作用吗?有什么想法? - techGaurdian
2个回答

3

1
这不是我想要的解决方案...尝试使用Microsoft.WindowsMobile.SharedSource.Bluetooth,但找不到dll(如果存在的话),但还是谢谢。 - techGaurdian
下载并安装以上程序,然后导航到: C:\Program Files\Microsoft\Windows Embedded Source Tools\ 你将会在那里找到dll文件 ;) - ravenx30
1
第一个链接现在返回404错误。 - illusion466
第二个链接也失效了。现在它只会打开 Edge 浏览器到默认的必应搜索页面。 - Los2000

1
我不确定你正在使用的库是什么,所以我不确定为什么会出现那个错误。
这里是微软关于如何在设备上设置蓝牙连接模式的文档。如果你可以使用这些DLL文件,你可能会有一些运气。

https://msdn.microsoft.com/en-us/library/bb416244.aspx


是的,我尝试过这个,但在实现时没有成功。我的假设是BthGetMode和BthSetMode的值是int类型的..我想要的是RadioMode...但还是谢谢。 - techGaurdian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接