Bluetooth RFCOMM
이 항목은 UWP(Universal Windows Platform) 앱의 Bluetooth RFCOMM에 대한 개요와 함께 파일을 보내거나 받는 방법에 대한 예시 코드를 제공합니다.
중요 API
중요
Package.appxmanifest에서 "bluetooth" 기능을 선언해야 합니다.
<Capabilities> <DeviceCapability Name="bluetooth" /> </Capabilities>
개요
Windows.Devices.Bluetooth.Rfcomm 네임스페이스의 API는 enumeration 및 instantiation을 포함하여 Windows.Devices에 대한 기존 패턴을 기반으로 합니다. 데이터 읽기 및 쓰기는 설정된 데이터 스트림 패턴을 활용하고 Windows.Storage.Streams의 개체를 사용하도록 디자인되었습니다. SDP(Service Discovery Protocol) 특성마다 값과 필요한 형식이 있습니다. 그러나 일부 일반적인 디바이스에는 값이 예상 형식이 아닌 SDP 특성의 잘못된 구현이 있습니다. 또한 여러 RFCOMM 사용에는 추가 SDP 특성이 필요하지 않습니다. 이러한 이유로, 이 API는 개발자가 필요한 정보를 얻을 수 있는 구분 분석되지 않은 SDP 데이터에 대한 액세스를 제공합니다.
RFCOMM API에는 서비스 식별자 개념이 사용됩니다. 서비스 식별자는 128비트 GUID지만 일반적으로는 16비트 또는 32비트 정수로도 지정됩니다. RFCOMM API는 128비트 GUID는 물론 32비트 정수로도 지정 및 사용할 수 있도록 하는 서비스 식별자용 래퍼를 제공하지만, 16비트 정수는 제공하지 않습니다. 언어가 32비트 정수까지 자동으로 확대되고 식별자가 여전히 올바르게 생성될 수 있으므로 이는 API에 문제가 되지 않습니다.
앱은 백그라운드 작업으로 다단계 디바이스 작업을 수행할 수 있으므로 앱이 백그라운드로 이동되고 일시 중단된 경우에도 실행을 완료할 수 있습니다. 따라서 사용자가 앉아서 진행률 표시기를 지켜보지 않더라도 콘텐츠 동기화와 지속적인 설정 또는 펌웨어 변경 등 신뢰할 수 있는 디바이스 서비스가 가능합니다. 디바이스 서비스에는 DeviceServicingTrigger를 사용하고 콘텐츠 동기화에는 DeviceUseTrigger를 사용합니다. 이러한 백그라운드 작업은 앱이 백그라운드에서 실행될 수 있는 시간을 제한하며, 무기한 작업이나 무기한 동기화를 허용하지 않습니다.
RFCOMM 작업에 대해 자세히 설명하는 전체 코드 샘플은 GitHub의 Bluetooth Rfcomm 채팅 샘플을 참조하세요.
파일을 클라이언트로 보내기
파일을 보낼 때 가장 기본적인 시나리오는 원하는 서비스에 따라 연결된 디바이스에 연결하는 것입니다. 여기에는 다음 단계가 포함됩니다.
- RfcommDeviceService.GetDeviceSelector* 함수를 사용하여 원하는 서비스의 쌍으로 구성된 디바이스 인스턴스를 열거하는 데 사용할 수 있는 AQS(고급 쿼리 구문) 쿼리를 생성할 수 있습니다.
- 열거된 디바이스를 선택하고, RfcommDeviceService를 만들고, 필요에 따라 SDP 특성을 읽습니다(established data helpers를 사용하여 특성 데이터 구문 분석).
- 소켓을 만들고 RfcommDeviceService.ConnectionHostName 및 RfcommDeviceService.ConnectionServiceName 속성을 사용하여 적절한 매개 변수로 원격 디바이스 서비스에 StreamSocket.ConnectAsync합니다.
- 설정된 데이터 스트림 패턴을 따라 파일의 데이터 청크를 읽어와 소켓의 StreamSocket.OutputStream을 통해 디바이스로 보낼 수 있습니다.
using System;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Devices.Bluetooth;
Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService _service;
Windows.Networking.Sockets.StreamSocket _socket;
async void Initialize()
{
// Enumerate devices with the object push service
var services =
await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
RfcommDeviceService.GetDeviceSelector(
RfcommServiceId.ObexObjectPush));
if (services.Count > 0)
{
// Initialize the target Bluetooth BR device
var service = await RfcommDeviceService.FromIdAsync(services[0].Id);
bool isCompatibleVersion = await IsCompatibleVersionAsync(service);
// Check that the service meets this App's minimum requirement
if (SupportsProtection(service) && isCompatibleVersion)
{
_service = service;
// Create a socket and connect to the target
_socket = new StreamSocket();
await _socket.ConnectAsync(
_service.ConnectionHostName,
_service.ConnectionServiceName,
SocketProtectionLevel
.BluetoothEncryptionAllowNullAuthentication);
// The socket is connected. At this point the App can wait for
// the user to take some action, for example, 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 it's authenticated.
bool SupportsProtection(RfcommDeviceService service)
{
switch (service.ProtectionLevel)
{
case SocketProtectionLevel.PlainSocket:
if ((service.MaxProtectionLevel == SocketProtectionLevel
.BluetoothEncryptionWithAuthentication)
|| (service.MaxProtectionLevel == SocketProtectionLevel
.BluetoothEncryptionAllowNullAuthentication))
{
// 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;
}
else
{
// 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.
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A; // UINT32
const uint MINIMUM_SERVICE_VERSION = 200;
async Task<bool> IsCompatibleVersionAsync(RfcommDeviceService service)
{
var attributes = await service.GetSdpRawAttributesAsync(
BluetoothCacheMode.Uncached);
var attribute = attributes[SERVICE_VERSION_ATTRIBUTE_ID];
var 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.ReadUInt32();
return version >= MINIMUM_SERVICE_VERSION;
}
else return false;
}
...
#include <winrt/Windows.Devices.Bluetooth.Rfcomm.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Networking.Sockets.h>
#include <winrt/Windows.Storage.Streams.h>
...
Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService m_service{ nullptr };
Windows::Networking::Sockets::StreamSocket m_socket;
Windows::Foundation::IAsyncAction Initialize()
{
// Enumerate devices with the object push service.
Windows::Devices::Enumeration::DeviceInformationCollection services{
co_await Windows::Devices::Enumeration::DeviceInformation::FindAllAsync(
Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService::GetDeviceSelector(
Windows::Devices::Bluetooth::Rfcomm::RfcommServiceId::ObexObjectPush())) };
if (services.Size() > 0)
{
// Initialize the target Bluetooth BR device.
Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService service{
co_await Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService::FromIdAsync(services.GetAt(0).Id()) };
// Check that the service meets this App's minimum
// requirement
if (SupportsProtection(service)
&& co_await IsCompatibleVersion(service))
{
m_service = service;
// Create a socket and connect to the target
co_await m_socket.ConnectAsync(
m_service.ConnectionHostName(),
m_service.ConnectionServiceName(),
Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionAllowNullAuthentication);
// The socket is connected. At this point the App can
// wait for the user to take some action, for example, 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 it's authenticated.
bool SupportsProtection(Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService const& service)
{
switch (service.ProtectionLevel())
{
case Windows::Networking::Sockets::SocketProtectionLevel::PlainSocket:
if ((service.MaxProtectionLevel() == Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionWithAuthentication)
|| (service.MaxProtectionLevel() == Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionAllowNullAuthentication))
{
// 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;
}
else
{
// The connection cannot be upgraded so an App may offer UI here
// to explain why a connection won't be made.
return false;
}
case Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionWithAuthentication:
return true;
case Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionAllowNullAuthentication:
return true;
}
return false;
}
// This application relies on CRC32 checking available in version 2.0 of the service.
const uint32_t SERVICE_VERSION_ATTRIBUTE_ID{ 0x0300 };
const byte SERVICE_VERSION_ATTRIBUTE_TYPE{ 0x0A }; // UINT32.
const uint32_t MINIMUM_SERVICE_VERSION{ 200 };
Windows::Foundation::IAsyncOperation<bool> IsCompatibleVersion(Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService const& service)
{
auto attributes{
co_await service.GetSdpRawAttributesAsync(Windows::Devices::Bluetooth::BluetoothCacheMode::Uncached) };
auto attribute{ attributes.Lookup(SERVICE_VERSION_ATTRIBUTE_ID) };
auto reader{ Windows::Storage::Streams::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
uint32_t version{ reader.ReadUInt32() };
co_return (version >= MINIMUM_SERVICE_VERSION);
}
}
...
Windows::Devices::Bluetooth::Rfcomm::RfcommDeviceService^ _service;
Windows::Networking::Sockets::StreamSocket^ _socket;
void Initialize()
{
// Enumerate devices with the object push service
create_task(
Windows::Devices::Enumeration::DeviceInformation::FindAllAsync(
RfcommDeviceService::GetDeviceSelector(
RfcommServiceId::ObexObjectPush)))
.then([](DeviceInformationCollection^ services)
{
if (services->Size > 0)
{
// Initialize the target Bluetooth BR device
create_task(RfcommDeviceService::FromIdAsync(services[0]->Id))
.then([](RfcommDeviceService^ service)
{
// 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 = ref new StreamSocket();
create_task(_socket->ConnectAsync(
_service->ConnectionHostName,
_service->ConnectionServiceName,
SocketProtectionLevel
::BluetoothEncryptionAllowNullAuthentication)
.then([](void)
{
// The socket is connected. At this point the App can
// wait for the user to take some action, for example, 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 it's authenticated.
bool SupportsProtection(RfcommDeviceService^ service)
{
switch (service->ProtectionLevel)
{
case SocketProtectionLevel->PlainSocket:
if ((service->MaxProtectionLevel == SocketProtectionLevel
::BluetoothEncryptionWithAuthentication)
|| (service->MaxProtectionLevel == SocketProtectionLevel
::BluetoothEncryptionAllowNullAuthentication))
{
// 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;
}
else
{
// 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.
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A; // UINT32
const uint MINIMUM_SERVICE_VERSION = 200;
bool IsCompatibleVersion(RfcommDeviceService^ service)
{
auto attributes = await service->GetSdpRawAttributesAsync(
BluetoothCacheMode::Uncached);
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->ReadUInt32();
return version >= MINIMUM_SERVICE_VERSION;
}
}
파일을 서버로 받기
또 다른 일반적인 RFCOMM 앱 시나리오는 PC에서 서비스를 호스팅하고 다른 디바이스를 위해 이 서비스를 표시하는 것입니다.
- RfcommServiceProvider를 만들어 원하는 서비스를 보급합니다.
- 필요에 따라 established data helpers를 사용하여 특성 데이터를 생성함으로써 SDP 특성을 설정하고 다른 디바이스에서 검색하도록 SDP 레코드를 알리기 시작합니다.
- 클라이언트 디바이스에 연결하기 위해 소켓 수신기를 만들어서 들어오는 연결 요청에 대한 수신 대기를 시작합니다.
- 연결이 수신되면 향후 처리를 위해 연결된 소켓을 저장합니다.
- 설정된 데이터 스트림 패턴을 따라서 소켓의 InputStream에서 데이터 청크를 읽어와 파일에 저장합니다.
백그라운드에서 RFCOMM 서비스를 유지하려면 RfcommConnectionTrigger를 사용합니다. 서비스에 연결할 때 백그라운드 작업이 트리거됩니다. 개발자는 백그라운드 작업에서 소켓에 대한 핸들을 수신합니다. 백그라운드 작업은 오랫동안 실행되며 소켓이 사용되는 동안 유지됩니다.
Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider _provider;
async void Initialize()
{
// Initialize the provider for the hosted RFCOMM service
_provider =
await Windows.Devices.Bluetooth.Rfcomm.RfcommServiceProvider.CreateAsync(
RfcommServiceId.ObexObjectPush);
// Create a listener for this service and start listening
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnectionReceivedAsync;
await listener.BindServiceNameAsync(
_provider.ServiceId.AsString(),
SocketProtectionLevel
.BluetoothEncryptionAllowNullAuthentication);
// Set the SDP attributes and start advertising
InitializeServiceSdpAttributes(_provider);
_provider.StartAdvertising(listener);
}
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A; // UINT32
const uint SERVICE_VERSION = 200;
void InitializeServiceSdpAttributes(RfcommServiceProvider provider)
{
Windows.Storage.Streams.DataWriter writer = new Windows.Storage.Streams.DataWriter();
// First write the attribute type
writer.WriteByte(SERVICE_VERSION_ATTRIBUTE_TYPE);
// Then write the data
writer.WriteUInt32(MINIMUM_SERVICE_VERSION);
IBuffer data = writer.DetachBuffer();
provider.SdpRawAttributes.Add(SERVICE_VERSION_ATTRIBUTE_ID, data);
}
void OnConnectionReceivedAsync(
StreamSocketListener listener,
StreamSocketListenerConnectionReceivedEventArgs args)
{
// Stop advertising/listening so that we're only serving one client
_provider.StopAdvertising();
listener.Dispose();
_socket = args.Socket;
// The client socket is connected. At this point the App can wait for
// the user to take some action, for example, click a button to receive a file
// from the device, which could invoke the Picker and then save the
// received file to the picked location. The transfer itself would use
// the Sockets API and not the Rfcomm API, and so is omitted here for
// brevity.
}
...
#include <winrt/Windows.Devices.Bluetooth.Rfcomm.h>
#include <winrt/Windows.Networking.Sockets.h>
#include <winrt/Windows.Storage.Streams.h>
...
Windows::Devices::Bluetooth::Rfcomm::RfcommServiceProvider m_provider{ nullptr };
Windows::Networking::Sockets::StreamSocket m_socket;
Windows::Foundation::IAsyncAction Initialize()
{
// Initialize the provider for the hosted RFCOMM service.
auto provider{ co_await Windows::Devices::Bluetooth::Rfcomm::RfcommServiceProvider::CreateAsync(
Windows::Devices::Bluetooth::Rfcomm::RfcommServiceId::ObexObjectPush()) };
m_provider = provider;
// Create a listener for this service and start listening.
Windows::Networking::Sockets::StreamSocketListener listener;
listener.ConnectionReceived({ this, &MainPage::OnConnectionReceived });
co_await listener.BindServiceNameAsync(m_provider.ServiceId().AsString(),
Windows::Networking::Sockets::SocketProtectionLevel::BluetoothEncryptionAllowNullAuthentication);
// Set the SDP attributes and start advertising
InitializeServiceSdpAttributes();
m_provider.StartAdvertising(listener);
}
const uint32_t SERVICE_VERSION_ATTRIBUTE_ID{ 0x0300 };
const byte SERVICE_VERSION_ATTRIBUTE_TYPE{ 0x0A }; // UINT32.
const uint32_t SERVICE_VERSION{ 200 };
void InitializeServiceSdpAttributes()
{
Windows::Storage::Streams::DataWriter writer;
// First write the attribute type
writer.WriteByte(SERVICE_VERSION_ATTRIBUTE_TYPE);
// Then write the data
writer.WriteUInt32(SERVICE_VERSION);
auto data{ writer.DetachBuffer() };
m_provider.SdpRawAttributes().Insert(SERVICE_VERSION_ATTRIBUTE_ID, data);
}
void OnConnectionReceived(
Windows::Networking::Sockets::StreamSocketListener const& listener,
Windows::Networking::Sockets::StreamSocketListenerConnectionReceivedEventArgs const& args)
{
// Stop advertising/listening so that we're only serving one client
m_provider.StopAdvertising();
listener.Close();
m_socket = args.Socket();
// The client socket is connected. At this point the application can wait for
// the user to take some action, for example, click a button to receive a
// file from the device, which could invoke the Picker and then save
// the received file to the picked location. The transfer itself
// would use the Sockets API and not the Rfcomm API, and so is
// omitted here for brevity.
}
...
Windows::Devices::Bluetooth::Rfcomm::RfcommServiceProvider^ _provider;
Windows::Networking::Sockets::StreamSocket^ _socket;
void Initialize()
{
// Initialize the provider for the hosted RFCOMM service
create_task(Windows::Devices::Bluetooth.
RfcommServiceProvider::CreateAsync(
RfcommServiceId::ObexObjectPush))
.then([](RfcommServiceProvider^ provider) -> task<void> {
_provider = provider;
// Create a listener for this service and start listening
auto listener = ref new StreamSocketListener();
listener->ConnectionReceived += ref new TypedEventHandler<
StreamSocketListener^,
StreamSocketListenerConnectionReceivedEventArgs^>
(&OnConnectionReceived);
return create_task(listener->BindServiceNameAsync(
_provider->ServiceId->AsString(),
SocketProtectionLevel
::BluetoothEncryptionAllowNullAuthentication));
}).then([listener](void) {
// Set the SDP attributes and start advertising
InitializeServiceSdpAttributes(_provider);
_provider->StartAdvertising(listener);
});
}
const uint SERVICE_VERSION_ATTRIBUTE_ID = 0x0300;
const byte SERVICE_VERSION_ATTRIBUTE_TYPE = 0x0A; // UINT32
const uint SERVICE_VERSION = 200;
void InitializeServiceSdpAttributes(RfcommServiceProvider^ provider)
{
auto writer = ref new Windows::Storage::Streams::DataWriter();
// First write the attribute type
writer->WriteByte(SERVICE_VERSION_ATTRIBUTE_TYPE);
// Then write the data
writer->WriteUInt32(SERVICE_VERSION);
auto data = writer->DetachBuffer();
provider->SdpRawAttributes->Add(SERVICE_VERSION_ATTRIBUTE_ID, data);
}
void OnConnectionReceived(
StreamSocketListener^ listener,
StreamSocketListenerConnectionReceivedEventArgs^ args)
{
// Stop advertising/listening so that we're only serving one client
_provider->StopAdvertising();
create_task(listener->Close())
.then([args](void) {
_socket = args->Socket;
// The client socket is connected. At this point the App can wait for
// the user to take some action, for example, click a button to receive a
// file from the device, which could invoke the Picker and then save
// the received file to the picked location. The transfer itself
// would use the Sockets API and not the Rfcomm API, and so is
// omitted here for brevity.
});
}