RFCOMM Scenario: Send File as a Client
Language: HTML | XAML

RFCOMM Scenario: Send File as a Client(XAML)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

The basic App scenario is to connect to a paired device based on a desired service. In this scenario, the developer can use the provided RfcommDeviceService.GetDeviceSelector* functions to help generate an AQS query that can be used to enumerated paired device instances of the desired service. From there, the App developer picks an enumerated device, creates an RfcommDeviceService, and reads the SDP attributes as needed (using established data helpers to parse the attribute’s Data). The App developer can then create a socket and use the RfcommDeviceService’s ConnectionHostName and ConnectionServiceName property to ConnectAsync to the remote device service with the appropriate parameters. When it’s time to send a file, the App developer can follow established data stream patterns to read chunks of data from the file and send it on the socket’s OutputStream to the device.

Windows.Devices.Bluetooth.RfcommDeviceService _service;
Windows.Networking.Sockets.StreamSocket _socket;

async void Initialize()
    // Enumerate devices with the object push service
    auto services =
        await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(

    if (services.Count > 0) 
        // Initialize the target Bluetooth BR device
        auto service = await RfcommDeviceService.FromIdAsync(services[0].Id);

        // Check that the service meets this App’s minimum requirement
        if (SupportsProtection(service) && IsCompatibleVersion(service))
            _service = service;

            // Create a socket and connect to the target
            _socket = new StreamSocket();
            await _socket.ConnectAsync(

            // The socket is connected. At this point the App can wait for
            // the user to take some action, e.g. click a button to send a
            // file to the device, which could invoke the Picker and then
            // send the picked file. The transfer itself would use the
            // Sockets API and not the Rfcomm API, and so is omitted here for
            // brevity.

// This App requires a connection that is encrypted but does not care about
// whether its authenticated.
bool SupportsProtection(RfcommDeviceService service)
    switch (service.ProtectionLevel)
    case SocketProtectionLevel.PlainSocket:
        if ((service.MaximumProtectionLevel == SocketProtectionLevel
            || (service.MaximumProtectionLevel == SocketProtectionLevel
            // The connection can be upgraded when opening the socket so the
            // App may offer UI here to notify the user that Windows may
            // prompt for a PIN exchange.
            return true;
            // The connection cannot be upgraded so an App may offer UI here
            // to explain why a connection won’t be made.
            return false;
    case SocketProtectionLevel.BluetoothEncryptionWithAuthentication:
        return true;
    case SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication:
        return true;
    return false;

// This App relies on CRC32 checking available in version 2.0 of the service.
bool IsCompatibleVersion(RfcommDeviceService service)
    auto attributes = await service.GetSdpRawAttributesAsync(
    auto attribute = attributes[SERVICE_VERSION_ATTRIBUTE_ID];
    auto reader = DataReader.FromBuffer(attribute);

    // The first byte contains the attribute's type
    byte attributeType = reader.ReadByte();
    if (attributeType == SERVICE_VERSION_ATTRIBUTE_TYPE)
        // The remainder is the data
        uint version = reader.Uint32();
        return version >= MINIMUM_SERVICE_VERSION;



© 2017 Microsoft