日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c# 低功耗蓝牙_c# - 如何使用C#手动绑定到WinForm中的蓝牙低能耗设备? - 堆栈内存溢出...

發布時間:2025/3/11 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# 低功耗蓝牙_c# - 如何使用C#手动绑定到WinForm中的蓝牙低能耗设备? - 堆栈内存溢出... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我想到了。 我走在正確的軌道上。

使用以下連接后:

var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

您需要執行自定義配對:

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);

但這只會給您一個錯誤。 您還必須創建device.DeviceInformation.Pairing.Custom.PairingRequested事件處理程序。

所以我創建了這個處理程序:

private void handlerPairingReq(DeviceInformationCustomPairing CP, DevicePairingRequestedEventArgs DPR)

{

//so we get here for custom pairing request.

//this is the magic place where your pin goes.

//my device actually does not require a pin but

//windows requires at least a "0". So this solved

//it. This does not pull up the Windows UI either.

DPR.Accept("0");

}

在PairAsync調用之前將其連接起來,例如:

device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;

連接我的BlueToothAdvertisementWatcher代碼的示例代碼:

private BluetoothLEAdvertisementWatcher BTWatch = new BluetoothLEAdvertisementWatcher();

private void Inits()

{

BTWatch.Received += new TypedEventHandler(BtAddRx);

BTWatch.Start();

}

private async void BtAddRx(BluetoothLEAdvertisementWatcher bw, BluetoothLEAdvertisementReceivedEventArgs args)

{

GattCommunicationStatus srslt;

GattReadResult rslt;

bw.Stop();//Stop this while inside.

device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);

if (device.DeviceInformation.Pairing.IsPaired == false)

{

/* Optional Below - Some examples say use FromIdAsync

to get the device. I don't think that it matters. */

var did = device.DeviceInformation.Id; //I reuse did to reload later.

device.Dispose();

device = null;

device = await BluetoothLEDevice.FromIdAsync(did);

/* end optional */

var handlerPairingRequested = new TypedEventHandler(handlerPairingReq);

device.DeviceInformation.Pairing.Custom.PairingRequested += handlerPairingRequested;

log("Pairing to device now....");

var prslt = await device.DeviceInformation.Pairing.Custom.PairAsync(DevicePairingKinds.ProvidePin, DevicePairingProtectionLevel.None);

log("Custom PAIR complete status: " + prslt.Status.ToString() + " Connection Status: " + device.ConnectionStatus.ToString());

device.DeviceInformation.Pairing.Custom.PairingRequested -= handlerPairingRequested; //Don't need it anymore once paired.

if (prslt.Status != DevicePairingResultStatus.Paired)

{ //This should not happen. If so we exit to try again.

log("prslt exiting. prslt.status=" + prslt.Status.ToString());// so the status may have updated. lets drop out of here and get the device again. should be paired the 2nd time around?

bw.Start();//restart this watcher.

return;

}

else

{

// The pairing takes some time to complete. If you don't wait you may have issues. 5 seconds seems to do the trick.

System.Threading.Thread.Sleep(5000); //try 5 second lay.

device.Dispose();

//Reload device so that the GATT services are there. This is why we wait.

device = await BluetoothLEDevice.FromIdAsync(did);

}

var services = device.GattServices;

//then more code to finish it up.

}

如果您想斷開連接,請使用:

await device.DeviceInformation.Pairing.UnpairAsync();

抱歉,代碼混亂。 如果發現有人有用或有疑問,請告訴我。 我在任何地方都找不到此代碼的任何WinForm示例。 實際上,在沒有UI的情況下,我找不到任何代碼來顯示如何與PIN配對。 因此,我希望這對任何可能陷入困境的人有所幫助。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的c# 低功耗蓝牙_c# - 如何使用C#手动绑定到WinForm中的蓝牙低能耗设备? - 堆栈内存溢出...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。