ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-28 18:19:09  阅读:1253  来源: 互联网

标签:windows-ce bluetooth visual-studio-2008 c smart-device-framework


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有