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

B4X:
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

B4X:
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);
      }
   }                     
}
 

MarkusR

Well-Known Member
Licensed User
Longtime User
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

emexes

Expert
Licensed User
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

emexes

Expert
Licensed User
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:
B4X:
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

emexes

Expert
Licensed User
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

f0raster0

Well-Known Member
Licensed User
Longtime User
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:
B4X:
Sub Button1_Click
    Dim b As Byte
    b = 1
    CallSub2(Starter, "SendMessage", Array As Byte(0, b))
End Sub
or
B4X:
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

MarkusR

Well-Known Member
Licensed User
Longtime User
B4X:
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

emexes

Expert
Licensed User
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:
B4X:
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:
B4X:
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

f0raster0

Well-Known Member
Licensed User
Longtime User

solved.
 
Upvote 0

emexes

Expert
Licensed User
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…