Displaying connected bluetooth device - need help

I'm working on an app to make a remote-controlled outlet with an HC-05 module, Arduino, and solid-state relay. As a prerequisite, I wanted to make sure I could turn on and off the onboard LED on the Arduino.

I started out following this tutorial:

https://projecthub.arduino.cc/mukeshkp2005/arduino-with-bluetooth-to-control-an-led-a7ad0a

I liked that tutorial because it actually has you making the app instead of downloading one that someone had already made (see "The App" section in above link).

I was able to re-create that app, but when I went into the device listpicker once I built and installed the app, the list of devices came up blank. I figured it was a permissions issue and I found this post, which I incorporated some of the blocks from into my app:

https://community.appinventor.mit.edu/t/bluetooth-list-of-paired-devices-not-visible/83313/6

After making that update, it asked me for permission when I opened up the app:

and it shows the list of devices when I click on the listpicker button and I can select my HC-05 module:

When I click on that module address / name, it went back to the main screen, but the ON/OFF buttons had no effect on the on-board LED on the Arduino. I wanted to make sure the module was actually connected in the app, so I tried to add a section to the main screen where it displays the connected device (with a label that says "Connected To:" and another directly below it that is supposed to get populated with the device address (which doesn't show up on the screen preview unless you select it in the Components pane, but it is there)):

When I tried to run this version of the app, it gives me a 503 error after selecting the device from the listpicker:

Is there something wrong I'm doing with my blocks? Here's what they currently look like:

The phone I'm writing this app for is running Android version 12. Before running the app, I make sure the HC-05 module is on and paired to my phone (in my phone's bluetooth settings).

Here's my Arduino code if that's relevant at all:

// Use HC-05 bluetooth module to control onboard LED
// D. Botos
// started 8-18-23  last updated 8-18-23
// see https://projecthub.arduino.cc/mukeshkp2005/arduino-with-bluetooth-to-control-an-led-a7ad0a
// and https://docs.arduino.cc/tutorials/communication/SoftwareSerialExample
// and Blink example (File -> Examples -> 01.Basics -> Blink

// ----- ----- ----- -----

#include <SoftwareSerial.h>

// create SoftwareSerial object
SoftwareSerial BTSerial(3, 2);   // Rx to Arduino | Tx from Arduino - see https://docs.arduino.cc/learn/built-in-libraries/software-serial for syntax

// create switchstate variable (has to be char, not int)
char switchstate;

// ----- ----- ----- -----

// setup runs once
void setup() {
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  
  // start serial for bluetooth
  BTSerial.begin(38400);  // HC-05 default speed
  
}

// ----- ----- ----- -----

// loop runs repeatedly
void loop() {

  while(BTSerial.available()>0){
    
    switchstate = BTSerial.read();  // Serial.read() is to read the value coming from app
    
    if(switchstate  == '1'){
     digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on
    }
   
    else if(switchstate == '0'){
     digitalWrite(LED_BUILTIN, LOW);  // turn the LED off 
    }
    
  }  // end of while loop

}  // end of void loop

// end of sketch

Thanks,
David

Try this:

Also I hope your hc05 is connected to the correct arduino pins?

(Canned Response ABG - Bluetooth non-BLE SCAN Permission Blocks)

These blocks can be dragged into your Blocks Editor to request permission(s) for Bluetooth non-BLE scanning and connecting. All three parts are needed.

You also will need to pull in TinyDB into your project.



When Permission Granted (@Patryk)

Thanks to @Anke for the original code.
My changes include

  • use of the Screen1 permission blocks to avoid the possibility of a typo
  • using a TinyDB tag particular to BT permissions and only for BT permissions.

(Thanks to @Patryk for correction to grant order)
Special note for Xiaomi devices:

The only wrong thing seems the beginning, because I started clicking on a button to initialize.

In this moment I don't remember why you cannot start initializing screen1, but it shoulf be written on the comments on the forum.

The permission order should be

Scan

Connect

My older examples had it reversed.

Yes, using the software serial for the HC-05 ↔ Arduino connection is on purpose (so I can keep the main serial connection for the computer). Arduino D3 pin is Rx (connected to HC-05 TxD) and Arduino D2 is Tx (connected to HC-05 RxD).

I'm slightly confused about the terms "scan" and "connect" with regard to the phone itself and then the app. Here is my understanding from my limited knowledge of bluetooth (and its utilization by apps):

on the phone side (before you ever use the app):

-you scan for available devices in your bluetooth settings
-once you find your desired device found, you pair to that device (and it stays paired unless you delete the pairing)
-if a device is on, you can be connected to it

In the app, I was assuming you only needed permission to access a device that you're already connected to. Are the blocks that I have (or are being suggested above) trying to duplicate that ability to find (scan), pair, and connect to a new device? Just trying to understand what I actually need to put in the app (and what each set of blocks is doing).

Thanks,
David

the bluetooth component is able to scan and connect to already paired devices... to be able to do this you need both the connect and scan permissions

my bluetooth extension is additionally able to pair devices and offers the method RequestBluetoothPermission to request the required runtime permissions together in only one method

Taifun