Bluetooth API에는 블루투스 프로필 작업 지원이 포함되어 있습니다. 블루투스 프로필은 핸즈프리 프로필과 같이 기기 간 블루투스 기반 통신을 위한 무선 인터페이스 사양입니다. 휴대기기를 무선 헤드셋에 연결하려면 두 기기 모두 핸즈프리 프로필을 지원해야 합니다.
블루투스 API는 다음과 같은 블루투스 프로필의 구현을 제공합니다.
- 헤드셋 헤드셋 프로필은 블루투스 헤드셋을 휴대전화와 함께 사용할 수 있도록 지원합니다. Android는 블루투스 헤드셋 서비스를 제어하는 프록시인
BluetoothHeadset클래스를 제공합니다. 여기에는 블루투스 헤드셋 및 핸즈프리(v1.5) 프로필이 모두 포함됩니다.BluetoothHeadset클래스에는 AT 명령어 지원이 포함됩니다. 이 주제에 관한 자세한 내용은 공급업체별 AT 명령어를 참고하세요. - A2DP 고급 오디오 전송 프로필(A2DP)은 블루투스 연결을 통해 한 기기에서 다른 기기로 고품질 오디오를 스트리밍하는 방법을 정의합니다. Android는 블루투스 A2DP 서비스를 제어하는 프록시인
BluetoothA2dp클래스를 제공합니다. - Health 기기. Android는 블루투스 건강 기기 프로필(HDP)을 지원합니다. 이를 통해 블루투스를 사용하여 심박수 모니터, 혈압계, 온도계, 체중계와 같이 블루투스를 지원하는 건강 기기와 통신하는 앱을 만들 수 있습니다. 지원되는 기기 및 해당 기기 데이터 전문화 코드 목록은 블루투스의 HDP 기기 데이터 전문화를 참고하세요. 이러한 값은 ISO/IEEE 11073-20601 [7] 사양에서 명명법 코드 부록의
MDC_DEV_SPEC_PROFILE_*로도 참조됩니다. HDP에 관한 자세한 내용은 건강 기기 프로필을 참고하세요.
프로필을 사용하는 기본 단계는 다음과 같습니다.
- 블루투스 설정에 설명된 대로 기본 어댑터를 가져옵니다.
BluetoothProfile.ServiceListener를 설정합니다. 이 리스너는BluetoothProfile클라이언트가 서비스에 연결되거나 연결 해제될 때 클라이언트에 알립니다.getProfileProxy()를 사용하여 프로필과 연결된 프로필 프록시 객체에 연결을 설정합니다. 다음 예에서 프로필 프록시 객체는BluetoothHeadset의 인스턴스입니다.onServiceConnected()에서 프로필 프록시 객체에 대한 핸들을 가져옵니다.- 프로필 프록시 객체가 있으면 이를 사용하여 연결 상태를 모니터링하고 해당 프로필과 관련된 다른 작업을 실행합니다.
다음 코드 스니펫은 헤드셋 프로필을 제어할 수 있도록 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)
자바
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(BluetoothProfile.HEADSET, bluetoothHeadset);
공급업체별 AT 명령어
앱은 헤드셋에서 전송하는 사전 정의된 공급업체별 AT 명령어(예: Plantronics +XEVENT 명령어)의 시스템 브로드캐스트를 수신하도록 등록할 수 있습니다. 예를 들어 앱은 연결된 기기의 배터리 잔량을 나타내는 브로드캐스트를 수신하고 필요에 따라 사용자에게 알리거나 다른 작업을 실행할 수 있습니다. ACTION_VENDOR_SPECIFIC_HEADSET_EVENT 인텐트의 broadcast receiver를 만들어 헤드셋의 공급업체별 AT 명령어를 처리합니다.
건강 관리 기기 프로필
Android는 블루투스 건강 기기 프로필(HDP)을 지원합니다. Bluetooth Health API에는 주요 클래스 및 인터페이스에 설명된 BluetoothHealth, BluetoothHealthCallback, BluetoothHealthAppConfiguration 클래스가 포함되어 있습니다.
블루투스 Health API를 사용할 때는 다음과 같은 주요 HDP 개념을 이해하는 것이 좋습니다.
- 소스
- 의료 데이터를 스마트 기기(예: Android 휴대전화 또는 태블릿)로 전송하는 건강 기기(예: 체중계, 혈당 측정기, 체온계)
- 싱크
- 의료 데이터를 수신하는 스마트 기기입니다. HDP 앱에서 싱크는
BluetoothHealthAppConfiguration객체로 표현됩니다. - 등록
- 특정 건강 기기와 통신하기 위해 싱크를 등록하는 데 사용되는 프로세스입니다.
- 연결
- 건강 기기(소스)와 스마트 기기(싱크) 간에 채널을 여는 데 사용되는 프로세스입니다.
HDP 앱 만들기
HDP 앱을 만드는 기본 단계는 다음과 같습니다.
BluetoothHealth프록시 객체에 대한 참조를 가져옵니다. 일반 헤드셋 및 A2DP 프로필 기기와 마찬가지로BluetoothProfile.ServiceListener및HEALTH프로필 유형으로getProfileProxy()를 호출하여 프로필 프록시 객체와 연결을 설정해야 합니다.BluetoothHealthCallback를 만들고 상태 싱크 역할을 하는 앱 구성(BluetoothHealthAppConfiguration)을 등록합니다.건강 기기에 연결을 설정합니다.
건강 기기에 성공적으로 연결되면 파일 설명자를 사용하여 건강 기기에 읽고 씁니다. 수신된 데이터는 IEEE 11073 사양을 구현하는 건강 관리자를 사용하여 해석해야 합니다.
완료되면 상태 채널을 닫고 앱을 등록 해제합니다. 장기간 활동이 없는 경우에도 채널이 닫힙니다.