私は自分のPCに接続する必要があるBluetooth低エネルギー(BTLE)デバイスを持っています。これを行うには、デスクトップWPFアプリケーションでWindows APIリファレンスを使用します。固有のサービスプロトコルを使用して32feet.netを使用してBTLEデバイスに接続してください
Bluetoothデバイスは、サービスが1つで、2つの特性(1つは読み取り/通知、1つは書き込み)です。
To make below code work, add the following references to the WPF (for windows 10):
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
C#
public GattDeviceService BLEService;
public GattCharacteristic BLEData_ReadNotify;
public GattCharacteristic BLEData_Write;
private static readonly Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
public static readonly Guid Characteristics_ReadNotify = new Guid("C3FBC9E2-676B-9FB5-3749-2F471DCF07B2");
public static readonly Guid Characteristics_Write = new Guid("D6AF9B3C-FE92-1CB2-F74B-7AFB7DE57E6D");
// Find all BTLE devices that have the service Guid: Guid_service
public void findandconnect() {
var BTLEdeviceList = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(Guid_service));
// lets assume at least one device is found, store it's id.
id = BTLEdeviceList[0].id;
// Connect to the SERVICE of the device
BLEService = await GattDeviceService.FromIdAsync(id);
// Get the first "ReadNotify" characteristics of that service
BLEData_ReadNotify = BLEService.GetCharacteristics(Characteristics_ReadNotify).First();
// If that value changes, update an event
BLEData_ReadNotify.ValueChanged += BLEData_ValueChanged;
// Also get the first "write" characteristics of that service
BLEData_Write = BLEService.GetCharacteristics(Characteristics_Write).First();
// and tell that characteristics to NOTIFY me if the BTLE device has an update.
await BLEData_ReadNotify.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify);
// Successfully connected
// Send the command "RUN\r" to the write characteristics, to let it start to execute the program
byte[] buffer = Encoding.ASCII.GetBytes("RUN" + "\r");
IBuffer ibuffer = buffer.AsBuffer();
// Sending "RUN"
await BLEData_Write.WriteValueAsync(ibuffer);
}
// And here should be some event function for the ValueChanged
ため
のWindows APIコードはまあ、それは動作しますが、ちょっと...それは、地獄のように不安定であり、それは解決できないエラーが発生し、ほとんどの時間:
行:BLEData_ReadNotify.WriteClientCharacteri ... 指定されたログオンセッションのユーザーセッションキーはありません。 (HRESULTからの例外:0x80070572)
およびセマフォのタイムアウト期間が切れています。 (HRESULTからの例外:0x80070079)。この2番目のエラーはあまり一般的ではなく、スクリプトが最初に何も受信しなかった場合に表示され、次にRUNコマンドを再度送信しようとするタイマーを介してすべてのクラッシュが発生し、エラーが表示されます。
stackoverflowと多くのサイトで検索すると、上記の両方のエラーは解決できないことがわかりました。したがって、私の質問:
上記のコードは32Feet.netを使用して書き換えることができますか?
2012年にはlinkにはまったく使用することはできませんでしたが、おそらく今ですか?この回答のlinkのスキャン方法を使用すると、ペアになっていないBLEデバイスが見つかります。
ので、私はいくつかのコード
// mac is mac address of local bluetooth device
Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
localEndpoint = new BluetoothEndPoint(myRadio.LocalAddress, Guid_service);
// client is used to manage connections
localClient = new BluetoothClient(localEndpoint);
BluetoothAddress a = new BluetoothAddress(new byte[] { 180, 99, 76, 86, 31, 21 });
Guid Guid_service = new Guid("25FB9E91-1616-448D-B5A3-F70A64BDA73A");
localClient.Connect(a, Guid_service);
NetworkStream stream = localClient.GetStream();
を試してみましたが、私は、エラーのある行localClient.Connectで撚りています:型「System.Net.Sockets.SocketException」の例外が発生した 。 追加情報:接続されているパーティが適切に一定期間後に応答しなかったため、接続の試行が失敗した、または確立された接続は、接続されたホストが
を応答に失敗したため、Windows APIのコードが32feet.netに書き換えることができませんでした、ifso、進める方法?
注:Windowsネイティブの.netコードを使用してBT2.1デバイスに接続しようとしましたが、シリアルポートが不安定で頭が痛いです。 32feet.netを使用すると、SerialPort上ですぐれて動作し、非常に安定し、毎回接続します!
あなたはこのためにそれを得た任意のソリューション?私も同じことを探しています。 –
ソリューションは8+ウィンドウを使用し、WinRTのAPIを利用することです。 「デスクトップC#アプリケーションにwinRTを含める方法」のようなものを検索してください。あなたはWinRTのAPIを含めたら、あなたはBT 2.1と同様にBT4デバイスに接続することができますそのブルートゥース呼び出し、利用することができます。私はコードをどこかに置いていますが、今は時間がありません。あなたがコードを必要としたら、私はできるだけ早く投稿します。 – Jeffrey
うん男はあなたのために可能なコードやリンクを投稿してください。ありがとう。 –