0

I have a way to connect a Bluetooth Device. Now I'm trying to show a popup dialog that shows when the app has no connection with the Bluetooth Device.

I already have the popup dialog, I only need to create a IF statement that checks if the app has a connection with a Bluetooth Device, if true: it doesn't show the popup dialog, if no: it shows the dialog and tries to reconnect with the Bluetooth Device.

This is the popup:

    val bluetoothDialog = AlertDialog.Builder(this@MainActivity)
    val bluetoothView = layoutInflater.inflate(R.layout.bluetoothdialog, null)
    
    bluetoothDialog.setTitle("Bluetooth Connection Status")
    bluetoothDialog.setView(bluetoothView)
    bluetoothDialog.setPositiveButton("OK") { _,_ ->}
    bluetoothDialog.setCancelable(false)

    testbutton.setOnClickListener{
        if(connection == true){
            "Don't show popup"
        }else{
            bluetoothDialog.show()
            "and Retry connection"
        }
    }
mohammed_sajid
  • 233
  • 2
  • 18
  • Does this answer your question? [How can I programmatically tell if a Bluetooth device is connected?](https://stackoverflow.com/questions/4715865/how-can-i-programmatically-tell-if-a-bluetooth-device-is-connected) – Dmytro Rostopira Nov 08 '22 at 13:14
  • I already looked into that, but that only updates when a device connects or disconnects, I need to check on startup if a device is connected. – mohammed_sajid Nov 08 '22 at 13:19
  • As far as I see, you wanna let the user know that a connection failed and let the user retry the connection. If so, this is the [BluetoothGattCallback.onconnectionstatechange](https://developer.android.com/reference/kotlin/android/bluetooth/BluetoothGattCallback#onconnectionstatechange) callback what for. It is more convenient to warn the user in this callback. Or if you try something else you may need to save connected gatt objects in a list depending on your implementation. – Kozmotronik Nov 08 '22 at 13:52
  • Have you checked second answer on that question? – Dmytro Rostopira Nov 08 '22 at 15:00

0 Answers0