Windows 10 タブレットを別の Bluetooth デバイスとペアリングする必要があるプロジェクトを開始しました。
プロセスに慣れるために、まずはシンプルな Windows フォーム アプリから始めることにしました。ソリューションに 32feet.NET NuGet パッケージを追加したところ、すぐにデバイスの検索とリスト ボックスへのデータ入力に成功しました。
client = new BluetoothClient();
devices = client.DiscoverDevices();
if (devices.Length > 0)
{
foreach (var device in devices)
{
lstBTDevices.Items.Add(device.DeviceName);
}
}
else
{
MessageBox.Show("Unable to detect any bluetooth devices");
}
次に、検出されたデバイスを選択してペアリングを試みることができるように、イベント ハンドラーを追加しました。
private void LstBTDevices_SelectedIndexChanged(object sender, EventArgs e)
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, "123456"))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
}
安価な Bluetooth 2.0 アダプタを搭載した Windows7 デスクトップ PC では、携帯電話に PIN コードの入力を要求するポップアップが表示されます。「123456」と入力すると、ペアリングが成功します。
しかし、ここから問題が始まります。次に、アプリケーションを Windows10 タブレットで実行すると、携帯電話を選択すると、携帯電話にポップアップが表示され、ランダムな 6 桁の PIN コードと、タブレット画面に表示されているものと一致する必要があるというメッセージが表示され、ペアリング/キャンセル ボタンがオプションとして表示されます。どちらのボタンを押しても失敗します。
これは私が間違っているのでしょうか? 32feet.NET でサポートされていないドライバーでしょうか?
アドバイスをいただければ幸いです。
更新: bare_metalからのコメントのおかげで、少し前進することができました
BluetoothWin32Authentication イベント ハンドラーを追加し、SSP ペアリングを開始するボタンを追加しました。
EventHandler<BluetoothWin32AuthenticationEventArgs> authHandler = new EventHandler<BluetoothWin32AuthenticationEventArgs>(handleAuthRequests);
BluetoothWin32Authentication authenticator = new BluetoothWin32Authentication(authHandler);
private void btnPairSSP_Click(object sender, EventArgs e)
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (MessageBox.Show(String.Format("Would you like to attempt to pair with {0}?", selectedDevice.DeviceName), "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Task t = new Task(PairBluetoothTask);
t.Start();
}
}
private void PairBluetoothTask()
{
BluetoothDeviceInfo selectedDevice = devices[lstBTDevices.SelectedIndex];
if (BluetoothSecurity.PairRequest(selectedDevice.DeviceAddress, null))
{
MessageBox.Show("We paired!");
}
else
{
MessageBox.Show("Failed to pair!");
}
}
private void handleAuthRequests(object sender, BluetoothWin32AuthenticationEventArgs e)
{
switch (e.AuthenticationMethod)
{
case BluetoothAuthenticationMethod.Legacy:
MessageBox.Show("Legacy Authentication");
break;
case BluetoothAuthenticationMethod.OutOfBand:
MessageBox.Show("Out of Band Authentication");
break;
case BluetoothAuthenticationMethod.NumericComparison:
if(e.JustWorksNumericComparison == true)
{
MessageBox.Show("Just Works Numeric Comparison");
}
else
{
MessageBox.Show("Show User Numeric Comparison");
if (MessageBox.Show(e.NumberOrPasskeyAsString, "Pair Device", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Confirm = true;
}
else
{
e.Confirm = false;
}
}
break;
case BluetoothAuthenticationMethod.PasskeyNotification:
MessageBox.Show("Passkey Notification");
break;
case BluetoothAuthenticationMethod.Passkey:
MessageBox.Show("Passkey");
break;
default:
MessageBox.Show("Event handled in some unknown way");
break;
}
}
携帯電話からペアリングを開始すると、正常に動作し、イベントがトリガーされ、メッセージ ボックスが表示され、ペアリングが成功します。
ただし、タブレットからペアリングを開始すると、イベント ハンドラーがトリガーされないため、ペアリングは失敗します。
ベストアンサー1
ここでの問題は、32feet ライブラリがレガシー ペアリングに基づいて構築されているため、接続先のデバイスのピンを知る必要があるか、ピンを入力するためのポップアップ ウィンドウを取得するために null を指定する必要があることだと思います。そのダイアログは、新しいバージョンの Windows では表示されない可能性があります。この点についてはよくわかりませんが、32feet ライブラリがラップするネイティブ関数のドキュメントには、Vista より新しいバージョン向けに開発する場合は別のメソッドを呼び出すように記載されています。
https://msdn.microsoft.com/en-us/library/windows/desktop/aa362770(v=vs.85).aspx
32feet の逆コンパイルされたソースを調べたところ、32feet は SSP をサポートしておらず、他のもののみをサポートしているように見えますが、これは提供されている Bluetooth スタック実装を更新する必要があるだけかもしれません。または、独自の実装を作成する必要があるかもしれません。これもよくわかりません。
このサードパーティの代わりに、Microsoft が提供する .NET 用ライブラリを調べることをお勧めします。Github のサンプルを使用して、すべてのデバイスに正常に接続してペアリングすることができました。