Android Question I'm having trouble reading from a BLE device

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm using the lightblue bean, which is a small arduino with bluetooth.

Here's the code running on the LBB

B4X:
bool lastState = 0;
bool ledstate = 0;

void setup() {
   // Initialize serial communication
   Serial.begin(57600);

   // Set D0 to pullup mode
   pinMode(0, INPUT_PULLUP);
}

void loop() {
   bool newState = digitalRead(2);

   if (lastState != newState) {
     lastState = newState;
     Serial.print("Pin changed: ");
     Serial.println(newState);
   }

   if(lastState){
     Bean.setLed(0,0,255);
   } else {
     if(ledstate){
       Bean.setLed(255,0,0);
     } else {
       Bean.setLed(0,255,0);
     }
     ledstate  = !ledstate;
   }

   // Sleep for a bit before checking the pins again
   Bean.sleep(10);
}

I can tell it's reading the button, because the light turns blue.
I can connect to the LBB using the BLE example, read data gets a bunch of data but I don't see the "Pin changed: "... What do I need to do to get the android app to receive data from the LBB as it's sent, as opposed to polling it?
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I got it working, it has it's own weird protocol.

The key is:
B4X:
Bean.setScratchNumber(1, buffer);

Then reading the service like so:

B4X:
Sub Process_Globals
Public KeyStart As String = "a495ff2", KeyEnd As String = "-c5b1-4b44-b512-1370f02d74de"
end sub

Public Sub ReadData
   manager.ReadData(KeyStart & 0 & KeyEnd)
   
   'For Each s As String In ConnectedServices
   '   Log("Reading " & s)
   '   manager.ReadData(s)
   'Next
End Sub

Sub Manager_DataAvailable (ServiceId As String, Characteristics As Map)
   Dim temp As Int, bc As ByteConverter, tempstr As String
   CallSub3(Main, "DataAvailable", ServiceId, Characteristics)
   Log("Service: " & ServiceId & " " & Characteristics)
   
   For temp = 0 To Characteristics.Size -1
     Dim data() As Byte = Characteristics.GetValueAt(temp)
     'a495ff21-c5b1-4b44-b512-1370f02d74de
     tempstr = Characteristics.GetKeyAt(temp)
     If tempstr.StartsWith(KeyStart) And tempstr.EndsWith(KeyEnd) Then
       Log("Scratch: " & tempstr.SubString2(7,8) & " = " & bc.HexFromBytes(data))
     End If
   Next
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Turns out that was the wrong/slow way. There's a way to enable serial data transfer in real time, but I don't know how to receive that data
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
That would explain it...

https://punchthrough.com/bean/guides/features/virtual-serial/#how-virtual-serial-works

Bluetooth Low Energy doesn't natively support serial communication.

Unlike Bluetooth Classic, BLE doesn't have a Serial Port Profile (SPP). Instead, Bean's firmware sends serial messages over BLE using the protocol we developed for the LightBlue Platform. On the other end, your computer parses these messages and converts them back into serial.

When an Arduino sketch writes serial data, Bean Loader receives it, parses it, and passes the data through to the virtual serial port. To see data that Bean is sending, we can use the serial monitor in Arduino IDE.
 
Upvote 0
Top