uwpbluetoothhololensrfcomm

Set Bluetooth discoverability in UWP App for HoloLens2


I have written an app that provides a Bluetooth RFCOMM service on HoloLens 2. Without using the settings app, can I make HoloLens2 discoverable for any devices looking for the service? I don't want to force the user to use the settings app just to pair the device. I have been unable to find anything that lets me control the device discoverability using my own app. Is this even possible in UWP?


Solution

  • Discoverable mode can be started by calling:

    rfcommServiceProvider.StartAdvertising(streamSocketListener, true);
    

    A working example can be found in the RFCOMM samples provided by Microsoft. Sample

    Full code:

    var rfcommServiceProvider = await RfcommServiceProvider.CreateAsync(RfcommServiceId.FromUuid(Constants.RfcommServiceUuid));
    //
    //Initialize SDP Attributes here
    //
    var listener = new StreamSocketListener();
    var rfcommID = rfcommServiceProvider.ServiceId.AsString();
    await listener.BindServiceNameAsync(rfcommID, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
    try
    {
        rfcommServiceProvider.StartAdvertising(listener, true);
        listener.ConnectionReceived += Listener_ConnectionReceived;
        Debug.WriteLine("Now Discoverable");
    }
    catch
    {
        Debug.WriteLine("Failed to set Discoverability.");
    }