Android bluetooth connection state

Android: How to detect Bluetooth connection status

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) @Override public void onServiceConnected(int profile, BluetoothProfile proxy) NEVER List 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) < // >> > > 

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:

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(). adapter private var bluetoothHeadsetProfile: BluetoothProfile? = null val connectedDevicesLiveData = DistinctLiveDataWrapper(MutableLiveData(ConnectedDevicesState.Idle)) val bluetoothTurnedOnLiveData = DistinctLiveDataWrapper(MutableLiveData(null)) val isConnectedToBtHeadsetLiveData = DistinctLiveDataWrapper(MutableLiveData(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) < this@MainActivityViewModel.bluetoothHeadsetProfile = 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(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) : 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(@Suppress("MemberVisibilityCanBePrivate") val mutableLiveData: MutableLiveData) < @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) < 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() @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() >> > 

Источник

Читайте также:  Блютуз наушники xiaomi руководство

How can I programmatically tell if a Bluetooth device is connected?

I understand how to get a list of paired devices, but how can I tell if they are connected? It must be possible since I see them listed in my phone’s Bluetooth device list and it states their connection status.

7 Answers 7

Add the Bluetooth permission to your AndroidManifest,

Then use intent filters to listen to the ACTION_ACL_CONNECTED , ACTION_ACL_DISCONNECT_REQUESTED , and ACTION_ACL_DISCONNECTED broadcasts:

public void onCreate() < . IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); this.registerReceiver(mReceiver, filter); >//The BroadcastReceiver that listens for bluetooth broadcasts private final BroadcastReceiver mReceiver = new BroadcastReceiver() < @Override public void onReceive(Context context, Intent intent) < String action = intent.getAction(); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (BluetoothDevice.ACTION_FOUND.equals(action)) < . //Device found >else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) < . //Device is now connected >else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) < . //Done searching >else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) < . //Device is about to disconnect >else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) < . //Device has disconnected >> >; 
  • There is no way to retrieve a list of connected devices at application startup. The Bluetooth API does not allow you to query, instead it allows you to listen to changes.
  • A hoaky workaround to the above problem would be to retrieve the list of all known/paired devices. then trying to connect to each one (to determine if you’re connected).
  • Alternatively, you could have a background service watch the Bluetooth API and write the device states to disk for your application to use at a later date.

Источник

How to Show bluetooth Connection status in android

I am developing one app. I need to show the status whether device is connected or not to another paired device. I am able to show toast message but am unable to show whether bluetooth is connected to other device or not in the List view. Can you please look at my added code once and help me in the issue.. my DeviceListActivity.class

 mListView = (ListView) findViewById(R.id.lv_paired); BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Set pairedDevices = mBluetoothAdapter.getBondedDevices(); mDeviceList = new ArrayList(); mDeviceList.addAll(pairedDevices); mAdapter = new DeviceListAdapter(this); mAdapter.setData(mDeviceList); mAdapter.setListener(new OnConnectButtonClickListener() < public void onConnectButtonClick(int position) < BluetoothDevice device = mDeviceList.get(position); ConnectDevice(device); >>); mListView.setAdapter(mAdapter); registerReceiver(mConnectReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED)); registerReceiver(mConnectReceiver, new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED)); > private void ConnectDevice(BluetoothDevice device) < try < // SERIAL_UUID = device.getUuids()[0].getUuid(); // msocket = device.createInsecureRfcommSocketToServiceRecord(SERIAL_UUID); Method method = device.getClass().getMethod("createRfcommSocket", new Class[] < int.class >); msocket = (BluetoothSocket) method.invoke(device, 2); msocket.connect(); > catch (Exception ex) < ex.printStackTrace(); try < msocket.close(); >catch (IOException closeEx) < closeEx.printStackTrace(); >return; > > private final BroadcastReceiver mConnectReceiver = new BroadcastReceiver() < public void onReceive(Context context, Intent intent) < String action = intent.getAction(); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) < showToast("BlueTooth is Connected"); >else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) < showToast("BlueTooth DisConnected"); >mAdapter.notifyDataSetChanged(); > >; @Override public void onDestroy()
public class DeviceListAdapter extends BaseAdapter < private LayoutInflater mInflater; private ListmData; private OnConnectButtonClickListener connectListener; public DeviceListAdapter(Context context) < mInflater = LayoutInflater.from(context); >public void setData(List data) < mData = data; >public void setListener(OnConnectButtonClickListener listener) < connectListener = listener; >public int getCount() < return (mData == null) ? 0 : mData.size(); >public Object getItem(int position) < return null; >public long getItemId(int position) < return position; >public View getView(final int position, View convertView, ViewGroup parent) < ViewHolder holder; if (convertView == null) < convertView = mInflater.inflate(R.layout.list_item_device, parent, false); holder = new ViewHolder(); holder.nameTv = (TextView) convertView.findViewById(R.id.tv_name); holder.addressTv = (TextView) convertView.findViewById(R.id.tv_address); holder.connectBtn = (Button) convertView.findViewById(R.id.btn_connect); convertView.setTag(holder); >else < holder = (ViewHolder) convertView.getTag(); >BluetoothDevice device = mData.get(position); holder.nameTv.setText(device.getName()); holder.addressTv.setText(device.getAddress()); holder.connectBtn.setText("connect"); holder.connectBtn.setText("connected"); holder.connectBtn.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < if (connectListener != null) < connectListener.onConnectButtonClick(position); >> >); return convertView; > static class ViewHolder < TextView nameTv; TextView addressTv; Button connectBtn; >public interface OnConnectButtonClickListener

Now by using register receiver, when click on connect I need to update the status of list item as connected. Any help in this issue can save my day. Thanks in advance.

Читайте также:  Bluetooth jbl tune 120tws

Источник

Оцените статью
Adblock
detector