Android Question Bluetooth example B4a-Arduino(solved)

f0raster0

Well-Known Member
Licensed User
Longtime User
hi guys,

What do i have to do to make the Bluetooth example can work with this code Arduino
I added two buttons to test, nothing happen
Sub Button1_Click
Dim b As Byte
b = 1
CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Thanks

Code: Copy
Sub Switch1_CheckedChange(Checked As Boolean)
    Dim b As Byte
    If Checked Then b = 1 Else b = 0
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Sub btnConnect_Click
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        CallSub(Starter, "Connect")
    Else
        ToastMessageShow("No permission", True)
    End If
End Sub

Sub lblStatus_Click
    
End Sub

public Sub RetornaTextoBluetoothname As String
    Return bluetoothname.text.trim
End Sub

Sub Button1_Click
    Dim b As Byte
    b = 1
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub
Sub Button2_Click
    Dim b As Byte
    b = 0
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Code: Copy
void setup()
{
  Serial.begin(9600);         
  pinMode(13, OUTPUT);       
}
int state;

void loop(){
if(Serial.available() > 0) {
    state= Serial.read();   
    if(state == '0')         
      {
      digitalWrite(13, LOW); 
      }
    if(state == '1')
      {
      digitalWrite(13, HIGH);
      }
   }                     
}
 
at first if u have a uno board, disconnect usb cable "pc<->arduino"
at phone u need enable gps location + bt.

the bt device should indicate the connect state via small led. (myself used a DSD TECH HM-10 Bluetooth 4.0 BLE + voltage divider)

at your loop i would add a else part if state is other then 0 or 1
 
Last edited:
Upvote 0
It might be that you are sending byte values 0x00 and 0x01, but comparing the received byte against ASCII digits '0' and '1' ie byte values 0x30 and 0x31.
 
Last edited:
Upvote 0
I am slightly confused about why you are sending a 0x00 byte beforehand each button click too. At 9600 bps, that means that if your output is already on, and Button1_Click sends another "on" command, your output might go off for about a millisecond when the leading 0 is received.

Unless perhaps your original intention was to offset the transmitted value by ASCII '0' eg:
Code: Copy
CallSub2(Starter, "SendMessage", Array As Byte('0' + b))
which would fix the previous ASCII-vs-byte-value issue if we were programming in C but probably result in a syntax error in B4X.

So close, and yet so far...

:-)
 
Last edited:
Upvote 0
which would fix the previous ASCII-vs-byte-value issue if we were programming in C but probably result in a syntax error in B4X.
Heck, it must be getting past my bedtime - I didn't even notice that the Arduino code was in C.

:-/
 
Upvote 0
thanks guys.

yes, I'm using the example: bluetooth of b4a, but I need it working with a code Arduino.
When I connect the Bluetooth+ArduinoUNO and
from serial monitor I send 1 the b4a App shows 1 and turn on the led,
same when sending 0 the b4a shows 0 and turn off the led.

But nothing happen with the leds when clicking in the buttons of the App B4A:
Code: Copy
Sub Button1_Click
    Dim b As Byte
    b = 1
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub
or
Code: Copy
Sub Switch1_CheckedChange(Checked As Boolean)
    Dim b As Byte
    If Checked Then b = 1 Else b = 0
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub
 
Upvote 0
Code: Copy
Serial.available()>0
turn your led on here for a test if data comes in.
then turn your led on if state is <> 0
for second test if you really got 0 or 1.
be sure you not use usb cable because its serial same at bt device.

show the
SendMessage sub in starter service.
 
Upvote 0
To me it still looks like your App is sending byte values 0x00 and 0x01, and the Arduino is expecting byte values 0x30 and 0x31.

Try changing your send lines to:
Code: Copy
CallSub2(Starter, "SendMessage", Array As Byte(Bit.Or(0x30, b)))
which will send the ASCII '0' and '1' (byte values 0x30 and 0x31) that the Arduino is expecting, and/OR your receive code to:
Code: Copy
void loop(){
    if(Serial.available() > 0) {
        state= Serial.read(); 
        if(state & 0x01) {
            digitalWrite(13, HIGH);
        } else {
            digitalWrite(13, LOW);
        }
    }
}
which only looks at bit 0, and thus works for both "protocols" ie ASCII characters '0' and '1', and byte values 0x00 and 0x01
 
Last edited:
Upvote 0
Code: Copy
Sub Switch1_CheckedChange(Checked As Boolean)
    Dim b As Byte
    If Checked Then b = 1 Else b = 0
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Sub btnConnect_Click
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        CallSub(Starter, "Connect")
    Else
        ToastMessageShow("No permission", True)
    End If
End Sub

Sub lblStatus_Click
   
End Sub

public Sub RetornaTextoBluetoothname As String
    Return bluetoothname.text.trim
End Sub

Sub Button1_Click
    Dim b As Byte
    b = 1
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub
Sub Button2_Click
    Dim b As Byte
    b = 0
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub

Code: Copy
void setup()
{
  Serial.begin(9600);        
  pinMode(13, OUTPUT);      
}
int state;

void loop(){
if(Serial.available() > 0) {
    state= Serial.read();  
    if(state == 0)        
      {
      digitalWrite(13, LOW);
      }
    if(state == 1)
      {
      digitalWrite(13, HIGH);
      }
   }                    
}

solved.
 
Upvote 0
If you put an oscilloscope on output pin 13, I think you'll still see a 1 ms LOW pulse on each click of Button1 after the first one.

For most applications, this is probably of no consequence. But the output is a clock or latch signal to some logic, then it could be entertaining.
 
Upvote 0
Back
Top