本指南將說明如何使用 Telecom API,並為 IP 網路語音傳遞技術通話。閱讀 「建構通話應用程式」指南 才能繼續。
使用 ConnectionService
和 Connection
門課程,您可以存取
音訊狀態以及可用藍牙裝置的清單,
音訊上傳到指定的藍牙裝置。
VoIP 連線和 ConnectionService
建立可擴充的 VoIPConnection
類別
Connection
。這個類別可控制目前通話的狀態。身為
「建構通話應用程式」指南
請設為自行管理的應用程式,並設定 VoIP 的音訊模式
應用程式。
class VoIPConnection : Connection() {
init {
setConnectionProperties(PROPERTY_SELF_MANAGED)
setAudioModeIsVoip(true)
}
}
public class VoIPConnection extends Connection {
public VoIPConnection() {
setConnectionProperties(PROPERTY_SELF_MANAGED);
setAudioModeIsVoip(true);
}
}
接著,在
ConnectionService
表示
。
class VoIPConnectionService : ConnectionService() {
override fun onCreateOutgoingConnection(
connectionManagerPhoneAccount: PhoneAccountHandle,
request: ConnectionRequest,
): Connection {
return VoIPConnection()
}
}
public class VoIPConnectionService extends ConnectionService {
@Override
public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount, ConnectionRequest request) {
return new VoIPConnection();
}
}
確認資訊清單正確指向 VoIPConnectionService
類別。
<service android:name=".voip.TelegramConnectionService" android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.ConnectionService"/>
</intent-filter>
</service>
使用自訂 Connection
和
ConnectionService
門課程,你已經
可控制要在執行期間使用
呼叫。
取得目前的音訊狀態
如要取得目前的音訊狀態,請呼叫
getCallAudioState()
。
getCallAudioState()
敬上
會在裝置使用藍牙、耳機、有線或
喇叭。
mAudioState = connection.getCallAudioState()
開啟狀態已變更
透過覆寫來訂閱 CallAudioState 變更
onCallAudioStateChanged()
。
一旦狀態有任何變更,就會通知您。
fun onCallAudioStateChanged(audioState: CallAudioState) {
mAudioState = audioState
}
@Override
public void onCallAudioStateChanged(CallAudioState audioState) {
mAudioState = audioState;
}
取得目前的裝置
透過以下方式取得目前使用中的裝置:
CallAudioState.getActiveBluetoothDevice()
。
這個函式會傳回使用中的藍牙裝置。
val activeDevice: BluetoothDevice = mAudioState.getActiveBluetoothDevice()
BluetoothDevice activeDevice = mAudioState.getActiveBluetoothDevice();
取得藍牙裝置
透過
CallAudioState.getSupportedBluetoothDevices()
。
轉送通話音訊
使用 API 級別 28 及以上級別 (建議)
使用以下方式將通話音訊轉送至可用的藍牙裝置
requestBluetoothAudio(BluetoothDevice)
:
requestBluetoothAudio(availableBluetoothDevices[0]);
使用 API 級別 23 及以上級別
啟用
ROUTE_BLUETOOTH
敬上
不必指定裝置
setAudioRoute(int)
。
根據預設,系統會為搭載 Android 9 以上版本的目前使用中的藍牙裝置提供預設路徑。
setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);