Reputation: 11
I created an app that advertises BLE through flutter.
I used flutter_bluetooth_serial library.
I confirmed that advertising was working well through the
"I/BLE Peripheral state (22874): advertising"
message in the flutter log.
But I don't know how to recognize my phone on HM-10
This is my arduino code.
Response to AT is OK.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("Starting BLE scan...");
mySerial.println("AT");
delay(1000);
mySerial.println("AT+IMME0");
delay(1000);
mySerial.println("AT+ROLE1");
delay(1000);
mySerial.println("AT+DISC?");
}
void loop() {
if (mySerial.available()) {
String response = mySerial.readString();
Serial.println(response);
int nameIndex = response.indexOf("+NAME:");
if (nameIndex >= 0) {
String deviceName = response.substring(nameIndex + 6);
deviceName.trim();
Serial.println("Device Found: " + deviceName);
}
}
}
Upvotes: 1
Views: 65
Reputation: 13285
I believe there are possibly two main issues here:- firstly, flutter_bluetooth_serial is a classic Bluetooth library and therefore will not be scanned by BLE central devices. You might want to consider using a BLE library instead (e.g. flutter_blue, flutter_reactive_ble). I don't have much experience with flutter, so I would double check if BLE peripheral is supported first. For prototyping, you can simply use the nRF Connect app to get your device to act as a BLE peripheral.
Secondly, some HM-10 documentation seem to imply that you need to do an AT+RESET before the BLE role change takes effect. I would try that in your code before attempting to discover BLE peripherals.
Below are some references:-
Upvotes: 1