@stream-io/video-react-native-sdk 1.21.2 → 1.22.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/gradle.properties +3 -3
- package/android/src/main/java/com/streamvideo/reactnative/StreamVideoReactNativeModule.kt +10 -0
- package/android/src/main/java/com/streamvideo/reactnative/StreamVideoReactNativePackage.kt +2 -1
- package/android/src/main/java/com/streamvideo/reactnative/audio/AudioDeviceManager.kt +592 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/BluetoothManager.kt +755 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/utils/AudioDeviceEndpointUtils.kt +192 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/utils/AudioFocusUtil.kt +62 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/utils/AudioManagerUtil.kt +159 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/utils/AudioSetupStoreUtil.kt +41 -0
- package/android/src/main/java/com/streamvideo/reactnative/audio/utils/WebRtcAudioUtils.kt +250 -0
- package/android/src/main/java/com/streamvideo/reactnative/callmanager/StreamInCallManagerModule.kt +191 -0
- package/android/src/main/java/com/streamvideo/reactnative/model/AudioDeviceEndpoint.kt +136 -0
- package/android/src/main/java/org/webrtc/audio/WebRtcAudioTrackHelper.kt +12 -0
- package/dist/commonjs/components/Call/CallContent/CallContent.js +13 -9
- package/dist/commonjs/components/Call/CallContent/CallContent.js.map +1 -1
- package/dist/commonjs/components/Call/CallContent/RTCViewPipIOS.js +9 -2
- package/dist/commonjs/components/Call/CallContent/RTCViewPipIOS.js.map +1 -1
- package/dist/commonjs/components/Call/CallContent/RTCViewPipNative.js +3 -0
- package/dist/commonjs/components/Call/CallContent/RTCViewPipNative.js.map +1 -1
- package/dist/commonjs/components/Livestream/HostLivestream/HostLivestream.js +10 -6
- package/dist/commonjs/components/Livestream/HostLivestream/HostLivestream.js.map +1 -1
- package/dist/commonjs/components/Livestream/LivestreamControls/ViewerLivestreamControls.js +8 -4
- package/dist/commonjs/components/Livestream/LivestreamControls/ViewerLivestreamControls.js.map +1 -1
- package/dist/commonjs/components/Livestream/LivestreamLayout/LivestreamLayout.js +26 -27
- package/dist/commonjs/components/Livestream/LivestreamLayout/LivestreamLayout.js.map +1 -1
- package/dist/commonjs/components/Livestream/ViewerLivestream/ViewerLivestream.js +10 -6
- package/dist/commonjs/components/Livestream/ViewerLivestream/ViewerLivestream.js.map +1 -1
- package/dist/commonjs/hooks/useIsInPiPMode.js +3 -3
- package/dist/commonjs/hooks/useIsInPiPMode.js.map +1 -1
- package/dist/commonjs/hooks/usePermissionNotification.js +5 -5
- package/dist/commonjs/hooks/usePermissionNotification.js.map +1 -1
- package/dist/commonjs/index.js +12 -0
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/modules/call-manager/CallManager.js +113 -0
- package/dist/commonjs/modules/call-manager/CallManager.js.map +1 -0
- package/dist/commonjs/modules/call-manager/PrevLibDetection.js +18 -0
- package/dist/commonjs/modules/call-manager/PrevLibDetection.js.map +1 -0
- package/dist/commonjs/modules/call-manager/index.js +24 -0
- package/dist/commonjs/modules/call-manager/index.js.map +1 -0
- package/dist/commonjs/modules/call-manager/native-module.d.js +4 -0
- package/dist/commonjs/modules/call-manager/native-module.d.js.map +1 -0
- package/dist/commonjs/modules/call-manager/types.js +2 -0
- package/dist/commonjs/modules/call-manager/types.js.map +1 -0
- package/dist/commonjs/providers/StreamCall/AppStateListener.js +5 -5
- package/dist/commonjs/providers/StreamCall/AppStateListener.js.map +1 -1
- package/dist/commonjs/theme/theme.js.map +1 -1
- package/dist/commonjs/utils/internal/rxSubjects.js +2 -2
- package/dist/commonjs/utils/internal/rxSubjects.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/commonjs/version.js.map +1 -1
- package/dist/module/components/Call/CallContent/CallContent.js +14 -9
- package/dist/module/components/Call/CallContent/CallContent.js.map +1 -1
- package/dist/module/components/Call/CallContent/RTCViewPipIOS.js +9 -2
- package/dist/module/components/Call/CallContent/RTCViewPipIOS.js.map +1 -1
- package/dist/module/components/Call/CallContent/RTCViewPipNative.js +3 -0
- package/dist/module/components/Call/CallContent/RTCViewPipNative.js.map +1 -1
- package/dist/module/components/Livestream/HostLivestream/HostLivestream.js +10 -5
- package/dist/module/components/Livestream/HostLivestream/HostLivestream.js.map +1 -1
- package/dist/module/components/Livestream/LivestreamControls/ViewerLivestreamControls.js +9 -4
- package/dist/module/components/Livestream/LivestreamControls/ViewerLivestreamControls.js.map +1 -1
- package/dist/module/components/Livestream/LivestreamLayout/LivestreamLayout.js +26 -27
- package/dist/module/components/Livestream/LivestreamLayout/LivestreamLayout.js.map +1 -1
- package/dist/module/components/Livestream/ViewerLivestream/ViewerLivestream.js +10 -5
- package/dist/module/components/Livestream/ViewerLivestream/ViewerLivestream.js.map +1 -1
- package/dist/module/hooks/useIsInPiPMode.js +4 -4
- package/dist/module/hooks/useIsInPiPMode.js.map +1 -1
- package/dist/module/hooks/usePermissionNotification.js +7 -6
- package/dist/module/hooks/usePermissionNotification.js.map +1 -1
- package/dist/module/icons/Back.js +1 -1
- package/dist/module/icons/Back.js.map +1 -1
- package/dist/module/icons/CameraSwitch.js +1 -1
- package/dist/module/icons/CameraSwitch.js.map +1 -1
- package/dist/module/icons/Mic.js +1 -1
- package/dist/module/icons/Mic.js.map +1 -1
- package/dist/module/icons/MicOff.js +1 -1
- package/dist/module/icons/MicOff.js.map +1 -1
- package/dist/module/icons/Phone.js +1 -1
- package/dist/module/icons/Phone.js.map +1 -1
- package/dist/module/icons/PinVertical.js +1 -1
- package/dist/module/icons/PinVertical.js.map +1 -1
- package/dist/module/icons/Reaction.js +1 -1
- package/dist/module/icons/Reaction.js.map +1 -1
- package/dist/module/icons/Spotlight.js +1 -1
- package/dist/module/icons/Spotlight.js.map +1 -1
- package/dist/module/icons/Video.js +1 -1
- package/dist/module/icons/Video.js.map +1 -1
- package/dist/module/icons/VideoSlash.js +1 -1
- package/dist/module/icons/VideoSlash.js.map +1 -1
- package/dist/module/index.js +1 -0
- package/dist/module/index.js.map +1 -1
- package/dist/module/modules/call-manager/CallManager.js +106 -0
- package/dist/module/modules/call-manager/CallManager.js.map +1 -0
- package/dist/module/modules/call-manager/PrevLibDetection.js +12 -0
- package/dist/module/modules/call-manager/PrevLibDetection.js.map +1 -0
- package/dist/module/modules/call-manager/index.js +4 -0
- package/dist/module/modules/call-manager/index.js.map +1 -0
- package/dist/module/modules/call-manager/native-module.d.js +2 -0
- package/dist/module/modules/call-manager/native-module.d.js.map +1 -0
- package/dist/module/modules/call-manager/types.js +2 -0
- package/dist/module/modules/call-manager/types.js.map +1 -0
- package/dist/module/providers/StreamCall/AppStateListener.js +6 -6
- package/dist/module/providers/StreamCall/AppStateListener.js.map +1 -1
- package/dist/module/theme/theme.js.map +1 -1
- package/dist/module/utils/internal/rxSubjects.js +1 -1
- package/dist/module/utils/internal/rxSubjects.js.map +1 -1
- package/dist/module/version.js +1 -1
- package/dist/module/version.js.map +1 -1
- package/dist/typescript/components/Call/CallContent/CallContent.d.ts +3 -2
- package/dist/typescript/components/Call/CallContent/CallContent.d.ts.map +1 -1
- package/dist/typescript/components/Call/CallContent/RTCViewPipIOS.d.ts +5 -0
- package/dist/typescript/components/Call/CallContent/RTCViewPipIOS.d.ts.map +1 -1
- package/dist/typescript/components/Call/CallContent/RTCViewPipNative.d.ts +6 -0
- package/dist/typescript/components/Call/CallContent/RTCViewPipNative.d.ts.map +1 -1
- package/dist/typescript/components/Livestream/HostLivestream/HostLivestream.d.ts.map +1 -1
- package/dist/typescript/components/Livestream/LivestreamControls/ViewerLivestreamControls.d.ts.map +1 -1
- package/dist/typescript/components/Livestream/LivestreamLayout/LivestreamLayout.d.ts.map +1 -1
- package/dist/typescript/components/Livestream/ViewerLivestream/ViewerLivestream.d.ts.map +1 -1
- package/dist/typescript/hooks/usePermissionNotification.d.ts.map +1 -1
- package/dist/typescript/icons/Back.d.ts +1 -1
- package/dist/typescript/icons/Back.d.ts.map +1 -1
- package/dist/typescript/icons/BadNetwork.d.ts +1 -1
- package/dist/typescript/icons/BadNetwork.d.ts.map +1 -1
- package/dist/typescript/icons/CameraSwitch.d.ts +1 -1
- package/dist/typescript/icons/CameraSwitch.d.ts.map +1 -1
- package/dist/typescript/icons/LivestreamControls.d.ts +1 -1
- package/dist/typescript/icons/LivestreamControls.d.ts.map +1 -1
- package/dist/typescript/icons/Lock.d.ts +1 -1
- package/dist/typescript/icons/Lock.d.ts.map +1 -1
- package/dist/typescript/icons/Maximize.d.ts +1 -1
- package/dist/typescript/icons/Maximize.d.ts.map +1 -1
- package/dist/typescript/icons/Mic.d.ts +1 -1
- package/dist/typescript/icons/Mic.d.ts.map +1 -1
- package/dist/typescript/icons/MicOff.d.ts +1 -1
- package/dist/typescript/icons/MicOff.d.ts.map +1 -1
- package/dist/typescript/icons/Phone.d.ts +1 -1
- package/dist/typescript/icons/Phone.d.ts.map +1 -1
- package/dist/typescript/icons/PhoneDown.d.ts +1 -1
- package/dist/typescript/icons/PhoneDown.d.ts.map +1 -1
- package/dist/typescript/icons/PinVertical.d.ts +1 -1
- package/dist/typescript/icons/PinVertical.d.ts.map +1 -1
- package/dist/typescript/icons/Reaction.d.ts +1 -1
- package/dist/typescript/icons/Reaction.d.ts.map +1 -1
- package/dist/typescript/icons/ScreenShare.d.ts +1 -1
- package/dist/typescript/icons/ScreenShare.d.ts.map +1 -1
- package/dist/typescript/icons/ScreenShareIndicator.d.ts +1 -1
- package/dist/typescript/icons/ScreenShareIndicator.d.ts.map +1 -1
- package/dist/typescript/icons/Spotlight.d.ts +1 -1
- package/dist/typescript/icons/Spotlight.d.ts.map +1 -1
- package/dist/typescript/icons/StopScreenShare.d.ts +1 -1
- package/dist/typescript/icons/StopScreenShare.d.ts.map +1 -1
- package/dist/typescript/icons/Video.d.ts +1 -1
- package/dist/typescript/icons/Video.d.ts.map +1 -1
- package/dist/typescript/icons/VideoSlash.d.ts +1 -1
- package/dist/typescript/icons/VideoSlash.d.ts.map +1 -1
- package/dist/typescript/index.d.ts +1 -0
- package/dist/typescript/index.d.ts.map +1 -1
- package/dist/typescript/modules/call-manager/CallManager.d.ts +67 -0
- package/dist/typescript/modules/call-manager/CallManager.d.ts.map +1 -0
- package/dist/typescript/modules/call-manager/PrevLibDetection.d.ts +13 -0
- package/dist/typescript/modules/call-manager/PrevLibDetection.d.ts.map +1 -0
- package/dist/typescript/modules/call-manager/index.d.ts +4 -0
- package/dist/typescript/modules/call-manager/index.d.ts.map +1 -0
- package/dist/typescript/modules/call-manager/types.d.ts +15 -0
- package/dist/typescript/modules/call-manager/types.d.ts.map +1 -0
- package/dist/typescript/providers/StreamCall/AppStateListener.d.ts.map +1 -1
- package/dist/typescript/theme/theme.d.ts +1 -2
- package/dist/typescript/theme/theme.d.ts.map +1 -1
- package/dist/typescript/utils/internal/rxSubjects.d.ts +1 -1
- package/dist/typescript/utils/internal/rxSubjects.d.ts.map +1 -1
- package/dist/typescript/version.d.ts +1 -1
- package/dist/typescript/version.d.ts.map +1 -1
- package/ios/PictureInPicture/StreamPictureInPictureController.swift +5 -0
- package/ios/RTCViewPip.swift +15 -0
- package/ios/RTCViewPipManager.mm +1 -0
- package/ios/StreamInCallManager.m +26 -0
- package/ios/StreamInCallManager.swift +303 -0
- package/ios/StreamVideoReactNative-Bridging-Header.h +1 -0
- package/ios/StreamVideoReactNative.m +6 -5
- package/ios/StreamVideoReactNative.xcodeproj/project.xcworkspace/xcuserdata/santhoshvaiyapuri.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/StreamVideoReactNative.xcodeproj/xcuserdata/santhoshvaiyapuri.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/package.json +33 -35
- package/src/components/Call/CallContent/CallContent.tsx +14 -10
- package/src/components/Call/CallContent/RTCViewPipIOS.tsx +17 -2
- package/src/components/Call/CallContent/RTCViewPipNative.tsx +8 -0
- package/src/components/Livestream/HostLivestream/HostLivestream.tsx +8 -3
- package/src/components/Livestream/LivestreamControls/ViewerLivestreamControls.tsx +11 -5
- package/src/components/Livestream/LivestreamLayout/LivestreamLayout.tsx +38 -29
- package/src/components/Livestream/ViewerLivestream/ViewerLivestream.tsx +8 -3
- package/src/hooks/useIsInPiPMode.tsx +4 -4
- package/src/hooks/usePermissionNotification.tsx +7 -12
- package/src/icons/Back.tsx +2 -2
- package/src/icons/BadNetwork.tsx +1 -1
- package/src/icons/CameraSwitch.tsx +2 -2
- package/src/icons/LivestreamControls.tsx +1 -1
- package/src/icons/Lock.tsx +1 -1
- package/src/icons/Maximize.tsx +1 -1
- package/src/icons/Mic.tsx +2 -2
- package/src/icons/MicOff.tsx +2 -2
- package/src/icons/Phone.tsx +2 -2
- package/src/icons/PhoneDown.tsx +1 -1
- package/src/icons/PinVertical.tsx +2 -2
- package/src/icons/Reaction.tsx +2 -2
- package/src/icons/ScreenShare.tsx +1 -1
- package/src/icons/ScreenShareIndicator.tsx +1 -1
- package/src/icons/Spotlight.tsx +2 -2
- package/src/icons/StopScreenShare.tsx +1 -1
- package/src/icons/Video.tsx +2 -2
- package/src/icons/VideoSlash.tsx +2 -2
- package/src/index.ts +1 -0
- package/src/modules/call-manager/CallManager.ts +116 -0
- package/src/modules/call-manager/PrevLibDetection.ts +27 -0
- package/src/modules/call-manager/index.ts +5 -0
- package/src/modules/call-manager/native-module.d.ts +80 -0
- package/src/modules/call-manager/types.ts +25 -0
- package/src/providers/StreamCall/AppStateListener.tsx +6 -9
- package/src/theme/theme.ts +2 -2
- package/src/utils/internal/rxSubjects.ts +1 -1
- package/src/version.ts +1 -1
- package/CHANGELOG.md +0 -2783
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2016 The WebRTC Project Authors. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Use of this source code is governed by a BSD-style license
|
|
5
|
+
* that can be found in the LICENSE file in the root of the source
|
|
6
|
+
* tree. An additional intellectual property rights grant can be found
|
|
7
|
+
* in the file PATENTS. All contributing project authors may
|
|
8
|
+
* be found in the AUTHORS file in the root of the source tree.
|
|
9
|
+
*/
|
|
10
|
+
package com.streamvideo.reactnative.audio
|
|
11
|
+
|
|
12
|
+
import android.Manifest
|
|
13
|
+
import android.annotation.SuppressLint
|
|
14
|
+
import android.bluetooth.BluetoothAdapter
|
|
15
|
+
import android.bluetooth.BluetoothClass
|
|
16
|
+
import android.bluetooth.BluetoothDevice
|
|
17
|
+
import android.bluetooth.BluetoothHeadset
|
|
18
|
+
import android.bluetooth.BluetoothManager
|
|
19
|
+
import android.bluetooth.BluetoothProfile
|
|
20
|
+
import android.content.BroadcastReceiver
|
|
21
|
+
import android.content.Context
|
|
22
|
+
import android.content.Intent
|
|
23
|
+
import android.content.IntentFilter
|
|
24
|
+
import android.content.pm.PackageManager
|
|
25
|
+
import android.media.AudioDeviceCallback
|
|
26
|
+
import android.media.AudioDeviceInfo
|
|
27
|
+
import android.media.AudioManager
|
|
28
|
+
import android.os.Build
|
|
29
|
+
import android.os.Handler
|
|
30
|
+
import android.os.Looper
|
|
31
|
+
import android.util.Log
|
|
32
|
+
import androidx.annotation.RequiresApi
|
|
33
|
+
import androidx.core.content.ContextCompat
|
|
34
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
35
|
+
import com.streamvideo.reactnative.audio.AudioDeviceManager.Companion.runInAudioThread
|
|
36
|
+
import com.streamvideo.reactnative.audio.utils.AudioDeviceEndpointUtils
|
|
37
|
+
import com.streamvideo.reactnative.callmanager.StreamInCallManagerModule
|
|
38
|
+
import com.streamvideo.reactnative.model.AudioDeviceEndpoint
|
|
39
|
+
|
|
40
|
+
class BluetoothManager(
|
|
41
|
+
private val mReactContext: ReactApplicationContext,
|
|
42
|
+
private val audioDeviceManager: AudioDeviceManager,
|
|
43
|
+
) {
|
|
44
|
+
// Bluetooth connection state.
|
|
45
|
+
enum class State {
|
|
46
|
+
// Bluetooth is not available; no adapter or Bluetooth is off.
|
|
47
|
+
UNINITIALIZED,
|
|
48
|
+
|
|
49
|
+
// Bluetooth error happened when trying to start Bluetooth.
|
|
50
|
+
ERROR,
|
|
51
|
+
|
|
52
|
+
// Bluetooth proxy object for the Headset profile exists, but no connected headset devices,
|
|
53
|
+
// SCO is not started or disconnected.
|
|
54
|
+
HEADSET_UNAVAILABLE,
|
|
55
|
+
|
|
56
|
+
// Bluetooth proxy object for the Headset profile connected, connected Bluetooth headset
|
|
57
|
+
// present, but SCO is not started or disconnected.
|
|
58
|
+
HEADSET_AVAILABLE,
|
|
59
|
+
|
|
60
|
+
// Bluetooth audio SCO connection with remote device is closing.
|
|
61
|
+
SCO_DISCONNECTING,
|
|
62
|
+
|
|
63
|
+
// Bluetooth audio SCO connection with remote device is initiated.
|
|
64
|
+
SCO_CONNECTING,
|
|
65
|
+
|
|
66
|
+
// Bluetooth audio SCO connection with remote device is established.
|
|
67
|
+
SCO_CONNECTED
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private val mAudioManager =
|
|
71
|
+
mReactContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
|
|
72
|
+
|
|
73
|
+
var bluetoothState: State = State.UNINITIALIZED
|
|
74
|
+
|
|
75
|
+
private val btManagerPlatform: BluetoothManagerPlatform =
|
|
76
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) BluetoothManager31PlusImpl() else BluetoothManager23PlusImpl()
|
|
77
|
+
|
|
78
|
+
private fun updateAudioDeviceState() {
|
|
79
|
+
Log.d(TAG, "updateAudioDeviceState")
|
|
80
|
+
audioDeviceManager.updateAudioDeviceState()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Start the listeners */
|
|
84
|
+
fun start() = btManagerPlatform.start()
|
|
85
|
+
|
|
86
|
+
/** Stop the listeners */
|
|
87
|
+
fun stop() = btManagerPlatform.stop()
|
|
88
|
+
|
|
89
|
+
/** is audio flowing through BT communication device? */
|
|
90
|
+
fun isScoOn() = btManagerPlatform.isScoOn()
|
|
91
|
+
|
|
92
|
+
/** Start audio flowing through BT communication device. */
|
|
93
|
+
fun startScoAudio() = btManagerPlatform.startScoAudio()
|
|
94
|
+
|
|
95
|
+
/** Stop audio flowing through BT communication device. */
|
|
96
|
+
fun stopScoAudio() = btManagerPlatform.stopScoAudio()
|
|
97
|
+
|
|
98
|
+
/** Check if there is a BT headset connected and update the state accordingly */
|
|
99
|
+
fun updateDevice() = btManagerPlatform.updateDevice()
|
|
100
|
+
|
|
101
|
+
fun getDeviceName() = btManagerPlatform.getDeviceName()
|
|
102
|
+
|
|
103
|
+
abstract inner class BluetoothManagerPlatform {
|
|
104
|
+
|
|
105
|
+
abstract fun hasPermission(): Boolean
|
|
106
|
+
|
|
107
|
+
/** is audio flowing through BT communication device? */
|
|
108
|
+
abstract fun isScoOn(): Boolean
|
|
109
|
+
|
|
110
|
+
/** Start audio flowing through BT communication device. */
|
|
111
|
+
abstract fun startScoAudio(): Boolean
|
|
112
|
+
|
|
113
|
+
/** Check if there is a BT headset connected and update the state. */
|
|
114
|
+
abstract fun updateDevice()
|
|
115
|
+
|
|
116
|
+
/** Get the name of the connected BT device if present, otherwise null. */
|
|
117
|
+
abstract fun getDeviceName(): String?
|
|
118
|
+
|
|
119
|
+
/** Stop audio flowing through BT communication device. */
|
|
120
|
+
open fun stopScoAudio(): Boolean {
|
|
121
|
+
Log.d(
|
|
122
|
+
TAG, ("stopScoAudio: BT state=" + bluetoothState + ", " + "SCO is on: " + isScoOn())
|
|
123
|
+
)
|
|
124
|
+
return !(bluetoothState != State.SCO_CONNECTING && bluetoothState != State.SCO_CONNECTED)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Start the listeners */
|
|
128
|
+
open fun start(): Boolean {
|
|
129
|
+
Log.d(TAG, "start")
|
|
130
|
+
if (!hasPermission()) {
|
|
131
|
+
Log.w(
|
|
132
|
+
TAG, "App lacks BLUETOOTH permission"
|
|
133
|
+
)
|
|
134
|
+
return false
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Ensure that the device supports use of BT SCO audio for off call use cases.
|
|
138
|
+
if (!mAudioManager.isBluetoothScoAvailableOffCall) {
|
|
139
|
+
Log.e(TAG, "Bluetooth SCO audio is not available off call")
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return true
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/* Stop the listeners */
|
|
147
|
+
open fun stop(): Boolean {
|
|
148
|
+
Log.d(
|
|
149
|
+
TAG, "stop: BT state=$bluetoothState"
|
|
150
|
+
)
|
|
151
|
+
// Close down remaining BT resources.
|
|
152
|
+
return bluetoothState != State.UNINITIALIZED
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
@RequiresApi(31)
|
|
157
|
+
inner class BluetoothManager31PlusImpl : BluetoothManagerPlatform() {
|
|
158
|
+
|
|
159
|
+
private var bluetoothAudioDevice: AudioDeviceInfo? = null
|
|
160
|
+
|
|
161
|
+
/** Get the connected BT device if present, otherwise null.
|
|
162
|
+
Note: this doesn't mean that the device is streaming the audio now. It is only connected.
|
|
163
|
+
*/
|
|
164
|
+
private fun getAvailableBtDevice(): AudioDeviceInfo? {
|
|
165
|
+
val devices = mAudioManager.availableCommunicationDevices
|
|
166
|
+
for (device in devices) {
|
|
167
|
+
val isBtDevice =
|
|
168
|
+
AudioDeviceEndpoint.TYPE_BLUETOOTH == AudioDeviceEndpointUtils.remapAudioDeviceTypeToCallEndpointType(
|
|
169
|
+
device.type
|
|
170
|
+
)
|
|
171
|
+
if (isBtDevice) {
|
|
172
|
+
return device
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return null
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private var bluetoothAudioDeviceCallback: AudioDeviceCallback =
|
|
179
|
+
object : AudioDeviceCallback() {
|
|
180
|
+
|
|
181
|
+
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {
|
|
182
|
+
if (addedDevices != null) {
|
|
183
|
+
runInAudioThread {
|
|
184
|
+
updateDeviceList()
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
override fun onAudioDevicesRemoved(removedDevices: Array<out AudioDeviceInfo>?) {
|
|
190
|
+
if (removedDevices != null) {
|
|
191
|
+
runInAudioThread {
|
|
192
|
+
updateDeviceList()
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fun updateDeviceList() {
|
|
198
|
+
val currentBtDevice = bluetoothAudioDevice
|
|
199
|
+
val newBtDevice: AudioDeviceInfo? = getAvailableBtDevice()
|
|
200
|
+
if (currentBtDevice != null && newBtDevice == null) {
|
|
201
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
202
|
+
} else if (currentBtDevice == null && newBtDevice != null) {
|
|
203
|
+
bluetoothState = State.HEADSET_AVAILABLE
|
|
204
|
+
} else if (currentBtDevice != null && newBtDevice != null && currentBtDevice.id != newBtDevice.id) {
|
|
205
|
+
updateDevice()
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
override fun hasPermission(): Boolean {
|
|
211
|
+
return mReactContext.checkSelfPermission(Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
override fun start(): Boolean {
|
|
215
|
+
if (!super.start()) {
|
|
216
|
+
return false
|
|
217
|
+
}
|
|
218
|
+
mAudioManager.registerAudioDeviceCallback(bluetoothAudioDeviceCallback, null)
|
|
219
|
+
bluetoothAudioDevice = getAvailableBtDevice()
|
|
220
|
+
bluetoothState =
|
|
221
|
+
if (bluetoothAudioDevice != null) State.HEADSET_AVAILABLE else State.HEADSET_UNAVAILABLE
|
|
222
|
+
Log.d(
|
|
223
|
+
TAG, "start done: BT state=$bluetoothState"
|
|
224
|
+
)
|
|
225
|
+
return true
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
override fun stop(): Boolean {
|
|
229
|
+
if (!super.start()) {
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
// Stop BT SCO connection with remote device if needed.
|
|
233
|
+
stopScoAudio()
|
|
234
|
+
mAudioManager.unregisterAudioDeviceCallback(bluetoothAudioDeviceCallback)
|
|
235
|
+
bluetoothState = State.UNINITIALIZED
|
|
236
|
+
Log.d(
|
|
237
|
+
TAG, "stop done: BT state=$bluetoothState"
|
|
238
|
+
)
|
|
239
|
+
return true
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
override fun isScoOn(): Boolean {
|
|
243
|
+
val communicationDevice: AudioDeviceInfo? = mAudioManager.communicationDevice
|
|
244
|
+
if (communicationDevice !== null) {
|
|
245
|
+
val isOn =
|
|
246
|
+
AudioDeviceEndpoint.TYPE_BLUETOOTH == AudioDeviceEndpointUtils.remapAudioDeviceTypeToCallEndpointType(
|
|
247
|
+
communicationDevice.type
|
|
248
|
+
)
|
|
249
|
+
if (isOn) {
|
|
250
|
+
bluetoothAudioDevice = communicationDevice
|
|
251
|
+
return true
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return false
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
override fun startScoAudio(): Boolean {
|
|
258
|
+
Log.d(
|
|
259
|
+
TAG, ("startSco: BT state=" + bluetoothState + ", SCO is on: " + isScoOn())
|
|
260
|
+
)
|
|
261
|
+
val currentBtDevice = bluetoothAudioDevice
|
|
262
|
+
if (currentBtDevice != null) {
|
|
263
|
+
mAudioManager.setCommunicationDevice(currentBtDevice)
|
|
264
|
+
bluetoothState = State.SCO_CONNECTED
|
|
265
|
+
Log.d(
|
|
266
|
+
TAG,
|
|
267
|
+
"Set bluetooth audio device as communication device: id=${currentBtDevice.id} name=${currentBtDevice.productName}"
|
|
268
|
+
)
|
|
269
|
+
return true
|
|
270
|
+
}
|
|
271
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
272
|
+
Log.e(
|
|
273
|
+
TAG, "Cannot find any bluetooth SCO device to set as communication device"
|
|
274
|
+
)
|
|
275
|
+
return false
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
override fun updateDevice() {
|
|
279
|
+
if (bluetoothState == State.UNINITIALIZED) {
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
if (bluetoothState == State.SCO_CONNECTED) {
|
|
283
|
+
if (isScoOn()) {
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
bluetoothAudioDevice = getAvailableBtDevice()
|
|
288
|
+
val currentBtDevice = bluetoothAudioDevice
|
|
289
|
+
if (currentBtDevice != null) {
|
|
290
|
+
bluetoothState = State.HEADSET_AVAILABLE
|
|
291
|
+
Log.d(
|
|
292
|
+
TAG, ("Connected bluetooth headset: " + "name=" + currentBtDevice.productName)
|
|
293
|
+
)
|
|
294
|
+
} else {
|
|
295
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
296
|
+
}
|
|
297
|
+
Log.d(
|
|
298
|
+
TAG, "updateDevice done: BT state=$bluetoothState"
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
override fun getDeviceName(): String? {
|
|
303
|
+
return bluetoothAudioDevice?.productName?.toString()
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
override fun stopScoAudio(): Boolean {
|
|
307
|
+
if (!super.stopScoAudio()) {
|
|
308
|
+
return false
|
|
309
|
+
}
|
|
310
|
+
mAudioManager.clearCommunicationDevice()
|
|
311
|
+
bluetoothState = State.SCO_DISCONNECTING
|
|
312
|
+
Log.d(TAG, "stopScoAudio done: BT state=$bluetoothState + SCO is on: ${isScoOn()}")
|
|
313
|
+
return true
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
@Suppress("DEPRECATION")
|
|
318
|
+
inner class BluetoothManager23PlusImpl : BluetoothManagerPlatform() {
|
|
319
|
+
private var scoConnectionAttempts: Int = 0
|
|
320
|
+
private var mBluetoothAdapter: BluetoothAdapter? = null
|
|
321
|
+
private var bluetoothHeadset: BluetoothHeadset? = null
|
|
322
|
+
private var bluetoothDevice: BluetoothDevice? = null
|
|
323
|
+
private val handler = Handler(Looper.getMainLooper())
|
|
324
|
+
|
|
325
|
+
private val bluetoothTimeoutRunnable = Runnable { bluetoothTimeout() }
|
|
326
|
+
|
|
327
|
+
private val bluetoothServiceListener: BluetoothProfile.ServiceListener =
|
|
328
|
+
object : BluetoothProfile.ServiceListener {
|
|
329
|
+
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) {
|
|
330
|
+
if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
Log.d(
|
|
334
|
+
TAG, "BluetoothServiceListener.onServiceConnected: BT state=$bluetoothState"
|
|
335
|
+
)
|
|
336
|
+
// Android only supports one connected Bluetooth Headset at a time.
|
|
337
|
+
bluetoothHeadset = proxy as BluetoothHeadset
|
|
338
|
+
updateAudioDeviceState()
|
|
339
|
+
Log.d(
|
|
340
|
+
TAG, "onServiceConnected done: BT state=$bluetoothState"
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
override fun onServiceDisconnected(profile: Int) {
|
|
345
|
+
if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
Log.d(
|
|
349
|
+
TAG,
|
|
350
|
+
"BluetoothServiceListener.onServiceDisconnected: BT state=$bluetoothState"
|
|
351
|
+
)
|
|
352
|
+
stopScoAudio()
|
|
353
|
+
bluetoothHeadset = null
|
|
354
|
+
bluetoothDevice = null
|
|
355
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
356
|
+
updateAudioDeviceState()
|
|
357
|
+
Log.d(
|
|
358
|
+
TAG, "onServiceDisconnected done: BT state=$bluetoothState"
|
|
359
|
+
)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private val bluetoothHeadsetReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
|
365
|
+
override fun onReceive(context: Context?, intent: Intent) {
|
|
366
|
+
if (bluetoothState == State.UNINITIALIZED) {
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
val action = intent.action
|
|
370
|
+
// Change in connection state of the Headset profile. Note that the
|
|
371
|
+
// change does not tell us anything about whether we're streaming
|
|
372
|
+
// audio to BT over SCO. Typically received when user turns on a BT
|
|
373
|
+
// headset while audio is active using another audio device.
|
|
374
|
+
if (action == BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED) {
|
|
375
|
+
val state = intent.getIntExtra(
|
|
376
|
+
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED
|
|
377
|
+
)
|
|
378
|
+
Log.d(
|
|
379
|
+
TAG,
|
|
380
|
+
("BluetoothHeadsetBroadcastReceiver.onReceive: " + "a=ACTION_CONNECTION_STATE_CHANGED, " + "s=" + stateToString(
|
|
381
|
+
state
|
|
382
|
+
) + ", " + "sb=" + isInitialStickyBroadcast + ", " + "BT state: " + bluetoothState)
|
|
383
|
+
)
|
|
384
|
+
if (state == BluetoothHeadset.STATE_CONNECTED) {
|
|
385
|
+
scoConnectionAttempts = 0
|
|
386
|
+
updateAudioDeviceState()
|
|
387
|
+
} else if (state == BluetoothHeadset.STATE_CONNECTING) {
|
|
388
|
+
// No action needed.
|
|
389
|
+
} else if (state == BluetoothHeadset.STATE_DISCONNECTING) {
|
|
390
|
+
// No action needed.
|
|
391
|
+
} else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
|
|
392
|
+
// Bluetooth is probably powered off during the call.
|
|
393
|
+
stopScoAudio()
|
|
394
|
+
updateAudioDeviceState()
|
|
395
|
+
}
|
|
396
|
+
// Change in the audio (SCO) connection state of the Headset profile.
|
|
397
|
+
// Typically received after call to startScoAudio() has finalized.
|
|
398
|
+
} else if (action == BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED) {
|
|
399
|
+
val state = intent.getIntExtra(
|
|
400
|
+
BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED
|
|
401
|
+
)
|
|
402
|
+
Log.d(
|
|
403
|
+
TAG,
|
|
404
|
+
("BluetoothHeadsetBroadcastReceiver.onReceive: " + "a=ACTION_AUDIO_STATE_CHANGED, " + "s=" + stateToString(
|
|
405
|
+
state
|
|
406
|
+
) + ", " + "sb=" + isInitialStickyBroadcast + ", " + "BT state: " + bluetoothState)
|
|
407
|
+
)
|
|
408
|
+
if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
|
|
409
|
+
cancelTimer()
|
|
410
|
+
if (bluetoothState == State.SCO_CONNECTING) {
|
|
411
|
+
Log.d(
|
|
412
|
+
TAG, "+++ Bluetooth audio SCO is now connected"
|
|
413
|
+
)
|
|
414
|
+
bluetoothState = State.SCO_CONNECTED
|
|
415
|
+
scoConnectionAttempts = 0
|
|
416
|
+
updateAudioDeviceState()
|
|
417
|
+
} else {
|
|
418
|
+
Log.w(
|
|
419
|
+
TAG, "Unexpected state BluetoothHeadset.STATE_AUDIO_CONNECTED"
|
|
420
|
+
)
|
|
421
|
+
}
|
|
422
|
+
} else if (state == BluetoothHeadset.STATE_AUDIO_CONNECTING) {
|
|
423
|
+
Log.d(
|
|
424
|
+
TAG, "+++ Bluetooth audio SCO is now connecting..."
|
|
425
|
+
)
|
|
426
|
+
} else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
|
|
427
|
+
Log.d(
|
|
428
|
+
TAG, "+++ Bluetooth audio SCO is now disconnected"
|
|
429
|
+
)
|
|
430
|
+
if (isInitialStickyBroadcast) {
|
|
431
|
+
Log.d(
|
|
432
|
+
TAG, "Ignore STATE_AUDIO_DISCONNECTED initial sticky broadcast."
|
|
433
|
+
)
|
|
434
|
+
return
|
|
435
|
+
}
|
|
436
|
+
updateAudioDeviceState()
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
Log.d(
|
|
440
|
+
TAG, "onReceive done: BT state=$bluetoothState"
|
|
441
|
+
)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
override fun hasPermission(): Boolean {
|
|
447
|
+
return mReactContext.checkSelfPermission(Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
override fun isScoOn(): Boolean = mAudioManager.isBluetoothScoOn()
|
|
451
|
+
|
|
452
|
+
override fun startScoAudio(): Boolean {
|
|
453
|
+
Log.d(
|
|
454
|
+
TAG,
|
|
455
|
+
("startSco: BT state=" + bluetoothState + ", " + "attempts: " + scoConnectionAttempts + ", " + "SCO is on: " + isScoOn())
|
|
456
|
+
)
|
|
457
|
+
if (scoConnectionAttempts >= MAX_SCO_CONNECTION_ATTEMPTS) {
|
|
458
|
+
Log.e(TAG, "BT SCO connection fails - no more attempts")
|
|
459
|
+
return false
|
|
460
|
+
}
|
|
461
|
+
if (bluetoothState == State.HEADSET_UNAVAILABLE) {
|
|
462
|
+
Log.e(TAG, "BT SCO connection fails - no headset available")
|
|
463
|
+
return false
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// The SCO connection establishment can take several seconds, hence we cannot rely on the
|
|
467
|
+
// connection to be available when the method returns but instead register to receive the
|
|
468
|
+
// intent ACTION_SCO_AUDIO_STATE_UPDATED and wait for the state to be SCO_AUDIO_STATE_CONNECTED.
|
|
469
|
+
// Start BT SCO channel and wait for ACTION_AUDIO_STATE_CHANGED.
|
|
470
|
+
Log.d(
|
|
471
|
+
TAG, "Starting Bluetooth SCO and waits for ACTION_AUDIO_STATE_CHANGED..."
|
|
472
|
+
)
|
|
473
|
+
bluetoothState = State.SCO_CONNECTING
|
|
474
|
+
startTimer()
|
|
475
|
+
mAudioManager.startBluetoothSco()
|
|
476
|
+
mAudioManager.setBluetoothScoOn(true)
|
|
477
|
+
scoConnectionAttempts++
|
|
478
|
+
Log.d(
|
|
479
|
+
TAG,
|
|
480
|
+
("startScoAudio done: BT state=" + bluetoothState + ", " + "SCO is on: " + isScoOn())
|
|
481
|
+
)
|
|
482
|
+
return true
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
@SuppressLint("MissingPermission")
|
|
486
|
+
override fun updateDevice() {
|
|
487
|
+
val currBtHeadset = bluetoothHeadset
|
|
488
|
+
if (bluetoothState == State.UNINITIALIZED || currBtHeadset == null) {
|
|
489
|
+
return
|
|
490
|
+
}
|
|
491
|
+
// Get connected devices for the headset profile. Returns the set of
|
|
492
|
+
// devices which are in state STATE_CONNECTED. The BluetoothDevice class
|
|
493
|
+
// is just a thin wrapper for a Bluetooth hardware address.
|
|
494
|
+
val devices = getFinalConnectedDevices()
|
|
495
|
+
for (device in devices) {
|
|
496
|
+
Log.d(
|
|
497
|
+
TAG,
|
|
498
|
+
("Connected bluetooth headset: " + "name=" + device.name + ", " + "state=" + stateToString(
|
|
499
|
+
currBtHeadset.getConnectionState(device)
|
|
500
|
+
))
|
|
501
|
+
)
|
|
502
|
+
}
|
|
503
|
+
if (devices.isEmpty()) {
|
|
504
|
+
bluetoothDevice = null
|
|
505
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
506
|
+
Log.d(TAG, "No connected bluetooth headset")
|
|
507
|
+
} else {
|
|
508
|
+
// Always use first device in list. Android only supports one device.
|
|
509
|
+
val firstBtDevice = devices[0]
|
|
510
|
+
bluetoothDevice = firstBtDevice
|
|
511
|
+
bluetoothState = State.HEADSET_AVAILABLE
|
|
512
|
+
Log.d(
|
|
513
|
+
TAG,
|
|
514
|
+
("Connected bluetooth headset: " + "name=" + firstBtDevice.name + ", " + "state=" + stateToString(
|
|
515
|
+
currBtHeadset.getConnectionState(
|
|
516
|
+
bluetoothDevice
|
|
517
|
+
)
|
|
518
|
+
) + ", SCO audio=" + currBtHeadset.isAudioConnected(
|
|
519
|
+
bluetoothDevice
|
|
520
|
+
))
|
|
521
|
+
)
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
Log.d(
|
|
525
|
+
TAG, "updateDevice done: BT state=$bluetoothState"
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
@SuppressLint("MissingPermission")
|
|
530
|
+
override fun getDeviceName(): String? {
|
|
531
|
+
return bluetoothDevice?.name
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
@SuppressLint("MissingPermission")
|
|
535
|
+
override fun start(): Boolean {
|
|
536
|
+
if (!super.start()) {
|
|
537
|
+
return false
|
|
538
|
+
}
|
|
539
|
+
val bluetoothManager: BluetoothManager =
|
|
540
|
+
mReactContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
|
541
|
+
mBluetoothAdapter = bluetoothManager.adapter
|
|
542
|
+
bluetoothHeadset = null
|
|
543
|
+
bluetoothDevice = null
|
|
544
|
+
scoConnectionAttempts = 0
|
|
545
|
+
|
|
546
|
+
// Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
|
|
547
|
+
// Hands-Free) proxy object and install a listener.
|
|
548
|
+
if (!getBluetoothProfileProxy(
|
|
549
|
+
mReactContext, bluetoothServiceListener
|
|
550
|
+
)
|
|
551
|
+
) {
|
|
552
|
+
Log.e(
|
|
553
|
+
TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed"
|
|
554
|
+
)
|
|
555
|
+
return false
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// Register receivers for BluetoothHeadset change notifications.
|
|
559
|
+
val bluetoothHeadsetFilter = IntentFilter()
|
|
560
|
+
// Register receiver for change in connection state of the Headset profile.
|
|
561
|
+
bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)
|
|
562
|
+
// Register receiver for change in audio connection state of the Headset profile.
|
|
563
|
+
bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)
|
|
564
|
+
ContextCompat.registerReceiver(
|
|
565
|
+
mReactContext,
|
|
566
|
+
bluetoothHeadsetReceiver,
|
|
567
|
+
bluetoothHeadsetFilter,
|
|
568
|
+
ContextCompat.RECEIVER_NOT_EXPORTED
|
|
569
|
+
)
|
|
570
|
+
bluetoothState = State.HEADSET_UNAVAILABLE
|
|
571
|
+
Log.d(
|
|
572
|
+
TAG, "HEADSET profile state: " + stateToString(
|
|
573
|
+
mBluetoothAdapter?.getProfileConnectionState(
|
|
574
|
+
BluetoothProfile.HEADSET
|
|
575
|
+
) ?: -1
|
|
576
|
+
)
|
|
577
|
+
)
|
|
578
|
+
|
|
579
|
+
Log.d(TAG, "Bluetooth proxy for headset profile has started")
|
|
580
|
+
Log.d(
|
|
581
|
+
TAG, "start done: BT state=$bluetoothState"
|
|
582
|
+
)
|
|
583
|
+
return true
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
override fun stop(): Boolean {
|
|
587
|
+
if (!super.stop()) {
|
|
588
|
+
return false
|
|
589
|
+
}
|
|
590
|
+
if (mBluetoothAdapter == null) {
|
|
591
|
+
return false
|
|
592
|
+
}
|
|
593
|
+
// Stop BT SCO connection with remote device if needed.
|
|
594
|
+
stopScoAudio()
|
|
595
|
+
try {
|
|
596
|
+
mReactContext.unregisterReceiver(bluetoothHeadsetReceiver)
|
|
597
|
+
bluetoothHeadset?.also {
|
|
598
|
+
mBluetoothAdapter!!.closeProfileProxy(BluetoothProfile.HEADSET, it)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
} catch (exception: Exception) {
|
|
602
|
+
// The receiver was not registered.
|
|
603
|
+
// There is nothing to do in that case.
|
|
604
|
+
// Everything is fine.
|
|
605
|
+
}
|
|
606
|
+
cancelTimer()
|
|
607
|
+
return true
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
override fun stopScoAudio(): Boolean {
|
|
611
|
+
if (!super.stopScoAudio()) {
|
|
612
|
+
return false
|
|
613
|
+
}
|
|
614
|
+
cancelTimer()
|
|
615
|
+
mAudioManager.stopBluetoothSco()
|
|
616
|
+
mAudioManager.setBluetoothScoOn(false)
|
|
617
|
+
|
|
618
|
+
bluetoothState = State.SCO_DISCONNECTING
|
|
619
|
+
Log.d(
|
|
620
|
+
TAG,
|
|
621
|
+
("stopScoAudio done: BT state=" + bluetoothState + ", " + "SCO is on: " + isScoOn())
|
|
622
|
+
)
|
|
623
|
+
return true
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Called when start of the BT SCO channel takes too long time. Usually
|
|
628
|
+
* happens when the BT device has been turned on during an ongoing call.
|
|
629
|
+
*/
|
|
630
|
+
@SuppressLint("MissingPermission")
|
|
631
|
+
private fun bluetoothTimeout() {
|
|
632
|
+
val btHeadset = bluetoothHeadset
|
|
633
|
+
if (bluetoothState == State.UNINITIALIZED || btHeadset == null) {
|
|
634
|
+
return
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
Log.d(
|
|
638
|
+
TAG,
|
|
639
|
+
("bluetoothTimeout: BT state=" + bluetoothState + ", " + "attempts: " + scoConnectionAttempts + ", " + "SCO is on: " + isScoOn())
|
|
640
|
+
)
|
|
641
|
+
if (bluetoothState != State.SCO_CONNECTING) {
|
|
642
|
+
return
|
|
643
|
+
}
|
|
644
|
+
// Bluetooth SCO should be connecting; check the latest result.
|
|
645
|
+
var scoConnected = false
|
|
646
|
+
val devices: List<BluetoothDevice> = getFinalConnectedDevices()
|
|
647
|
+
if (devices.isNotEmpty()) {
|
|
648
|
+
bluetoothDevice = devices[0]
|
|
649
|
+
val currBtDevice = bluetoothDevice!!
|
|
650
|
+
if (btHeadset.isAudioConnected(currBtDevice)) {
|
|
651
|
+
Log.d(
|
|
652
|
+
TAG, "SCO connected with " + currBtDevice.name
|
|
653
|
+
)
|
|
654
|
+
scoConnected = true
|
|
655
|
+
} else {
|
|
656
|
+
Log.d(
|
|
657
|
+
TAG, "SCO is not connected with " + currBtDevice.name
|
|
658
|
+
)
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (scoConnected) {
|
|
662
|
+
// We thought BT had timed out, but it's actually on; updating state.
|
|
663
|
+
bluetoothState = State.SCO_CONNECTED
|
|
664
|
+
scoConnectionAttempts = 0
|
|
665
|
+
} else {
|
|
666
|
+
// Give up and "cancel" our request by calling stopBluetoothSco().
|
|
667
|
+
Log.w(TAG, "BT failed to connect after timeout")
|
|
668
|
+
stopScoAudio()
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
updateAudioDeviceState()
|
|
672
|
+
Log.d(
|
|
673
|
+
TAG, "bluetoothTimeout done: BT state=$bluetoothState"
|
|
674
|
+
)
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
@SuppressLint("MissingPermission")
|
|
678
|
+
private fun getFinalConnectedDevices(): List<BluetoothDevice> {
|
|
679
|
+
val connectedDevices = bluetoothHeadset?.connectedDevices ?: emptyList()
|
|
680
|
+
val finalDevices: MutableList<BluetoothDevice> = ArrayList()
|
|
681
|
+
|
|
682
|
+
Log.d(TAG, "getFinalConnectedDevices: connectedDevices=$connectedDevices")
|
|
683
|
+
|
|
684
|
+
for (device in connectedDevices) {
|
|
685
|
+
val majorClass = device.bluetoothClass.majorDeviceClass
|
|
686
|
+
|
|
687
|
+
if (majorClass == BluetoothClass.Device.Major.AUDIO_VIDEO) {
|
|
688
|
+
Log.d(TAG, "getFinalConnectedDevices: device=${device.name}")
|
|
689
|
+
finalDevices.add(device)
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return finalDevices
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
private fun getBluetoothProfileProxy(
|
|
696
|
+
context: Context?, listener: BluetoothProfile.ServiceListener?
|
|
697
|
+
): Boolean {
|
|
698
|
+
try {
|
|
699
|
+
return mBluetoothAdapter?.getProfileProxy(
|
|
700
|
+
context,
|
|
701
|
+
listener,
|
|
702
|
+
BluetoothProfile.HEADSET
|
|
703
|
+
) ?: false
|
|
704
|
+
} catch (e: Exception) {
|
|
705
|
+
Log.e(TAG, "gBPP: hit exception while getting bluetooth profile", e)
|
|
706
|
+
return false
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** Starts timer which times out after BLUETOOTH_SCO_TIMEOUT_MS milliseconds. */
|
|
711
|
+
private fun startTimer() {
|
|
712
|
+
Log.d(TAG, "startTimer")
|
|
713
|
+
handler.postDelayed(
|
|
714
|
+
bluetoothTimeoutRunnable, BLUETOOTH_SCO_TIMEOUT_MS.toLong()
|
|
715
|
+
)
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** Cancels any outstanding timer tasks. */
|
|
719
|
+
private fun cancelTimer() {
|
|
720
|
+
Log.d(TAG, "cancelTimer")
|
|
721
|
+
handler.removeCallbacks(bluetoothTimeoutRunnable)
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
companion object {
|
|
726
|
+
private val TAG: String =
|
|
727
|
+
StreamInCallManagerModule.TAG + ":" + BluetoothManager::class.java.simpleName.toString()
|
|
728
|
+
|
|
729
|
+
// Timeout interval for starting or stopping audio to a Bluetooth SCO device.
|
|
730
|
+
private const val BLUETOOTH_SCO_TIMEOUT_MS: Int = 6000
|
|
731
|
+
|
|
732
|
+
// Maximum number of SCO connection attempts.
|
|
733
|
+
private const val MAX_SCO_CONNECTION_ATTEMPTS: Int = 10
|
|
734
|
+
|
|
735
|
+
private fun stateToString(state: Int): String {
|
|
736
|
+
return when (state) {
|
|
737
|
+
BluetoothAdapter.STATE_DISCONNECTED -> "DISCONNECTED"
|
|
738
|
+
BluetoothAdapter.STATE_CONNECTED -> "CONNECTED"
|
|
739
|
+
BluetoothAdapter.STATE_CONNECTING -> "CONNECTING"
|
|
740
|
+
BluetoothAdapter.STATE_DISCONNECTING -> "DISCONNECTING"
|
|
741
|
+
BluetoothAdapter.STATE_OFF -> "OFF"
|
|
742
|
+
BluetoothAdapter.STATE_ON -> "ON"
|
|
743
|
+
BluetoothAdapter.STATE_TURNING_OFF -> // Indicates the local Bluetooth adapter is turning off. Local clients should immediately
|
|
744
|
+
// attempt graceful disconnection of any remote links.
|
|
745
|
+
"TURNING_OFF"
|
|
746
|
+
|
|
747
|
+
BluetoothAdapter.STATE_TURNING_ON -> // Indicates the local Bluetooth adapter is turning on. However local clients should wait
|
|
748
|
+
// for STATE_ON before attempting to use the adapter.
|
|
749
|
+
"TURNING_ON"
|
|
750
|
+
|
|
751
|
+
else -> "INVALID"
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|