Hi,
I'm working on an application that involves an Arduino 101 sending data to a phone via Bluetooth. I'm using the CurieBLE library and testing the program with the nRF Master Control Panel app.
Whenever the phone attempts to disconnect from the Arduino, the disconnection is successful. Calling blePeripheral.connected() will return false, as expected.
However, when I attempt to make the Arduino initiate the disconnection by calling blePeripheral.disconnect() from the Arduino sketch, the disconnect() function always returns false, meaning that it has failed to disconnect successfully, and the Arduino remains connected to the phone.
I don't understand why only the phone is able to initiate a disconnection. The Arduino needs to be able to call disconnect() itself if the phone ever goes out of range and fails to disconnect properly.
I have attached a very simple Arduino sketch that demonstrates the error I've been getting in my main program. If anyone has any insights, please let me know.
Thanks!
// This program sends the numbers 0 through 250 to a phone
// as fast as possible. It then attempts to disconnect from
// the phone.
#include <CurieBLE.h>
#define MAX 250
BLEPeripheral blePeripheral;
BLECentral bleCentral = blePeripheral.central();
BLEService myService("aa7b3c40-f6ed-4ffc-bc29-5750c59e74b3");
BLECharacteristic myChar("b0351694-25e6-4eb5-918c-ca9403ddac47", BLERead | BLENotify, 1);
int counter; // the data we're sending
boolean isConnected = false;
void setup() {
Serial.begin(115200);
while(!Serial);
setUpBLE(); // sets up the bluetooth services and characteristics
}
void setUpBLE() {
blePeripheral.setLocalName("Brett");
blePeripheral.setAdvertisedServiceUuid(myService.uuid());
blePeripheral.addAttribute(myService);
blePeripheral.addAttribute(myChar);
blePeripheral.begin();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop(){
if(isConnected){
if(blePeripheral.connected()){
if (counter <= MAX){ // send the numbers 0-250 to the phone
unsigned char myCharArray[1] = {(unsigned char) counter };
myChar.setValue(myCharArray, 1); // send to phone
Serial.println(counter);
if (counter == MAX) {
if (blePeripheral.disconnect()) { // disconnect after counter reaches 250
// this is executed when the Arduino disconnects from the phone
Serial.print("disconnected from central: ");
Serial.println(bleCentral.address());
isConnected = false;
} else {
// THIS IS PRINTED EVERYTIME I TRY TO DISCONNECT ![]()
Serial.println("disconnect failed");
}
}
counter++;
}
} else {
// this is executed when the phone disconnects from the Arduino
Serial.print("disconnected from by central: ");
Serial.println(bleCentral.address());
isConnected = false;
}
} else {
// if we're not currently connected, attempt to connect
bleCentral = blePeripheral.central();
// this is executed whenever the Arduino connects or reconnects to the phone
if (bleCentral){
Serial.print("connected to central: ");
Serial.println(bleCentral.address());
counter = 0;
isConnected = true;
}
}
}
testing_disconnect.ino (2.42 KB)