UWP蓝牙RFCOMM通信问题:BluetoothDevice对象始终返回null
BluetoothDevice.FromIdAsync Returning Null in UWP Bluetooth RFCOMM Hey Dustin, let’s work through why BluetoothDevice.FromIdAsync keeps returning null even though your PC and HoloLens are paired. I’ve dealt with similar UWP Bluetooth quirks before, so here are targeted fixes to check:
1. Verify UWP App Permissions
UWP requires explicit Bluetooth capabilities to access paired devices. Double-check your Package.appxmanifest:
- Add the
bluetooth.rfcommdevice capability with the correct function type (e.g., serial port for RFCOMM):
<Capabilities> <DeviceCapability Name="bluetooth.rfcomm"> <Device Id="any"> <Function Type="name:serialPort" /> </Device> </DeviceCapability> <!-- Include other required capabilities like internetClient if needed --> <Capability Name="internetClient" /> </Capabilities>
- Also, request runtime access to Bluetooth before calling
FromIdAsync:
var accessStatus = await Bluetooth.RequestAccessAsync(); if (accessStatus != BluetoothAccessStatus.Allowed) { // Handle permission denied case return; }
2. Validate the DeviceInfo.Id Format
Make sure you’re fetching the device ID via UWP’s native DeviceInformation API, not from an external tool like Device Manager. Use this to get paired Bluetooth devices:
var pairedDevices = await DeviceInformation.FindAllAsync( BluetoothDevice.GetDeviceSelectorFromPairingState(true));
The ID from this call is the correct format for FromIdAsync—external tool IDs often don’t match what UWP expects.
3. Check Post-Pairing Device Permissions
Even after pairing, Windows might restrict app access to the device:
- On your PC: Go to Settings > Bluetooth & devices > Devices, select your HoloLens, and ensure "Allow apps to access this device" is enabled.
- On HoloLens: Navigate to Settings > Bluetooth, select your PC, and confirm app access permissions are granted.
- If permissions are missing, unpair and re-pair both devices while your UWP app is running—this can trigger the permission prompt for the app.
4. Confirm RFCOMM Service Registration
If your HoloLens app acts as the RFCOMM server, ensure the service is properly registered and advertised:
// Server-side (HoloLens) var rfcommProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.SerialPort); var serviceName = "MyRFCOMMService"; rfcommProvider.StartAdvertising(null, true);
Your PC client must target the same service ID when connecting—mismatched service IDs can lead to null device objects even if paired.
5. Reset Bluetooth Stack & Debug Environment
Sometimes cached Bluetooth state causes issues:
- Restart both your PC and HoloLens to clear the Bluetooth stack.
- In Visual Studio, ensure you’re deploying the app directly to the physical device (not a simulator) and that debug permissions are enabled.
Give these steps a shot—most of the time, null returns here boil down to permission gaps or incorrect device ID formatting. Let me know if you hit any roadblocks!
内容的提问来源于stack exchange,提问作者duesterdust

