Skip to content

Instantly share code, notes, and snippets.

@vlad-ds
Created August 3, 2023 10:15
Show Gist options
  • Save vlad-ds/3b8903ba87b8335ce2f9cdd186a70064 to your computer and use it in GitHub Desktop.
Save vlad-ds/3b8903ba87b8335ce2f9cdd186a70064 to your computer and use it in GitHub Desktop.
Android: Bluetooth Device App
package com.example.cyborg2
import android.bluetooth.BluetoothA2dp
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHeadset
import android.bluetooth.BluetoothProfile
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.activity.ComponentActivity
import android.util.Log
import android.view.KeyEvent
class MainActivity : ComponentActivity() {
private lateinit var bluetoothAdapter: BluetoothAdapter
private var bluetoothHeadset: BluetoothHeadset? = null
private lateinit var headsetFilter: IntentFilter
private val profileListener = object: BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = proxy as BluetoothHeadset
Log.d("MainActivity", "BluetoothHeadset connected")
}
}
override fun onServiceDisconnected(profile: Int) {
if (profile == BluetoothProfile.HEADSET) {
bluetoothHeadset = null
Log.d("MainActivity", "BluetoothHeadset disconnected")
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize BluetoothAdapter
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
// Initialize IntentFilter
headsetFilter = IntentFilter().apply {
addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT)
addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)
addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)
addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED)
addAction(BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED)
}
// Establish connection to the proxy.
bluetoothAdapter.getProfileProxy(this, profileListener, BluetoothProfile.HEADSET)
}
override fun onStart() {
super.onStart()
// Register for BluetoothHeadset broadcasts
registerReceiver(bluetoothHeadsetBroadcastReceiver, headsetFilter)
}
override fun onStop() {
super.onStop()
// Unregister BluetoothHeadset broadcasts
unregisterReceiver(bluetoothHeadsetBroadcastReceiver)
}
private val bluetoothHeadsetBroadcastReceiver = object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED -> {
val state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1)
Log.d("MainActivity", "BluetoothHeadset state: $state")
}
BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED -> {
val state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1)
Log.d("MainActivity", "BluetoothHeadset audio state: $state")
}
BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT -> {
val command = intent.getStringExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_CMD)
val args = intent.getIntArrayExtra(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS)
Log.d("MainActivity", "BluetoothHeadset event: $command, args: ${args?.contentToString()}")
}
BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED -> {
val state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)
Log.d("MainActivity", "BluetoothA2dp connection state: $state")
}
BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED -> {
val state = intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, -1)
Log.d("MainActivity", "BluetoothA2dp playing state: $state")
}
Intent.ACTION_MEDIA_BUTTON -> {
val keyEvent = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
Log.d("MainActivity", "Media button event: ${keyEvent?.keyCode}")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment