Skip to content

Instantly share code, notes, and snippets.

@crespum
Created June 30, 2016 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crespum/d301b1bb3efde9757cc045f49ad472d3 to your computer and use it in GitHub Desktop.
Save crespum/d301b1bb3efde9757cc045f49ad472d3 to your computer and use it in GitHub Desktop.
Creates an iBeacon in a Linux environment, including Raspbian for Raspberry Pi.
#!/bin/bash
# Script to create an iBeacon for Linux systems (including Raspbian for Raspberry Pi). Based on
# RadiusNetwork script for AltBeacons (https://github.com/RadiusNetworks/altbeacon-reference/blob/master/altbeacon_transmit)
#
# System Requirements: Linux, BlueZ (Official Linux Bluetooth protocol stack), Bluetooth 4.0 Adapter
# Advertising Data Flags (not part of iBeacon standard)
AD_Length_Flags="02" # Length of Flags AD structure in bytes
AD_Type_Flags="01" # Type of AD structure as Flags type
AD_Data_Flags="1A" # Flags data LE General Discoverable
# iBeacon Advertisement
AD_Length="1A" # Length of Data AD structure in bytes (not including this field)
AD_Type="FF" # Manufacturer Specific Data
AD_Company_ID="4C 00" # Company identifier (little endian). Apple uses 0x004C
AD_iBeacon_Type="02" # Type of AD structure as Manufacturer Specific Data
AD_iBeacon_Length="15" # iBeacon advertisement length
AD_Data_UUID="E2 0A 40 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61" # 16-byte UUID
AD_Data_Major="00 00" # Beacon Group as 2-byte value
AD_Data_Minor="00 00" # Beacon Unit as 2-byte value
AD_Data_Reference_RSSI="C5" # Signed value representing the average received signal strength at 1m from the advertiser
# Set up advertising data flags
AD_Flags=`echo "$AD_Length_Flags $AD_Type_Flags $AD_Data_Flags"`
# Set up iBeacon advertisement
iBeacon_Advertisement=`echo "$AD_Length $AD_Type $AD_Company_ID $AD_iBeacon_Type $AD_iBeacon_Length"`
iBeacon_Advertisement=`echo "$iBeacon_Advertisement $AD_Data_UUID $AD_Data_Major $AD_Data_Minor $AD_Data_Reference_RSSI"`
echo "Transmitting iBeacon profile with the following identifiers:"
echo "UUID: $AD_Data_UUID"
echo "Major: $AD_Data_Major"
echo "Minor: $AD_Data_Minor"
echo ""
echo "iBeacon Advertisement: $iBeacon_Advertisement"
echo ""
# Set Bluetooth device ID
BLUETOOTH_DEVICE="hci0"
# Command Bluetooth device up
sudo hciconfig $BLUETOOTH_DEVICE up
# Start LE advertising (non-connectable)
sudo hciconfig $BLUETOOTH_DEVICE leadv 3
# Stop Scanning
sudo hciconfig $BLUETOOTH_DEVICE noscan
# HCI_LE_Set_Advertising_Data command
# Command parameters: Advertising_Data_Length (1D), Advertising_Data
sudo hcitool -i $BLUETOOTH_DEVICE cmd 0x08 0x0008 1D $AD_Flags $iBeacon_Advertisement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment