Using Bluetooth Low Energy (BLE) /iBeacon on Spark to read out BLE/iBeacon tags

@thebaldgeek, thanks for the insight!

About the SensorTag, I think it is for just Development purpose and not for the production purpose, may be that’s why it goes not sleep mode. I didn’t use Node-RED and I am new to that, is it easy to connect to connect and may be do some debugging the BLE device?, I think I have to try this, seems to to be good.

About the iBeacon, you can turn BLE Mini into and iBeacon be updating the firmware RedBearLab provides, and RFDuino also.

Did you manage to get the iBeacon detected using :spark:/Arduino, I am also very much interest in this any insight will be helpful.

I was searching for a Raspberry Pi iBeacon scanner as well previously and i found this Raspberry Pi iBeacon development kit which might interest you :slight_smile: It is able to scan iBeacons as well.

About RFID, yes the range is much less indeed. And i need at least 1.5m for what i have in mind.. Not a lot of (cheap) RFID scanners can achieve this i'm afraid..

Sorry to be so persistent and stubborn @thebaldgeek, I just don't wanna give up hope :stuck_out_tongue_closed_eyes:

But you said the receiver is the problem. And yes ofcourse you're right.
In your previous answer you showed me a typical answer:
https://redbearlab.zendesk.com/entries/68797873-BLE-Shield-as-a-receiver-for-iBeacon-RedBear-Beacon-B1

But the receiver you're referring to here is the BLE Shield, which only supports the peripheral role.
Does this answer still apply to the BLE Mini which can serve as a peripheral role or a central role?

The BLE Mini is for making BlueTooth links. I dont see anything in their github that gives me confidence that it can be put into some sort of iBeacon scan mode to listen and then transmit over its serial port any UUID's that it hears.

I do understand what you mean here. But i noticed that someone was able to make a small "scanner" with the BLE mini:
https://redbearlab.zendesk.com/entries/76312427-BLE-Mini-CENTRAL-It-cannot-scan-properly-SOLVED-

Also see:
https://redbearlab.zendesk.com/entries/50793156-FInding-Devices-Using-BLE-Mini

But you have to use the beta HCI Library and it says that it can only scan up to 8 BLE devices/tags :confused:
And there's a issue about this going on here:
https://redbearlab.zendesk.com/entries/77169088-BLE-Mini-CENTRAL-How-to-Increase-Maximum-Number-of-Discoverable-Devices-SOLVED-

Again, the iBeacon source is not the challenge, its the SparkCore/Arduino compatible receiver that is the challenge.

Hope i'm not saying anything stupid here.. But if the BLE Mini does what is described in the links above, does this solve our issues as the BLE Mini is the receiver which is our challenge like you said?

If i'm wrong again and you're right and the Spark Core won't be able to scan for BLE/iBeacon tags (please someone read this and say we're wrong).. then I'm surely going for the Raspberry option like you..

Sorry if this feels like a debate of some sort, but since you said you would love to be wrong, and so do I, i'm hoping I can make us both happy :thumbsup:

1 Like

@jnijstad, @thebaldgeek, BLE Mini with HCI Library and Firmware can be used to scan BLE devices. As in case of iBeacons, it is a BLE device and why can’t we use HCI commands to detect iBeacons?, I didn’t try this but I think I have to try HCI commands with Arduino first, may be this weekend.

1 Like

Yep. It looks like these Redbearlabs guys have got the device in the BLE Mini.
30 bucks and a SparkCore and you will be listing UUID’s and RSSI’s from your iBeacons in ‘no’ time.

Cool.

(Sorry for wasting everyone’s time - I deleted my past posts since they did not add anything of value to the thread - they were just noise).

I am able to find iBeacon using BLE Mini and :spark:. The process is first send Device Discovery HCI copmmand, wait for the GAP_DeviceInformation. The device information contains iBeacon UUID, Major, Minor and Measured Power. Following Arduino code prints the details. I am working on a :spark: sample, will publish later, may be this weekend.

#include <stdarg.h>

#define GAP_PROFILE_CENTRAL           0x08
#define KEYLEN                        16
#define B_ADDR_LEN                    6 

#define BUILD_UINT16(loByte, hiByte) \
          ((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))

static uint8_t gapCentralRoleTaskId = 0;
static uint8_t  gapCentralRoleIRK[KEYLEN] = {0};
static uint8_t  gapCentralRoleSRK[KEYLEN] = {0};
static uint32_t gapCentralRoleSignCounter = 1;
static uint8_t  gapCentralRoleBdAddr[B_ADDR_LEN];
static uint8_t  gapCentralRoleMaxScanRes = 5;

void p(char *fmt, ... ){
  char tmp[128]; // resulting string limited to 128 chars
  va_list args;
  va_start (args, fmt );
  vsnprintf(tmp, 128, fmt, args);
  va_end (args);
  Serial.print(tmp);
}

void setup(){
  Serial.begin(57600);
  hci_init();
}

void loop(){ 
  if(Serial.available()){
    ble_event_process();
  }
}

byte ble_event_process(){
  uint8_t type, event_code, data_len, status1;
  uint16_t event;
  uint8_t buf[64];
  
  type = Serial.read();
  delay(100);
  event_code = Serial.read();
  data_len = Serial.read();
  
  for (int i = 0; i < data_len; i++)
    buf[i] = Serial.read();
   
  event = BUILD_UINT16(buf[0], buf[1]);
  status1 = buf[2];
  
  switch(event){
    case 0x600:{
      hci_start_discovery();
      
      break;
    }
    case 0x060D:{
      int rssi = buf[11] - 255;
      
      p("\r\n");
      p("RSSI: %d, %d\r\n", buf[11], rssi);
      p("iBeacon UUID: ");
      
      for(int i=22; i<38; i++){
        if(i<37)
          p("%02X:", buf[i]);
        else
          p("%02X", buf[i]);
      }
      
      p("\r\n");
      p("iBeaconMajor: %d\r\n", BUILD_UINT16(buf[39], buf[38]));
      p("Minor: %d\r\n", BUILD_UINT16(buf[41], buf[40]));
      p("Measured Power: %X", buf[42]);
      p("\r\n");
      
      break;
    }
    case 0x601:{
      hci_start_discovery();
      
      break;
    }
  }
}

int hci_init(){
  return GAP_DeviceInit( gapCentralRoleTaskId, GAP_PROFILE_CENTRAL,
                         gapCentralRoleMaxScanRes, gapCentralRoleIRK,
                         gapCentralRoleSRK, &gapCentralRoleSignCounter );
}

int hci_start_discovery(){
  return GAP_DeviceDiscoveryRequest();
}

int GAP_DeviceInit( uint8_t taskID, uint8_t profileRole, uint8_t maxScanResponses, uint8_t *pIRK, uint8_t *pSRK, uint32_t *pSignCounter )
{
  uint8_t buf[42];
  uint8_t len = 0;
    
  buf[len++] = 0x01;                  // -Type    : 0x01 (Command)
  buf[len++] = 0x00;                  // -Opcode  : 0xFE00 (GAP_DeviceInit)
  buf[len++] = 0xFE;
  
  buf[len++] = 0x26;                  // -Data Length
  buf[len++] = profileRole;           //  Profile Role
  buf[len++] = maxScanResponses;      //  MaxScanRsps
  memcpy(&buf[len], pIRK, 16);        //  IRK
  len += 16;
  memcpy(&buf[len], pSRK, 16);        //  SRK
  len += 16;
  memcpy(&buf[len], pSignCounter, 4); //  SignCounter
  len += 4;

  Serial.write(buf, len);
  
  return 1;
}

int GAP_DeviceDiscoveryRequest()
{
  uint8_t buf[20];
  uint8_t len = 0;
    
  buf[len++] = 0x01;                 // -Type    : 0x01 (Command)
  buf[len++] = 0x04;                 // -Opcode  : 0xFE04 (GAP_DeviceDiscoveryRequest)
  buf[len++] = 0xFE;
    
  buf[len++] = 0x03;                 // -Data Length
  buf[len++] = 0x03;                 //  Mode
  buf[len++] = 0x01;                 //  ActiveScan
  buf[len++] = 0x00;                 //  WhiteList
  
  Serial.write(buf, len);
  
  return 1;
}

void readChar(int len){
  int i=0;
  
  while(i<len){
    char ch = Serial.read();
    
    ++i;
  }
}
4 Likes

Here is the :spark: port of the above code.

1 Like

Is there such a thing as a USB key fob BLE beacon ? Something that can be recharged by plugging into a standard USB port ?

I’m not looking for any on device programmability - simply a beacon announcement (configurable interval maybe).

Thinking of putting spark cores in a power socket package in each room and transmitting staff locations back to a central service.

@defsdoor, take a look at this and see it matches your requirements

I’m after a key fob beacon. The plan is to put spark based receivers all around the building and detect who is in which room. On a key fob I can also use it for who enters and leaves the building as staff will take them home with them. Looking at some of the advertised lifetime of these on cr2032 batteries I’d forgoe the rechargeable aspect.