Help us understand the problem. What is going on with this article?

Windows10のC#コンソールアプリケーションでBLEのアドバタイズメントをスキャンしたい

More than 1 year has passed since last update.

参考文献

WindowsデスクトップアプリでBLEのGATTで体温計と血圧計と通信する
https://qiita.com/gebo/items/41da7474936845d77d06

Windows10でBLEデバイスとGATTで通信するメモ
https://qiita.com/Dr10_TakeHiro/items/7446d68cbffeae7c7184

iBeaconをスキャンするWindowsデスクトップアプリ
https://qiita.com/gebo/items/469dd49ddd1e24ce7a42

できる!C#で非同期処理(Taskとasync-await)
https://www.kekyo.net/2016/12/06/6186

C#のコンソールアプリケーションで非同期処理をするときのメモ
https://qiita.com/gpsnmeajp/items/ef21ba4d988a76922bab

メモ

1.Windows 10対応、Bluetooth 4.0対応のBluetoothアダプタを刺しておく。(Windows標準ドライバで良い)
 なお、Winodws10のスキャン画面でアドバタイズメントの受信チェックができる
image.png

2.Visual Stduio 2015 Communityをインストールする。
 この際、ここにチェックを入れる
 image.png

3.C#コンソールアプリケーションプロジェクトを作成する
4.NuGetからUwpDesktopをインストールする
Install-Package UwpDesktop

image.png
image.png

5.以下のコードを実行する

受信したアドバタイズメントパケットを片っ端から表示する

こんな感じで検出できる。(なお、Windows標準の検索では出てくるのに、ここでは出てこない機器も居たりする)
image.png

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Background;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;

namespace ConsoleApplication2
{
    class Program
    {
        static BluetoothLEAdvertisementWatcher watcher;

        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            watcher = new BluetoothLEAdvertisementWatcher();
            watcher.Received += Watcher_Received;
            watcher.ScanningMode = BluetoothLEScanningMode.Passive;
            watcher.Start();
            Thread.Sleep(60000);
            watcher.Stop();
            Console.WriteLine("Stop");
        }

        private static void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            Console.WriteLine("---Received---");
            var bleServiceUUIDs = args.Advertisement.ServiceUuids;

            Console.WriteLine("Found");
            Console.WriteLine("MAC:" + args.BluetoothAddress.ToString());
            Console.WriteLine("NAME:" + args.Advertisement.LocalName.ToString());
            Console.WriteLine("ServiceUuid");
            foreach (var uuidone in bleServiceUUIDs)
            {
                Console.WriteLine(uuidone.ToString());
            }
            Console.WriteLine("---END---");
            Console.WriteLine("");
        }
    }
}

一定時間スキャンした上で見つかったものを重複なく表示する

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Background;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    class Program
    {
        static BluetoothLEAdvertisementWatcher watcher;
        static Dictionary<ulong, string> dict = new Dictionary<ulong, string>();

        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            watcher = new BluetoothLEAdvertisementWatcher();
            watcher.Received += Watcher_Received;
            watcher.ScanningMode = BluetoothLEScanningMode.Passive;
            watcher.Start();
            Thread.Sleep(10000);
            watcher.Stop();
            Console.WriteLine("\nStop");

            Console.WriteLine("Found");
            foreach (var d in dict)
            {
                Console.WriteLine("MAC:" + d.Key.ToString() +" NAME:"+ d.Value);
            }
        }

        private static void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
        {
            Console.Write("!");
            var bleServiceUUIDs = args.Advertisement.ServiceUuids;
            dict[args.BluetoothAddress] = args.Advertisement.LocalName; //こうでないと重複例外が出る
        }
    }
}

gpsnmeajp
電子工作が主な趣味ですが、PC用のツールを作るのも好きです。 特にライセンスを明記していないソースコードのライセンスについてはCC0です。
https://sabowl.sakura.ne.jp/gpsnmeajp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした