블루투스 프로필

Bluetooth API에는 Bluetooth 프로필 작업을 위한 지원 기능이 포함되어 있습니다. 블루투스 프로필은 핸즈프리 프로필과 같은 기기 간의 블루투스 기반 통신을 위한 무선 인터페이스 사양입니다. 휴대기기를 무선 헤드셋에 연결하려면 두 기기 모두 핸즈프리 프로필을 지원해야 합니다.

Bluetooth API는 다음 블루투스 프로필의 구현을 제공합니다.

  • 헤드셋 헤드셋 프로필은 블루투스 헤드셋을 휴대전화와 함께 사용할 수 있도록 지원합니다. Android는 블루투스 헤드셋 서비스를 제어하는 프록시인 BluetoothHeadset 클래스를 제공합니다. 여기에는 블루투스 헤드셋 및 핸즈프리 (v1.5) 프로필이 모두 포함됩니다. BluetoothHeadset 클래스에는 AT 명령어 지원이 포함됩니다. 이 주제에 관한 자세한 내용은 공급업체별 AT 명령어를 참고하세요.
  • A2DP 고급 오디오 전송 프로필 (A2DP) 프로필은 블루투스 연결을 통해 고품질 오디오를 한 기기에서 다른 기기로 스트리밍하는 방법을 정의합니다. Android는 블루투스 A2DP 서비스를 제어하는 프록시인 BluetoothA2dp 클래스를 제공합니다.
  • 의료 기기. Android는 블루투스 의료 기기 프로필 (HDP)을 지원합니다. 이를 통해 블루투스를 사용하여 심박수 모니터, 혈압계, 체온계, 체중계 등 블루투스를 지원하는 의료 기기와 통신하는 앱을 만들 수 있습니다. 지원되는 기기 및 상응하는 기기 데이터 전문화 코드의 목록은 블루투스의 HDP 기기 데이터 전문화를 참고하세요. 이러한 값은 ISO/IEEE 11073-20601 [7] 사양에서 명명법 코드 부록의 MDC_DEV_SPEC_PROFILE_*로 참조됩니다. HDP에 관한 자세한 내용은 의료 기기 프로필을 참고하세요.

다음은 기본적인 프로필 작업 단계입니다.

  1. 블루투스 설정에 설명된 대로 기본 어댑터를 가져옵니다.
  2. BluetoothProfile.ServiceListener를 설정합니다. 이 리스너는 서비스에 연결되었거나 연결 해제되었을 때 BluetoothProfile 클라이언트에 알립니다.
  3. getProfileProxy()를 사용하여 프로필과 연결된 프로필 프록시 객체에 연결을 설정합니다. 다음 예에서 프로필 프록시 객체는 BluetoothHeadset의 인스턴스입니다.
  4. onServiceConnected()에서 프로필 프록시 객체에 대한 핸들을 가져옵니다.
  5. 프로필 프록시 객체가 있으면 이를 사용하여 연결 상태를 모니터링하고 해당 프로필과 관련된 다른 작업을 수행합니다.

다음 코드 스니펫은 헤드셋 프로필을 제어할 수 있도록 BluetoothHeadset 프록시 객체에 연결하는 방법을 보여줍니다.

Kotlin

var bluetoothHeadset: BluetoothHeadset? = null

// Get the default adapter
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()

private val profileListener = object : BluetoothProfile.ServiceListener {

    override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = proxy as BluetoothHeadset
        }
    }

    override fun onServiceDisconnected(profile: Int) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = null
        }
    }
}

// Establish connection to the proxy.
bluetoothAdapter?.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET)

// ... call functions on bluetoothHeadset

// Close proxy connection after use.
bluetoothAdapter?.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset)

Java

BluetoothHeadset bluetoothHeadset;

// Get the default adapter
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

private BluetoothProfile.ServiceListener profileListener = new BluetoothProfile.ServiceListener() {
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = (BluetoothHeadset) proxy;
        }
    }
    public void onServiceDisconnected(int profile) {
        if (profile == BluetoothProfile.HEADSET) {
            bluetoothHeadset = null;
        }
    }
};

// Establish connection to the proxy.
bluetoothAdapter.getProfileProxy(context, profileListener, BluetoothProfile.HEADSET);

// ... call functions on bluetoothHeadset

// Close proxy connection after use.
bluetoothAdapter.closeProfileProxy(bluetoothHeadset);

공급업체 특정 AT 명령어

앱은 헤드셋에서 보낸 사전 정의된 공급업체별 AT 명령어 (예: Plantronics +XEVENT 명령어)의 시스템 브로드캐스트를 수신하도록 등록할 수 있습니다. 예를 들어 앱은 연결된 기기의 배터리 수준을 나타내는 브로드캐스트를 수신하여 사용자에게 알리거나 필요에 따라 다른 조치를 취할 수 있습니다. ACTION_VENDOR_SPECIFIC_HEADSET_EVENT 인텐트의 broadcast receiver를 만들어 헤드셋에 관한 공급업체별 AT 명령어를 처리합니다.

건강 관리 기기 프로필

Android는 블루투스 의료 기기 프로필 (HDP)을 지원합니다. Bluetooth Health API에는 키 클래스 및 인터페이스에 설명된 BluetoothHealth, BluetoothHealthCallback, BluetoothHealthAppConfiguration 클래스가 포함되어 있습니다.

Bluetooth Health API를 사용할 때 다음과 같은 주요 HDP 개념을 이해하면 도움이 됩니다.

소스
Android 휴대전화나 태블릿과 같은 스마트 기기로 의료 데이터를 전송하는 체중계, 혈당 측정기, 체온계와 같은 건강 기기
싱크
의료 데이터를 수신하는 스마트 기기입니다. HDP 앱에서 싱크는 BluetoothHealthAppConfiguration 객체로 표시됩니다.
등록
특정 의료 기기와 통신하기 위해 싱크를 등록하는 데 사용되는 프로세스입니다.
연대감
의료 기기 (소스)와 스마트 기기 (싱크) 사이의 채널을 여는 데 사용되는 프로세스입니다.

HDP 앱 만들기

다음은 HDP 앱을 만드는 기본적인 단계입니다.

  1. BluetoothHealth 프록시 객체에 대한 참조를 가져옵니다. 일반 헤드셋 및 A2DP 프로필 기기와 마찬가지로 BluetoothProfile.ServiceListenerHEALTH 프로필 유형으로 getProfileProxy()를 호출하여 프로필 프록시 객체와의 연결을 설정해야 합니다.

  2. BluetoothHealthCallback를 만들고 상태 싱크 역할을 하는 앱 구성(BluetoothHealthAppConfiguration)을 등록합니다.

  3. 의료 기기 연결을 설정합니다.

  4. 의료 기기에 성공적으로 연결되면 파일 설명자를 사용하여 의료 기기를 읽고 씁니다. 수신된 데이터는 IEEE 11073 사양을 구현하는 의료 관리자를 사용하여 해석해야 합니다.

  5. 완료되면 건강 채널을 닫고 앱을 등록 취소합니다. 확장된 비활성이 있는 경우에도 채널이 닫힙니다.