Android Question BLE2 Library issue. ("Unknown Type: ble2")

JacoMuller

Member
Licensed User
Longtime User
I am doing a project to monitor the signals for the lights on a tow wiring harness. I want to read 5 GPIO signals with my ESP32. The names of the signals are Left indicator, Right indicator, Brake, Reverse, Tail/Park. I want to do the sketch in Arduino IDE. I then want to connect to my Samsung Phone via Bluetooth. I want an app on my Samsung phone, written in B4A to show the status of the signals. The ESP32 sketch is done and I have tested the Bluetooth message with nRF Connect - all good. The issue is with B4A and more specific the BLE2 Library. I cannot get B4A to compile. There is an error "Unknown Type: ble2". I have selected BLE2 in Library manager. Installed version 1.41, online version 1.39. I have used ChatGPT to assist with the code for B4A as I am familiar with Arduino IDE and ESP32 for ESP32 code. The first round of code from ChatGPT was comprehensive and included everything that I have asked for. Only issue is, the BLE2 library error keeps popping up. ChatGPT then simplified the code for fault finding but the error is still there. I am at a point where the code is only:
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Private lblStatus As Label
    Private btnConnect As Button
    Private ble As BLE2
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainPage")
    lblStatus.Text = "Disconnected"
    btnConnect.Text = "CONNECT"
End Sub

Sub btnConnect_Click
    lblStatus.Text = "Button pressed"
End Sub
The error is in the line 7, "Private ble As BLE2".

What am I missing?
 

JacoMuller

Member
Licensed User
Longtime User
Have you used the ble2 library before? there is no type ble2, perhaps it is BleManager2, here is the example.
Thanks for the feedback:). No, I am completely new to B4A. I have done a number of VB and VBA projects and I am working with Arduino IDE for my ESP32 projects. Normally, I don't have a need to do an App for Android but in this case, the "user interface" for my ESP32 must be on an Android phone, connected via Bluetooth or BLE. The code I have so far is ChatGPT generated. The reason for going for B4A is that I am familiar with VB and I think the learning curve will be shorter. I will have a look at the example.
 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
Longtime User
I suggest you start with the example project that Erel posted in the thread you were already given the reference to. It works very well.

 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I am doing a project to monitor the signals for the lights on a tow wiring harness. I want to read 5 GPIO signals with my ESP32. The names of the signals are Left indicator, Right indicator, Brake, Reverse, Tail/Park. I want to do the sketch in Arduino IDE. I then want to connect to my Samsung Phone via Bluetooth. I want an app on my Samsung phone, written in B4A to show the status of the signals. The ESP32 sketch is done and I have tested the Bluetooth message with nRF Connect - all good. The issue is with B4A and more specific the BLE2 Library. I cannot get B4A to compile. There is an error "Unknown Type: ble2". I have selected BLE2 in Library manager. Installed version 1.41, online version 1.39. I have used ChatGPT to assist with the code for B4A as I am familiar with Arduino IDE and ESP32 for ESP32 code. The first round of code from ChatGPT was comprehensive and included everything that I have asked for. Only issue is, the BLE2 library error keeps popping up. ChatGPT then simplified the code for fault finding but the error is still there. I am at a point where the code is only:
B4X:
Sub Process_Globals
End Sub

Sub Globals
    Private lblStatus As Label
    Private btnConnect As Button
    Private ble As BLE2
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MainPage")
    lblStatus.Text = "Disconnected"
    btnConnect.Text = "CONNECT"
End Sub

Sub btnConnect_Click
    lblStatus.Text = "Button pressed"
End Sub
The error is in the line 7, "Private ble As BLE2".

What am I missing?

That one is based in:

B4X:
Sub Manager_DeviceFound (Name As String, Id As String, AdvertisingData As Map, RSSI As Double)
    Log("Found: " & Name & ", " & Id & ", RSSI = " & RSSI & ", " & AdvertisingData) 'ignore
'    If Id = "6D:D4:F2:0C:A4:74" Then
    If Name="ESP32-C3" Then
        ConnectedName = Name
        manager.StopScan
        Log("connecting")
        #if B4A
        manager.Connect2(Id, False) 'disabling auto connect can make the connection quicker
        #else if B4I
            manager.Connect(Id)
        #end if       
    End If
End Sub
 
Upvote 0

JacoMuller

Member
Licensed User
Longtime User
I am doing a project to monitor the signals for the lights on a tow wiring harness. I want to read 5 GPIO signals with my ESP32. The names of the signals are Left indicator, Right indicator, Brake, Reverse, Tail/Park. I want to do the sketch in Arduino IDE. I then want to connect to my Samsung Phone via Bluetooth. I want an app on my Samsung phone, written in B4A to show the status of the signals. The ESP32 sketch is done and I have tested the Bluetooth message with nRF Connect - all good.

Because I am new to B4A, I made friends with ChatGPT and we have been in a conversation for 4 days now. I started with the full brief in detail as what the B4A app should do. After days of almost getting where, but just not, I lowered the bar a bit. I have asked ChatGPT just to do the following:

I just need an App that will connect to the ESP32 BLE without looking at any other devices. I then want the lblStatus.text to show if the BLE connection was successful or not. If successful, I want the lblDeviceStatus.text to show the value of the received data. Every time the data change the text of lblDeviceStatus must change. At this point ChatGPT is busy ruining our friendship. So I need to ask for better assistance. This is the ESP32 Sketch:



BLE_TowWireMonitor:
//Pin Definitions
#define PIN_LEFT     32
#define PIN_RIGHT    33
#define PIN_BRAKE    25
#define PIN_REVERSE  26
#define PIN_TAIL     27

//BLE Definitions
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define SERVICE_UUID        "c1c10000-0000-0000-0000-000000000001"
#define CHARACTERISTIC_UUID "c1c10000-0000-0000-0000-000000000002"

//Global Objects
BLECharacteristic *statusCharacteristic;
bool deviceConnected = false;

//Connection Callbacks
class MyServerCallbacks : public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
  }

  void onDisconnect(BLEServer* pServer) {
    deviceConnected = false;
    pServer->getAdvertising()->start();
  }
};

//Trailer Status Structure
struct TrailerStatus {
  bool left;
  bool right;
  bool brake;
  bool reverse;
  bool tail;
};

//Read Inputs
TrailerStatus readTrailer() {
  TrailerStatus s;
  s.left    = digitalRead(PIN_LEFT);
  s.right   = digitalRead(PIN_RIGHT);
  s.brake   = digitalRead(PIN_BRAKE);
  s.reverse = digitalRead(PIN_REVERSE);
  s.tail    = digitalRead(PIN_TAIL);
  return s;
}

//Convert to Bitmask (1 byte)
uint8_t makeBitmask(TrailerStatus s) {
  return (s.left    << 0) |
         (s.right   << 1) |
         (s.brake   << 2) |
         (s.reverse << 3) |
         (s.tail    << 4);
}

 //Setup
void setup() {
  Serial.begin(115200);

 
  Serial.println("##########################");
  Serial.println("File Name: 'BLE_TowWireMonitor.ino'");
  Serial.println("##########################");
  Serial.println("");


  pinMode(PIN_LEFT, INPUT_PULLDOWN);
  pinMode(PIN_RIGHT, INPUT_PULLDOWN);
  pinMode(PIN_BRAKE, INPUT_PULLDOWN);
  pinMode(PIN_REVERSE, INPUT_PULLDOWN);
  pinMode(PIN_TAIL, INPUT_PULLDOWN);

  BLEDevice::init("Trailer Tester");

  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  BLEService *pService = pServer->createService(SERVICE_UUID);

  statusCharacteristic = pService->createCharacteristic(
    CHARACTERISTIC_UUID,
    BLECharacteristic::PROPERTY_READ |
    BLECharacteristic::PROPERTY_NOTIFY
  );

  statusCharacteristic->addDescriptor(new BLE2902());

  pService->start();

  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->start();

  Serial.println("BLE Trailer Tester ready");
}
//Loop
void loop() {
  static uint32_t lastUpdate = 0;

  if (deviceConnected && millis() - lastUpdate > 100) {
    TrailerStatus status = readTrailer();
    uint8_t data = makeBitmask(status);

    statusCharacteristic->setValue(&data, 1);
    statusCharacteristic->notify();

    lastUpdate = millis();
  }
}
 
Upvote 0
Top