Android Question Bluetooth example B4a-Arduino(solved)

Discussion in 'Android Questions' started by f0raster0, Jul 8, 2019.

  1. f0raster0

    f0raster0 Active Member Licensed 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:
    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:
    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);
          }
       }                     
    }
     
  2. MarkusR

    MarkusR Well-Known Member Licensed 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: Jul 8, 2019
    f0raster0 likes this.
  3. emexes

    emexes Well-Known Member 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: Jul 8, 2019
    f0raster0 likes this.
  4. emexes

    emexes Well-Known Member 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:
    Code:
    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: Jul 8, 2019
  5. emexes

    emexes Well-Known Member Licensed User

    Heck, it must be getting past my bedtime - I didn't even notice that the Arduino code was in C.

    :-/
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. f0raster0

    f0raster0 Active Member Licensed 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:
    Code:
    Sub Button1_Click
        
    Dim b As Byte
        b = 
    1
        
    CallSub2(Starter, "SendMessage"Array As Byte(0, b))
    End Sub
    or
    Code:
    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
     
  8. MarkusR

    MarkusR Well-Known Member Licensed User

    Code:
    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.
     
    f0raster0 likes this.
  9. emexes

    emexes Well-Known Member Licensed User

    is serial monitor a Bluetooth serial terminal app running on the same Android device as your B4A app?
    or is serial monitor a completely separate device?

    I thought the B4A is doing the sending, not the receiving/showing/LEDding.
     
  10. emexes

    emexes Well-Known Member 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:
    Code:
    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:
    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: Jul 9, 2019
    f0raster0 likes this.
  11. f0raster0

    f0raster0 Active Member Licensed User

    solved.
     
    emexes likes this.
  12. emexes

    emexes Well-Known Member 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.
     
    f0raster0 likes this.
  13. MarkusR

    MarkusR Well-Known Member Licensed User

    ahh, now i see
    u compared integer with string at #1
     
    f0raster0 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice