Search code examples
androidbluetooth-lowenergyreconnectarduino-nano

I'm trying to implement autoConnect between android app and Arduino ble


I'm android developer and facing some autoConnect issue.

I'm using Arduino nano IOT and developed some simple application to communicate. but I don't know why autoConnect doesn't work in this case.

Arduino nano continuously sends gyroscope sensing data(but only when app connected to module)

below is sample code.

#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h>

BLEService sensorService("66df5109-edde-4f8a-a5e1-02e02a69cbd5");
BLEStringCharacteristic xSensorLevel("741c12b9-e13c-4992-8a5e-fce46dec0bff", BLERead | BLENotify,15);
BLEStringCharacteristic ySensorLevel("baad41b2-f12e-4322-9ba6-22cd9ce09832", BLERead | BLENotify,15);
BLEStringCharacteristic zSensorLevel("5748a25d-1834-4c68-a49b-81bf3aeb2e50", BLERead | BLENotify,15);

// last sensor data
float oldXLevel = 0;
float oldYLevel = 0;
float oldZLevel = 0;
long previousMillis = 0;


void setup() {
 Serial.begin(9600);
 
 while (!Serial);
 
 if (!IMU.begin()) {
  Serial.println("Failed to initialize IMU!");
  while (1);
 }
 
 pinMode(LED_BUILTIN, OUTPUT);
 
 if (!BLE.begin()) {
  Serial.println("starting BLE failed!");
  while (1);
 }

 BLE.setLocalName("Demo Gyroscope");
 BLE.setAdvertisedService(sensorService);
 sensorService.addCharacteristic(xSensorLevel);
 sensorService.addCharacteristic(ySensorLevel);
 sensorService.addCharacteristic(zSensorLevel);
 BLE.addService(sensorService);

 BLE.advertise();
 Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
 BLEDevice central = BLE.central();
 if (central) {
  Serial.print("Connected to central: ");
  Serial.println(central.address());
  digitalWrite(LED_BUILTIN, HIGH);

  while (central.connected()) {
   //long currentMillis = millis();
   updateGyroscopeLevel();
   delay(300);
  }

  digitalWrite(LED_BUILTIN, LOW);
  Serial.print("Disconnected from central: ");
  Serial.println(central.address());
 }
}

void updateGyroscopeLevel() {
 float x, y, z;
 if (IMU.gyroscopeAvailable()) {
  IMU.readGyroscope(x, y, z);
  if (x != oldXLevel) {
   xSensorLevel.writeValue(String(x));
   oldXLevel = x;
  }
 if (y != oldYLevel) {
  ySensorLevel.writeValue(String(y));
  oldYLevel = y;
 }
 
 if (z != oldZLevel) {
  zSensorLevel.writeValue(String(z));
  oldZLevel = z;
 }
 
 Serial.print(x);
 Serial.print('\t');
 Serial.print(y);
 Serial.print('\t');
 Serial.println(z);
 }
}

and in my android app, I have set autoConnect true

    private fun connectDevice(device: BluetoothDevice?) {
        // update the status
        broadcastUpdate(Actions.STATUS_MSG, "Connecting to ${device?.address}")
        bleGatt = device?.connectGatt(context, true, gattClientCallback)
    }

App can connect to module and read/write some data using UUID but when I turn off the module and turn on again, App cannot automatically connect it.

As far as I know, once I set it true, android store bt info as cache and trying to reconnect repeatedly. (fyi, I'm not using Service to maintain connection)

but in my case, when I turn the module on again, it just shows below message in serial monitor

Bluetooth device active, waiting for connections...

It seems like app doesn't retry to connect. I have read related questions and answers over here but couldn't find clue for my case.

My question over here is am I doing wrong? or this is normal behaviour? when it comes to bt earphone, it's automatically connected when turn on. so I'm thinking something like that.

Please share any idea for this.

Really appreciate it!


Solution

  • After reading more articles, I found what's the issue.

    Not sure if anyone reaching out to this question but in my case, I want app to reconnect to ble automatically even after turning off and on the ble.

    But I found turning off/on the phone, turning off/on ble clear the cache in android internally. so Cannot automatically reconnect in this scenario.

    Please have a look. this is really helpful for me

    https://medium.com/@martijn.van.welie/making-android-ble-work-part-2-47a3cdaade07