编程语言
首页 > 编程语言> > 如何使用C#启用和禁用蓝牙

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

作者:互联网

我目前正在进行蓝牙激活(从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

当在“设备”中禁用了蓝牙并且执行了上述行并关闭了应用程序后,就会发生上述错误
但是随着应用程序关闭,并且当我进入Mobile中的Bluetooth Manager时,蓝牙已启用.

我的问题 :

i have to click a button 2 times to enable button (1st when the
application closes with error (but Bluetooth is set ON) and 2nd to
search Devices in range) instead of 1 click.

我的假设

I think that there maybe some security problem when the program tries
to enable the Bluetooth from Mobile from OFF to Discoverable.

因此,有没有任何进程(System.Digonostics; dll),通过它我可以在C#的WindowsMo​​bile CE中自动设置蓝牙的开和关,

我尝试了但没有得到,所以任何人都可以帮我解决这个问题,或者建议任何用于蓝牙连接的dll文件.

谢谢

解决方法:

您可能会发现这很有用:

Bluetooth Device Development using C#

此处提供了用于蓝牙技术的Windows Embedded Source Tools的下载:

Download

希望这可以帮助 :)

标签:windows-ce,bluetooth,visual-studio-2008,c,smart-device-framework
来源: https://codeday.me/bug/20191028/1954234.html