2015年1月8日 星期四

BLE in Linux Zero : How to Set an iBeacon

The setting of Bluetooth low energy  makes persons confused.
In there  , I organize the bluetooth low energy in linux.


There are lots blog/discussing about this (like http://stackoverflow.com/questions/26853011/broadcasting-message-in-bluetooth-low-energy-mode )



First, your linux distribution should be installed bluez package.
Most have been done.

We would use two commandline tools:
hcitool and hciconfig.

hciconfig  could inquiry/set devices (bluetooth card in local computer)

like :

hiconfig
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 74:E5:43:96:87:A2  ACL MTU: 1021:8  SCO MTU: 64:1
    DOWN 
    RX bytes:196507 acl:2915 sco:0 events:9293 errors:0
    TX bytes:166857 acl:3037 sco:0 commands:6061 errors:0


the hci0 is the device symbol for bluez.
If you have more then one bluetooth device, that would be hci0, hci1...etc.

There is a "DOWN" flag to note this device being disable.
 We should use
sudo hciconfig hci0 up     : to be on working
sudo hciconfig hci psan  : to be scanable (by the other device, like android phone)

 sudo hciconfig hci0 up

 sudo hciconfig hci0 pscan

 hciconfig
hci0:    Type: BR/EDR  Bus: USB
    BD Address: 74:E5:43:96:87:A2  ACL MTU: 1021:8  SCO MTU: 64:1
    UP RUNNING PSCAN 
    RX bytes:197113 acl:2915 sco:0 events:9329 errors:0
    TX bytes:167553 acl:3037 sco:0 commands:6097 errors:0




and also, hciconfig hci0 noscan : stop to be scanable.
hciconfig hci noleadv  : stop ble advertising.


hcitool could set  what data to advertise and the advertise frequency.
(hcitool -i cmd ....)

The maximum data length be 31 bytes,  but availalbe is 29 byte only( 2 bytes for length and type ) 

hcitool should start as 0x08 and 0x0008, for
#OGF = Operation Group Field = Bluetooth Command Group = 0x08
#OCF = Operation Command Field = HCI_LE_Set_Advertising_Data = 0x0008

the ibeacon(apple's) format be :

 <IBEACONPROFIX> <UUID> <MAJOR> <MINOR> <POWER>

Here IBEACONPROFIX are number for identifying apple's format.

IBEACONPROFIX = 1E 02 01 1A 1A FF 4C 00 02 15

UUID: 16 byte data, you could use uuidgen this commandline tool to get this unique number.
 MAJOR, MINOR and POWER : 0000 to ffff
  
Totally, the ibeacon script be :

#!/bin/bash
# ref: http://www.theregister.co.uk/Print/2013/11/29/feature_diy_apple_ibeacons/
set -x
# inquiry local bluetooth device
#hcitool dev
export BLUETOOTH_DEVICE=hci0
#sudo hcitool -i hcix cmd <OGF> <OCF> <No. Significant Data Octets> <iBeacon Prefix> <UUID> <Major> <Minor> <Tx Power> <Placeholder Octets>

#OGF = Operation Group Field = Bluetooth Command Group = 0x08
#OCF = Operation Command Field = HCI_LE_Set_Advertising_Data = 0x0008
#No. Significant Data Octets (Max of 31) = 1E (Decimal 30)
#iBeacon Prefix (Always Fixed) = 02 01 1A 1A FF 4C 00 02 15

export OGF="0x08"
export OCF="0x0008"
export IBEACONPROFIX="1E 02 01 1A 1A FF 4C 00 02 15"

#uuidgen  could gerenate uuid
export UUID="4a 4e ce 60 7e b0 11 e4 b4 a9 08 00 20 0c 9a 66"
#export UUID="B9 40 7F 30 F5 F8 46 6E AF F9 25 55 6B 57 FE 6D"
#export UUID="76 E8 B4 E0 7E B5 11 E4 B4 A9 08 00 20 0C 9A 66"
export MAJOR="00 01"
export MINOR="00 00"
export POWER="C5 00"


# initialize device
sudo hciconfig $BLUETOOTH_DEVICE up
# disable advertising
sudo hciconfig $BLUETOOTH_DEVICE noleadv
# stop the dongle looking for other Bluetooth devices
sudo hciconfig $BLUETOOTH_DEVICE noscan

sudo hciconfig $BLUETOOTH_DEVICE pscan


sudo hciconfig $BLUETOOTH_DEVICE leadv

# advertise
sudo hcitool -i $BLUETOOTH_DEVICE cmd 0x08 0x0008 $IBEACONPROFIX $UUID $MAJOR $MINOR $POWER
sudo hcitool -i $BLUETOOTH_DEVICE cmd 0x08 0x0006 A0 00 A0 00 00 00 00 00 00 00 00 00 00 07 00
sudo hcitool -i $BLUETOOTH_DEVICE cmd 0x08 0x000a 01

echo "complete"

 hcitool cmd 0x08 0x0006 A0 00 A0 00 ...

 the first two bytes(A0 00) is "min interval"

And the 2nd two bytes (A0 A0 ) is "Max interval"

 A0 00, be 0x00A0(big endian), it is 160 in decimal.

The granularity of ble is 0.625ms. 

Here, min and max interval are 160* 625ms = 100ms both.


(ref : http://stackoverflow.com/questions/21124993/is-there-a-way-to-increase-ble-advertisement-frequency-in-bluez)

Then,  you could use android app to detect the ibeacon exist.


1 則留言:

  1. Thank you for this!
    In order to have "BLE only" mode I had to change the 1A flag to 16 like this:
    export IBEACONPROFIX="1E 02 01 16 1A FF 4C 00 02 15"

    回覆刪除