Android:如何检测蓝牙连接状态

发布于 2024-10-29 03:45:14 字数 226 浏览 0 评论 0原文

我想检测已配对蓝牙耳机的连接状态 到电话。 在 Android 3.0(API 级别 11)中,“BluetoothHeadset”类具有 “isAudioConnected()”方法。

  • 我不知道如何创建(初始化)“BluetoothHeadset”对象。 看来我需要使用 “getProfileProxy()”但我需要一个示例代码来了解我需要什么 创建并传递参数。

谢谢, 何斯

I want to detect the connection status of a paired Bluetooth headset
to the phone.
In Android 3.0 (API level 11) "BluetoothHeadset" class has
"isAudioConnected()" method.

  • I don't know how to create (initialize) a "BluetoothHeadset" object.
    It seems that I need to use
    "getProfileProxy ()" but I need a sample code to find out how I need
    to create and pass the parameters.

Thanks,
Hos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-11-05 03:45:14

您需要实现BluetoothProfile.ServiceListener:

BluetoothProfile.ServiceListener b = new BlueToothListener();
            boolean profileProxy = BluetoothAdapter.getDefaultAdapter()
                    .getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET);


public class BlueToothListener implements ServiceListener {
        public static BluetoothHeadset headset;
        public static BluetoothDevice bluetoothDevice;
    @Override
    public void onServiceDisconnected(int profile) {// dont care
        headset = null;
    }

    @Override
    public void onServiceConnected(int profile,
            BluetoothProfile proxy) {// dont care
        try {
            Debugger.test("BluetoothProfile onServiceConnected "+proxy);
            if (proxy instanceof BluetoothHeadset)
                headset = ((BluetoothHeadset) proxy);
            else// getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET); 
                return;// ^^ => NEVER

            List<BluetoothDevice> connectedDevices = proxy
                    .getConnectedDevices();
            for (BluetoothDevice device : connectedDevices) {
                Debugger.log("BluetoothDevice found :" + device);
                bluetoothDevice = device;
                int connectionState = headset.getConnectionState(bluetoothDevice);
                Debugger.log("BluetoothHeadset connectionState "+connectionState);//2 == OK
                boolean startVoiceRecognition = headset
                        .startVoiceRecognition(device);
                if (startVoiceRecognition) {
                    Debugger
                            .log("BluetoothHeadset init Listener OK");
                    return;
                }
                else 
                    Notify.popup("Bluetooth headset can't start speech recognition");

            }
        } catch (Exception e) {
            // }
        }
    }
}

`

You need to implement BluetoothProfile.ServiceListener :

BluetoothProfile.ServiceListener b = new BlueToothListener();
            boolean profileProxy = BluetoothAdapter.getDefaultAdapter()
                    .getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET);


public class BlueToothListener implements ServiceListener {
        public static BluetoothHeadset headset;
        public static BluetoothDevice bluetoothDevice;
    @Override
    public void onServiceDisconnected(int profile) {// dont care
        headset = null;
    }

    @Override
    public void onServiceConnected(int profile,
            BluetoothProfile proxy) {// dont care
        try {
            Debugger.test("BluetoothProfile onServiceConnected "+proxy);
            if (proxy instanceof BluetoothHeadset)
                headset = ((BluetoothHeadset) proxy);
            else// getProfileProxy(Handler.bot, b, BluetoothProfile.HEADSET); 
                return;// ^^ => NEVER

            List<BluetoothDevice> connectedDevices = proxy
                    .getConnectedDevices();
            for (BluetoothDevice device : connectedDevices) {
                Debugger.log("BluetoothDevice found :" + device);
                bluetoothDevice = device;
                int connectionState = headset.getConnectionState(bluetoothDevice);
                Debugger.log("BluetoothHeadset connectionState "+connectionState);//2 == OK
                boolean startVoiceRecognition = headset
                        .startVoiceRecognition(device);
                if (startVoiceRecognition) {
                    Debugger
                            .log("BluetoothHeadset init Listener OK");
                    return;
                }
                else 
                    Notify.popup("Bluetooth headset can't start speech recognition");

            }
        } catch (Exception e) {
            // }
        }
    }
}

`

北风几吹夏 2024-11-05 03:45:14

BT状态的监控确实可以通过轮询来完成。这是我的做法(完整示例此处)。请注意,这只是一个示例,您应该更好地管理轮询:

ma​​nifest

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

gradle

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.work:work-runtime-ktx:2.7.1"

MainActivityViewModel.kt

@UiThread
class MainActivityViewModel(application: Application) : BaseViewModel(application) {
    private val bluetoothAdapter: BluetoothAdapter =
        context.getSystemService<BluetoothManager>()!!.adapter
    private var bluetoothHeadsetProfile: BluetoothProfile? = null
    val connectedDevicesLiveData =
        DistinctLiveDataWrapper(MutableLiveData<ConnectedDevicesState>(ConnectedDevicesState.Idle))
    val bluetoothTurnedOnLiveData = DistinctLiveDataWrapper(MutableLiveData<Boolean?>(null))
    val isConnectedToBtHeadsetLiveData = DistinctLiveDataWrapper(MutableLiveData<Boolean?>(null))
    private val pollingBtStateRunnable: Runnable

    init {
        updateBtStates()
        pollingBtStateRunnable = object : Runnable {
            override fun run() {
                updateBtStates()
                handler.postDelayed(this, POLLING_TIME_IN_MS)
            }
        }
        // Establish connection to the proxy.
        val serviceListener = object : BluetoothProfile.ServiceListener {
            override fun onServiceConnected(profile: Int, bluetoothProfile: BluetoothProfile) {
                [email protected] = bluetoothProfile
                handler.removeCallbacks(pollingBtStateRunnable)
                pollingBtStateRunnable.run()
            }

            override fun onServiceDisconnected(profile: Int) {
                handler.removeCallbacks(pollingBtStateRunnable)
                updateBtStates()
            }
        }
        bluetoothAdapter.getProfileProxy(context, serviceListener, BluetoothProfile.HEADSET)
        onClearedListeners.add {
            this.bluetoothHeadsetProfile?.let { bluetoothProfile ->
                bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothProfile)
            }
            handler.removeCallbacks(pollingBtStateRunnable)
        }
    }

    fun initWithLifecycle(lifecycle: Lifecycle) {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                pollingBtStateRunnable.run()
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                handler.removeCallbacks(pollingBtStateRunnable)
            }
        })
    }

    @UiThread
    private fun updateBtStates() {
        //        Log.d("AppLog", "updateBtStates")
        val isBlueToothTurnedOn = bluetoothAdapter.state == BluetoothAdapter.STATE_ON
        bluetoothTurnedOnLiveData.value = isBlueToothTurnedOn
        if (!isBlueToothTurnedOn) {
            connectedDevicesLiveData.value = ConnectedDevicesState.BluetoothIsTurnedOff
            isConnectedToBtHeadsetLiveData.value = false
            return
        }
        val isConnectedToBtHeadset = try {
            bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothAdapter.STATE_CONNECTED
        } catch (e: SecurityException) {
            null
        }
        isConnectedToBtHeadsetLiveData.value = isConnectedToBtHeadset
        val bluetoothProfile = bluetoothHeadsetProfile
        if (bluetoothProfile != null) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
                val connectedDevicesSet = bluetoothProfile.connectedDevices.toHashSet()
                val previousConnectedDevices =
                    (connectedDevicesLiveData.value as? ConnectedDevicesState.GotResult)?.connectedDevices
                if (previousConnectedDevices == null || previousConnectedDevices != connectedDevicesSet)
                    connectedDevicesLiveData.value =
                        ConnectedDevicesState.GotResult(connectedDevicesSet)
            } else {
                connectedDevicesLiveData.value =
                    ConnectedDevicesState.NeedBlueToothConnectPermission
            }
        } else {
            connectedDevicesLiveData.value = ConnectedDevicesState.Idle
        }
    }

    companion object {
        private const val POLLING_TIME_IN_MS = 500L
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var viewModel: MainActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java]
        viewModel.initWithLifecycle(lifecycle)
        viewModel.bluetoothTurnedOnLiveData.observe(this) {
            Log.d("AppLog", "MainActivity bluetoothTurnedOnLiveData BT turned on? $it")
        }
        viewModel.isConnectedToBtHeadsetLiveData.observe(this) {
            Log.d("AppLog", "MainActivity isConnectedToBtHeadsetLiveData BT headset connected? $it")
        }
        viewModel.connectedDevicesLiveData.observe(this) {
            Log.d("AppLog", "MainActivity connectedDevicesLiveData devices: $it")
        }
        findViewById<View>(R.id.grantBtPermission).setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_CONNECT), 1)
            }
        }
    }
}

ConnectedDevicesState.kt

sealed class ConnectedDevicesState {
    object Idle : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "Idle"
            return super.toString()
        }
    }

    class GotResult(@Suppress("MemberVisibilityCanBePrivate") val connectedDevices: Set<BluetoothDevice>) : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG) {
                return "GotResult: connectedDevices:${
                    connectedDevices.map {
                        try {
                            it.name
                        } catch (e: SecurityException) {
                            it.address
                        }
                    }
                }"
            }
            return super.toString()
        }
    }

    object BluetoothIsTurnedOff : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "BluetoothIsTurnedOff"
            return super.toString()
        }
    }

    object NeedBlueToothConnectPermission : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "NeedBlueToothConnectPermission"
            return super.toString()
        }
    }
}

DistinctLiveDataWrapper.kt

class DistinctLiveDataWrapper<T>(@Suppress("MemberVisibilityCanBePrivate") val mutableLiveData: MutableLiveData<T>) {
    @Suppress("MemberVisibilityCanBePrivate")
    val distinctLiveData = Transformations.distinctUntilChanged(mutableLiveData)
    var value: T?
        @UiThread
        set(value) {
            mutableLiveData.value = value
        }
        get() {
            return mutableLiveData.value
        }

    @AnyThread
    fun postValue(value: T) {
        mutableLiveData.postValue(value)
    }

    fun observe(lifecycleOwner: LifecycleOwner, observer: Observer<in T>) {
        distinctLiveData.observe(lifecycleOwner, observer)
    }
}

BaseViewModel.kt

/**usage: class MyViewModel(application: Application) : BaseViewModel(application)
 * getting instance:    private lateinit var viewModel: MyViewModel
 * viewModel=ViewModelProvider(this).get(MyViewModel::class.java)*/
abstract class BaseViewModel(application: Application) : AndroidViewModel(application) {
    @Suppress("MemberVisibilityCanBePrivate")
    var isCleared = false
    @Suppress("MemberVisibilityCanBePrivate")
    val onClearedListeners = ArrayList<Runnable>()

    @Suppress("unused")
    @SuppressLint("StaticFieldLeak")
    val context: Context = application.applicationContext
    @Suppress("unused")
    val handler = Handler(Looper.getMainLooper())

    override fun onCleared() {
        super.onCleared()
        isCleared = true
        onClearedListeners.forEach { it.run() }
    }
}

Monitoring of the BT status can be done indeed by polling. Here's how I did it (full sample here) . Note that it's just a sample and you should manage the polling better:

manifest

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

gradle

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.work:work-runtime-ktx:2.7.1"

MainActivityViewModel.kt

@UiThread
class MainActivityViewModel(application: Application) : BaseViewModel(application) {
    private val bluetoothAdapter: BluetoothAdapter =
        context.getSystemService<BluetoothManager>()!!.adapter
    private var bluetoothHeadsetProfile: BluetoothProfile? = null
    val connectedDevicesLiveData =
        DistinctLiveDataWrapper(MutableLiveData<ConnectedDevicesState>(ConnectedDevicesState.Idle))
    val bluetoothTurnedOnLiveData = DistinctLiveDataWrapper(MutableLiveData<Boolean?>(null))
    val isConnectedToBtHeadsetLiveData = DistinctLiveDataWrapper(MutableLiveData<Boolean?>(null))
    private val pollingBtStateRunnable: Runnable

    init {
        updateBtStates()
        pollingBtStateRunnable = object : Runnable {
            override fun run() {
                updateBtStates()
                handler.postDelayed(this, POLLING_TIME_IN_MS)
            }
        }
        // Establish connection to the proxy.
        val serviceListener = object : BluetoothProfile.ServiceListener {
            override fun onServiceConnected(profile: Int, bluetoothProfile: BluetoothProfile) {
                [email protected] = bluetoothProfile
                handler.removeCallbacks(pollingBtStateRunnable)
                pollingBtStateRunnable.run()
            }

            override fun onServiceDisconnected(profile: Int) {
                handler.removeCallbacks(pollingBtStateRunnable)
                updateBtStates()
            }
        }
        bluetoothAdapter.getProfileProxy(context, serviceListener, BluetoothProfile.HEADSET)
        onClearedListeners.add {
            this.bluetoothHeadsetProfile?.let { bluetoothProfile ->
                bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothProfile)
            }
            handler.removeCallbacks(pollingBtStateRunnable)
        }
    }

    fun initWithLifecycle(lifecycle: Lifecycle) {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                pollingBtStateRunnable.run()
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                handler.removeCallbacks(pollingBtStateRunnable)
            }
        })
    }

    @UiThread
    private fun updateBtStates() {
        //        Log.d("AppLog", "updateBtStates")
        val isBlueToothTurnedOn = bluetoothAdapter.state == BluetoothAdapter.STATE_ON
        bluetoothTurnedOnLiveData.value = isBlueToothTurnedOn
        if (!isBlueToothTurnedOn) {
            connectedDevicesLiveData.value = ConnectedDevicesState.BluetoothIsTurnedOff
            isConnectedToBtHeadsetLiveData.value = false
            return
        }
        val isConnectedToBtHeadset = try {
            bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothAdapter.STATE_CONNECTED
        } catch (e: SecurityException) {
            null
        }
        isConnectedToBtHeadsetLiveData.value = isConnectedToBtHeadset
        val bluetoothProfile = bluetoothHeadsetProfile
        if (bluetoothProfile != null) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
                val connectedDevicesSet = bluetoothProfile.connectedDevices.toHashSet()
                val previousConnectedDevices =
                    (connectedDevicesLiveData.value as? ConnectedDevicesState.GotResult)?.connectedDevices
                if (previousConnectedDevices == null || previousConnectedDevices != connectedDevicesSet)
                    connectedDevicesLiveData.value =
                        ConnectedDevicesState.GotResult(connectedDevicesSet)
            } else {
                connectedDevicesLiveData.value =
                    ConnectedDevicesState.NeedBlueToothConnectPermission
            }
        } else {
            connectedDevicesLiveData.value = ConnectedDevicesState.Idle
        }
    }

    companion object {
        private const val POLLING_TIME_IN_MS = 500L
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var viewModel: MainActivityViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java]
        viewModel.initWithLifecycle(lifecycle)
        viewModel.bluetoothTurnedOnLiveData.observe(this) {
            Log.d("AppLog", "MainActivity bluetoothTurnedOnLiveData BT turned on? $it")
        }
        viewModel.isConnectedToBtHeadsetLiveData.observe(this) {
            Log.d("AppLog", "MainActivity isConnectedToBtHeadsetLiveData BT headset connected? $it")
        }
        viewModel.connectedDevicesLiveData.observe(this) {
            Log.d("AppLog", "MainActivity connectedDevicesLiveData devices: $it")
        }
        findViewById<View>(R.id.grantBtPermission).setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                requestPermissions(arrayOf(Manifest.permission.BLUETOOTH_CONNECT), 1)
            }
        }
    }
}

ConnectedDevicesState.kt

sealed class ConnectedDevicesState {
    object Idle : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "Idle"
            return super.toString()
        }
    }

    class GotResult(@Suppress("MemberVisibilityCanBePrivate") val connectedDevices: Set<BluetoothDevice>) : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG) {
                return "GotResult: connectedDevices:${
                    connectedDevices.map {
                        try {
                            it.name
                        } catch (e: SecurityException) {
                            it.address
                        }
                    }
                }"
            }
            return super.toString()
        }
    }

    object BluetoothIsTurnedOff : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "BluetoothIsTurnedOff"
            return super.toString()
        }
    }

    object NeedBlueToothConnectPermission : ConnectedDevicesState() {
        override fun toString(): String {
            if (BuildConfig.DEBUG)
                return "NeedBlueToothConnectPermission"
            return super.toString()
        }
    }
}

DistinctLiveDataWrapper.kt

class DistinctLiveDataWrapper<T>(@Suppress("MemberVisibilityCanBePrivate") val mutableLiveData: MutableLiveData<T>) {
    @Suppress("MemberVisibilityCanBePrivate")
    val distinctLiveData = Transformations.distinctUntilChanged(mutableLiveData)
    var value: T?
        @UiThread
        set(value) {
            mutableLiveData.value = value
        }
        get() {
            return mutableLiveData.value
        }

    @AnyThread
    fun postValue(value: T) {
        mutableLiveData.postValue(value)
    }

    fun observe(lifecycleOwner: LifecycleOwner, observer: Observer<in T>) {
        distinctLiveData.observe(lifecycleOwner, observer)
    }
}

BaseViewModel.kt

/**usage: class MyViewModel(application: Application) : BaseViewModel(application)
 * getting instance:    private lateinit var viewModel: MyViewModel
 * viewModel=ViewModelProvider(this).get(MyViewModel::class.java)*/
abstract class BaseViewModel(application: Application) : AndroidViewModel(application) {
    @Suppress("MemberVisibilityCanBePrivate")
    var isCleared = false
    @Suppress("MemberVisibilityCanBePrivate")
    val onClearedListeners = ArrayList<Runnable>()

    @Suppress("unused")
    @SuppressLint("StaticFieldLeak")
    val context: Context = application.applicationContext
    @Suppress("unused")
    val handler = Handler(Looper.getMainLooper())

    override fun onCleared() {
        super.onCleared()
        isCleared = true
        onClearedListeners.forEach { it.run() }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文