如何在Windows 10中订阅蓝牙开启或关闭事件? [英] How to subscribe the Bluetooth is on or off event in windows 10?

查看:122
本文介绍了如何在Windows 10中订阅蓝牙开启或关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有Bluetooth LE功能的应用程序,当它在Windows 10上运行时,该应用程序需要知道Bluetooth的打开或关闭状态.

但是我没有找到任何可以订阅蓝牙设置打开或关闭事件的接口.

在UWP库或.Net库上是否为此定义了任何接口?

或者任何人都可以了解有关"ImmersiveControlPanel-> SystemSettings.exe"的详细信息. Windows 10中的工具,它可以设置许多系统选项,我真的很想知道它用来完成这些工作的API.

解决方案

coolog,

谢谢您在这里发布.

对于您的问题,您可以尝试以下代码.请先安装32feet.net.

这是代码.

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Security.Permissions;
使用System.Text;
使用System.Threading.Tasks;

命名空间watch_file_change
{
    班级计划
    {
        静态void Main(string [] args)
        {
            跑步();
            Console.ReadKey();
        }

        [PermissionSet(SecurityAction.Demand,Name ="FullTrust")]
        公共静态无效Run()
        {
            string [] args = System.Environment.GetCommandLineArgs();

            //如果未指定目录,请退出程序.
            如果(args.Length!= 2)
            {
                //显示正确的程序调用方式.
                Console.WriteLine(用法:Watcher.exe(目录)");
                返回;
            }

            //创建一个新的FileSystemWatcher并设置其属性.
            FileSystemWatcher watcher =新的FileSystemWatcher();
            watcher.Path = args [1];
            /*注意LastAccess和LastWrite时间的变化,以及
               文件或目录的重命名. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            //仅观看文本文件.
            watcher.Filter ="* .txt";

            //添加事件处理程序.
            watcher.Changed + =新的FileSystemEventHandler(OnChanged);
            watcher.Created + = new FileSystemEventHandler(OnChanged);
            watcher.Deleted + = new FileSystemEventHandler(OnChanged);
            watcher.Renamed + = new RenamedEventHandler(OnRenamed);

            //开始观看.
            watcher.EnableRaisingEvents = true;

            //等待用户退出程序.
            Console.WriteLine(按\'q \'退出示例.");
            而(Console.Read()!='q');
        }

        //定义事件处理程序.
        私有静态无效OnChanged(对象源,FileSystemEventArgs e)
        {
            //指定在更改,创建或删除文件时要执行的操作.
            Console.WriteLine(文件:" + e.FullPath +" + e.ChangeType);
        }

        私有静态无效OnRenamed(对象源,RenamedEventArgs e)
        {
            //指定重命名文件时要执行的操作.
            Console.WriteLine(文件:{0}重命名为{1}",e.OldFullPath,e.FullPath);
        }
    }
} 

如果打开了蓝牙,您将获得有关蓝牙的信息.

我希望这会对您有所帮助.

如果还有其他问题,请随时与我们联系.

最好的问候,

温迪


I am developing an App which has the Bluetooth LE feature, when run at windows 10, the App need to know the Bluetooth is on or off.

But I did not found any interface which can subscribe the Bluetooth setting on or off event. 

Is there any interface defined on UWP library or .Net library for this? 

Or can anybody knows the detail about the "ImmersiveControlPanel-->SystemSettings.exe" tools in windows 10, It can setting many system options, I really want to know which the API it used to accomplish these works.

解决方案

Hi coolog,

Thank you for posting here.

For your question, you could try the following code. Please install 32feet.net first.

Here is the code.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace watch_file_change
{
    class Program
    {
        static void Main(string[] args)
        {
            Run();
            Console.ReadKey();
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            string[] args = System.Environment.GetCommandLineArgs();

            // If a directory is not specified, exit program.
            if (args.Length != 2)
            {
                // Display the proper way to call the program.
                Console.WriteLine("Usage: Watcher.exe (directory)");
                return;
            }

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = args[1];
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        // Define the event handlers.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
        }
    }
}

If the BlueTooth is on, you would get the information about your BlueTooth.

I hope this would be helpful to you.

If you have something else, please feel free to contact us.

Best Regards,

Wendy


这篇关于如何在Windows 10中订阅蓝牙开启或关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆