11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-10-05

#参考文献
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; //こうでないと重複例外が出る
        }
    }
}

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?