My Cart

How to Accept or Reject Incoming BT Connection Using Bluetooth Connectivity Kit

Difficulty Level: Expert -

1. Introduction

This tutorial allows the user to accept or reject an incomming Bluetooth connection with 2 different buttons.

Ingredients:

    - 1 x Bluetooth Connectivity Kit:
    • 1x Platform
    • 1x Bluetooth PRO
    • 1x XBee Shield
    • 2x 10kΩ resistors
    • 2x Buttons
    • 1x LCD
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now
NOTE: Depending on the platform chosen to develop the project, the ingredients and the schematics shown in this tutorial can vary.

This project can be developed with Arduino or Intel Galileo. It is also compatible with Raspberry Pi using the Raspberry Pi to Arduino shields connection bridge.

For further information about the Bluetooth Pro Shield, consult the main tutorial.

Step 1: Connection

Connect the XBee shield to Arduino and then connect Bluetooth PRO module or you can connect it directly to Raspberry Pi connection bridge. Connect the buttons with the resistors and the LCD as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to the VCC supply (5V in Arduino and 3.3V in Raspberry Pi) and ground.

Follow the wire color diagram to connect all LCD pins.

Connect digital pin 6 and 7 to one leg of the button. Place a resistor in the opposed leg of the button and connect it to GND. Connect the other leg of the button to VCC.

Step 2: The Code

Arduino:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Luis Miguel MartĂ­
 */
 
#include <LiquidCrystal.h>

#define BUTTON1 6
#define BUTTON2 7

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char error[] = "SYNTAX ERROR";
char event[80];
char mac[18];
char devName[16];
boolean ok;
boolean reject;

char aux_str[30];
char aux_str2[30];

void setup(){
  
  lcd.begin(16, 2);
  Serial.begin(115200);
  lcd.print("BT Module");
  delay(2000);
  Serial.print("Setting up...");
  lcd.clear();
  lcd.print("Setting up...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET BT PAIR *", "", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0980", "SET CONTROL CONFIG 0000 0000 0980", error, 1000) == 2);
  delay(500);
  Serial.print("Done...");
  lcd.clear();
  lcd.print("Done...");
  delay(500);
  memset(event,0,40);
  memset(mac,0,18);
  memset(devName,0,16);
  delay(1000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  lcd.clear();
  lcd.print("Waiting for ");
  lcd.setCursor(0,1);
  lcd.print("connection...");
}


void loop(){
  
  if(Serial.available()>0){
    lcd.clear();
    lcd.print("Establishing a");
    lcd.setCursor(0,1);
    lcd.print("connection...");
    readEvent();
    strcpy(devName, getName());
    Serial.println(devName);
    lcd.clear();
    lcd.print("Conn incoming");
    lcd.setCursor(0,1);
    byte i =1;
    while(devName[i]!='"'){
      lcd.print(devName[i]);
      i++;
    }
    delay(500);    
    ok = digitalRead(BUTTON1);
    reject = digitalRead(BUTTON2);
    Serial.print(ok);
    Serial.print(reject);
    while(!ok && !reject){
      ok = digitalRead(BUTTON1);
      reject = digitalRead(BUTTON2);
    }
    if (ok){   
      sprintf(aux_str, "AUTH %s 0000\r\n", mac);
      sprintf(aux_str2, "PAIR %s\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      
      Serial.print("Connected to ");
      Serial.println(devName);
      lcd.clear();
      lcd.print("Connected to ");
      lcd.setCursor(0,1);
      i =1;
      while(devName[i]!='"'){
        lcd.print(devName[i]);
        i++;
      }
    }else if (reject){
      sprintf(aux_str, "AUTH %s?\r\n", mac);
      sprintf(aux_str2, "AUTH %s?\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      while (sendATcommand(aux_str, "", error, 1000) == 2);
      
      Serial.print("Rejected conn to");
      Serial.println(devName);
      lcd.clear();
      lcd.print("Rejected conn to");
      lcd.setCursor(0,1);
      i =1;
      while(devName[i]!='"'){
        lcd.print(devName[i]);
        i++;
      }
    }
    ok = false;
    reject = false;
    delay(2500);
    while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
    lcd.clear();
    lcd.print("Waiting for ");
    lcd.setCursor(0,1);
    lcd.print("connection...");

  }
  memset(aux_str, '0', 30);  // Initialize the string
  memset(aux_str2, '0', 30);    // Initialize the string
  delay(1000);
}


int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return answer;
}

char* sendNAMEcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[200];
  unsigned long previous;
  memset(response, '\0', sizeof(response));    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  delay(1000);

    while (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
    }
      char* pch = strstr(response, expected_answer);
      if (pch != NULL) {
        return pch;
      } else if (strstr(response, error_answer) != NULL) {
        return "error";
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    // Waits for the asnwer with time out

  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return NULL;
}

void readEvent(){
  
  byte i=0;
  while(Serial.available()>0){
  
    event[i] = Serial.read();
    i++;
    delay(10);
  }
  Serial.print(event);
  byte j=0;
  boolean go_on = true;
  while (go_on && j<80){
    if(event[j]=='A'){
      if(event[j+1]=='U'){
        if(event[j+2]=='T'){
          if(event[j+3]=='H'){
                    for (byte z=0;z<17;z++){
                      mac[z] = event[z+j+5];
                    } 
                    Serial.println(mac);
                    go_on = false;
          }else j++;
        }else j++;
      }else j++;
    }else j++;
  }
  delay(1000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
}

char* getName(){
  
  char aux[23];
  sprintf(aux, "NAME %s\r\n", mac);
  char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  while (ans == "error"){
    char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  }
  
  delay(200);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return ans;
}
        

Raspberry Pi:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include "arduPi.h"

#define BUTTON1 6
#define BUTTON2 7

char error[] = "SYNTAX ERROR";
char event[80];
char mac[18];
char devName[16];
boolean ok;
boolean reject;

char aux_str[30];
char aux_str2[30];

int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout);
char* sendNAMEcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout);
void readEvent();
char* getName();

void setup(){
	
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  
  Serial.begin(115200);
  printf("BT Module\n");
  delay(2000);
  printf("Setting up...\n");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET BT PAIR *", "", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0980", "SET CONTROL CONFIG 0000 0000 0980", error, 1000) == 2);
  delay(500);
  printf("Done...\n");
  delay(500);
  memset(event,0,40);
  memset(mac,0,18);
  memset(devName,0,16);
  delay(1000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  printf("Waiting for connection...\n");
}


void loop(){
  
  if(Serial.available()>0){
    printf("Establishing a connection...\n");
    readEvent();
    strcpy(devName, getName());
    printf("%s\n",devName);
    printf("Conn incoming\n");
    byte i =1;
    delay(500);    
    ok = digitalRead(BUTTON1);
    reject = digitalRead(BUTTON2);
    
    while(!ok && !reject){
      ok = digitalRead(BUTTON1);
      reject = digitalRead(BUTTON2);
    }
    if (ok){   
      sprintf(aux_str, "AUTH %s 0000\r\n", mac);
      sprintf(aux_str2, "PAIR %s\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      
      printf("Connected to %s\n",devName);
      i =1;
    }else if (reject){
      sprintf(aux_str, "AUTH %s?\r\n", mac);
      sprintf(aux_str2, "AUTH %s?\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      while (sendATcommand(aux_str, "", error, 1000) == 2);
      
      printf("Rejected conn to %s\n",devName);
      i =1;
    }
    ok = false;
    reject = false;
    delay(2500);
    while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
    printf("Waiting for connection...\n");

  }
  memset(aux_str, '0', 30);  // Initialize the string
  memset(aux_str2, '0', 30);    // Initialize the string
  delay(1000);
}


int8_t sendATcommand(const char* ATcommand, const char* expected_answer, const char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial.println(expected_answer);
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
        answer = 1;
        printf("Expected response\n");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return answer;
}

char* sendNAMEcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[200];
  unsigned long previous;
  memset(response, '\0', sizeof(response));    // Initialize the string
  delay(100);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  delay(1000);

    while (Serial.available() != 0) {
      response[x] = Serial.read();
      x++;
    }
      char* pch = strstr(response, expected_answer);
      if (pch != NULL) {
        return pch;
      } else if (strstr(response, error_answer) != NULL) {
        return "error";
        while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
      }
    // Waits for the asnwer with time out

  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return NULL;
}

void readEvent(){
  
  byte i=0;
  while(Serial.available()>0){
  
    event[i] = Serial.read();
    i++;
    delay(10);
  }
  byte j=0;
  boolean go_on = true;
  while (go_on && j<80){
    if(event[j]=='A'){
      if(event[j+1]=='U'){
        if(event[j+2]=='T'){
          if(event[j+3]=='H'){
                    for (byte z=0;z<17;z++){
                      mac[z] = event[z+j+5];
                    } 
                    printf("%s\n",mac);
                    go_on = false;
          }else j++;
        }else j++;
      }else j++;
    }else j++;
  }
  delay(1000);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
}

char* getName(){
  
  char aux[23];
  sprintf(aux, "NAME %s\r\n", mac);
  char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  while (ans == "error"){
    char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  }
  
  delay(200);
  while ( Serial.available() > 0) Serial.read();   // Clean the input buffer
  return ans;
}

int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

        

Intel Galileo:

Code:
/*  
 *  Bluetooth module PRO
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Jorge Casanova, Luis MartĂ­n
 */
 
#include <LiquidCrystal.h>

#define BUTTON1 6
#define BUTTON2 7

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char error[] = "SYNTAX ERROR";
char event[80];
char mac[18];
char devName[16];
boolean ok;
boolean reject;

char aux_str[30];
char aux_str2[30];

void setup(){
  
  lcd.begin(16, 2);
  Serial.begin(115200);
  Serial1.begin(115200);
  lcd.print("BT Module");
  delay(2000);
  Serial.print("Setting up...");
  lcd.clear();
  lcd.print("Setting up...");
  while (sendATcommand("AT", "OK", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET BT PAIR *", "", error, 1000) == 2);
  delay(1000);
  while (sendATcommand("SET CONTROL CONFIG 0000 0000 0980", "SET CONTROL CONFIG 0000 0000 0980", error, 1000) == 2);
  delay(500);
  Serial.print("Done...");
  lcd.clear();
  lcd.print("Done...");
  delay(500);
  memset(event,0,40);
  memset(mac,0,18);
  memset(devName,0,16);
  delay(1000);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  lcd.clear();
  lcd.print("Waiting for ");
  lcd.setCursor(0,1);
  lcd.print("connection...");
}


void loop(){
  
  if(Serial1.available()>0){
    lcd.clear();
    lcd.print("Establishing a");
    lcd.setCursor(0,1);
    lcd.print("connection...");
    readEvent();
    strcpy(devName, getName());
    Serial.println(devName);
    lcd.clear();
    lcd.print("Conn incoming");
    lcd.setCursor(0,1);
    byte i =1;
    while(devName[i]!='"'){
      lcd.print(devName[i]);
      i++;
    }
    delay(500);    
    ok = digitalRead(BUTTON1);
    reject = digitalRead(BUTTON2);
    Serial.print(ok);
    Serial.print(reject);
    while(!ok && !reject){
      ok = digitalRead(BUTTON1);
      reject = digitalRead(BUTTON2);
    }
    if (ok){   
      sprintf(aux_str, "AUTH %s 0000\r\n", mac);
      sprintf(aux_str2, "PAIR %s\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      
      Serial.print("Connected to ");
      Serial.println(devName);
      lcd.clear();
      lcd.print("Connected to ");
      lcd.setCursor(0,1);
      i =1;
      while(devName[i]!='"'){
        lcd.print(devName[i]);
        i++;
      }
    }else if (reject){
      sprintf(aux_str, "AUTH %s?\r\n", mac);
      sprintf(aux_str2, "AUTH %s?\r\n", mac);
      while (sendATcommand(aux_str, aux_str2, error, 1000) == 2);
      while (sendATcommand(aux_str, "", error, 1000) == 2);
      
      Serial.print("Rejected conn to");
      Serial.println(devName);
      lcd.clear();
      lcd.print("Rejected conn to");
      lcd.setCursor(0,1);
      i =1;
      while(devName[i]!='"'){
        lcd.print(devName[i]);
        i++;
      }
    }
    ok = false;
    reject = false;
    delay(2500);
    while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
    lcd.clear();
    lcd.print("Waiting for ");
    lcd.setCursor(0,1);
    lcd.print("connection...");

  }
  memset(aux_str, '0', 30);  // Initialize the string
  memset(aux_str2, '0', 30);    // Initialize the string
  delay(1000);
}


int8_t sendATcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial1.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
      if (strstr(response, expected_answer) != NULL) {
        Serial1.println(expected_answer);
        while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
        answer = 1;
        Serial.println("Expected response");
      } else if (strstr(response, error_answer) != NULL) {
        answer = 2;
        while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
      }
    }
    // Waits for the asnwer with time out
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  return answer;
}

char* sendNAMEcommand(char* ATcommand, char* expected_answer, char* error_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[200];
  unsigned long previous;
  memset(response, '\0', sizeof(response));    // Initialize the string
  delay(100);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  if (ATcommand != NULL)  Serial1.println(ATcommand);  // Send the AT command
  x = 0;
  previous = millis();
  delay(1000);

    while (Serial1.available() != 0) {
      response[x] = Serial1.read();
      x++;
    }
      char* pch = strstr(response, expected_answer);
      if (pch != NULL) {
        return pch;
      } else if (strstr(response, error_answer) != NULL) {
        return "error";
        while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
      }
    // Waits for the asnwer with time out

  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  return NULL;
}

void readEvent(){
  
  byte i=0;
  while(Serial1.available()>0){
  
    event[i] = Serial1.read();
    i++;
    delay(10);
  }
  Serial.print(event);
  byte j=0;
  boolean go_on = true;
  while (go_on && j<80){
    if(event[j]=='A'){
      if(event[j+1]=='U'){
        if(event[j+2]=='T'){
          if(event[j+3]=='H'){
                    for (byte z=0;z<17;z++){
                      mac[z] = event[z+j+5];
                    } 
                    Serial.println(mac);
                    go_on = false;
          }else j++;
        }else j++;
      }else j++;
    }else j++;
  }
  delay(1000);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
}

char* getName(){
  
  char aux[23];
  sprintf(aux, "NAME %s\r\n", mac);
  char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  while (ans == "error"){
    char* ans = sendNAMEcommand(aux,"\"", "NAME ERROR", 1000);
  }
  
  delay(200);
  while ( Serial1.available() > 0) Serial1.read();   // Clean the input buffer
  return ans;
}
        

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: