BLE en Raspberry Pi
Como hemos visto en una entrada anterior la Raspberry Pi 3 dispone de BLE.
Para comenzar a trabajar con BLE en Raspberry Pi debemos comprobar que está disponible, mediante el comando hciconfig:
pi@raspberrypi:~ $ hciconfig hci0: Type: Primary Bus: UART BD Address: XX:XX:XX:XX:XX:XX ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING RX bytes:822 acl:0 sco:0 events:57 errors:0 TX bytes:4231 acl:0 sco:0 commands:57 errors:0
En caso de que no esté arrancado hay que activarlo
sudo hciconfig hci0 up
Una utilidad que proporciona más información es blescan
pi@raspberrypi:~ $ sudo blescan Scanning for devices... Device (new): xx:xx:xx:xx:xx:xx (random), -91 dBm (not connectable) Manufacturer: <060001092000c7ce0663439d1e190d97c0a7827dfe0cb225c5cad35fee>
Después de comprobar que BLE está activado podemos arrancar el modo central u observador que escucha en las direcciones más usuales y nos devuelve las direcciones MAC (Se han cambiado el resultado de las MAC, son identificaciones únicas a nivel globalpor XX:XX:XX:XX:XX:XX):
pi@raspberrypi:~ $ sudo hcitool lescan LE Scan ... XX:XX:XX:XX:XX:XX (unknown) XX:XX:XX:XX:XX:XX (unknown) XX:XX:XX:XX:XX:XX (unknown) XX:XX:XX:XX:XX:XX Flex XX:XX:XX:XX:XX:XX (unknown) XX:XX:XX:XX:XX:XX MTLocomotive
Una vez descubiertos los dispositivos BLE que están emitiendo anuncios o «beacons» podemos con la utilidad gatttool establecer una conexión:
pi@raspberrypi:~ $ gatttool -I -b XX:XX:XX:XX:XX:XX [XX:XX:XX:XX:XX:XX][LE]> [XX:XX:XX:XX:XX:XX][LE]> connect Attempting to connect to XX:XX:XX:XX:XX:XX Error: connect error: Connection refused (111)
En este caso Flex se rechaza la conexión pues precisa una autentificación, los parámetros completos de gatttool son:
[XX:XX:XX:XX:XX:XX][LE]> help help Show this help exit Exit interactive mode quit Exit interactive mode connect [address [address type]] Connect to a remote device disconnect Disconnect from a remote device primary [UUID] Primary Service Discovery included [start hnd [end hnd]] Find Included Services characteristics [start hnd [end hnd [UUID]]] Characteristics Discovery char-desc [start hnd] [end hnd] Characteristics Descriptor Discovery char-read-hnd <handle> Characteristics Value/Descriptor Read by handle char-read-uuid <UUID> [start hnd] [end hnd] Characteristics Value/Descriptor Read by UUID char-write-req <handle> <new value> Characteristic Value Write (Write Request) char-write-cmd <handle> <new value> Characteristic Value Write (No response) sec-level [low | medium | high] Set security level. Default: low mtu <value> Exchange MTU for GATT/ATT
Otra utilidad es bluetoothctl:
pi@raspberrypi:~ $ bluetoothctl [NEW] Controller XX:XX:XX:XX:XX:XX raspberrypi [default] [NEW] Device XX:XX:XX:XX:XX:XX Flex [NEW] Device XX:XX:XX:XX:XX:XX MTLocomotive
Los comandos de bluetoolhctl los podemos listar con help:
[bluetooth]# help Available commands: list List available controllers show [ctrl] Controller information select Select default controller devices List available devices paired-devices List paired devices power <on/off> Set controller power pairable <on/off> Set controller pairable mode discoverable <on/off> Set controller discoverable mode agent <on/off/capability> Enable/disable agent with given capability default-agent Set agent as the default one advertise <on/off/type> Enable/disable advertising with given type set-advertise-uuids [uuid1 uuid2 ...] Set advertise uuids set-advertise-service [uuid][data=[xx xx ...] Set advertise service data set-advertise-manufacturer [id][data=[xx xx ...] Set advertise manufacturer data set-advertise-tx-power <on/off> Enable/disable TX power to be advertised set-scan-filter-uuids [uuid1 uuid2 ...] Set scan filter uuids set-scan-filter-rssi [rssi] Set scan filter rssi, and clears pathloss set-scan-filter-pathloss [pathloss] Set scan filter pathloss, and clears rssi set-scan-filter-transport [transport] Set scan filter transport set-scan-filter-clear Clears discovery filter. scan <on/off> Scan for devices info [dev] Device information pair [dev] Pair with device trust [dev] Trust device untrust [dev] Untrust device block [dev] Block device unblock [dev] Unblock device remove Remove device connect Connect device disconnect [dev] Disconnect device list-attributes [dev] List attributes set-alias Set device alias select-attribute Select attribute attribute-info [attribute] Select attribute read Read attribute value write <data=[xx xx ...]> Write attribute value notify <on/off> Notify attribute value register-profile Register profile to connect unregister-profile Unregister profile version Display version quit Quit program
Con esta utilidad podemos encender y apagar BLE:
[bluetooth]# power off Changing power off succeeded [CHG] Controller XX:XX:XX:XX:XX:XX Powered: no [CHG] Controller XX:XX:XX:XX:XX:XX Discovering: no [CHG] Controller XX:XX:XX:XX:XX:XX Class: 0x000000 [bluetooth]# power on [CHG] Controller XX:XX:XX:XX:XX:XX Class: 0x6c0000 Changing power on succeeded [CHG] Controller XX:XX:XX:XX:XX:XX Powered: yes
Como con gatttool se puede arrancar el modo scan con scan on o ver la versión
[bluetooth]# version Version 5.43 info XX:XX:XX:XX:XX:XX
Y obtener información de un dispositivo en concreto
[bluetooth]# info XX:XX:XX:XX:XX:XX Device XX:XX:XX:XX:XX:XX Name: MTLocomotive Alias: MTLocomotive Paired: no Trusted: no Blocked: no Connected: no LegacyPairing: no UUID: Vendor specific (XXXXXXXX-4a75-4c14-9ede-c8a5fb84f77b) [bluetooth]# info XX:XX:XX:XX:XX:XX Device XX:XX:XX:XX:XX:XX Name: Flex Alias: Flex Paired: no Trusted: no Blocked: no Connected: no LegacyPairing: no UUID: Vendor specific (XXXXXXXX-6e7d-4601-bda2-bffaa68956ba) ServiceData Key: 0000180a-0000-1000-8000-00805f9b34fb ServiceData Value: 0x07 ServiceData Value: 0x04
Parear
[bluetooth]# pair XX:XX:XX:XX:XX:XX Attempting to pair with XX:XX:XX:XX:XX:XX [CHG] Device XX:XX:XX:XX:XX:XX Connected: yes [CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001530-1212-efde-1523-785feabcd123 [CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001800-0000-1000-8000-00805f9b34fb [CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 00001801-0000-1000-8000-00805f9b34fb [CHG] Device XX:XX:XX:XX:XX:XX UUIDs: 98241523-4a75-4c14-9ede-c8a5fb84f77b [CHG] Device XX:XX:XX:XX:XX:XX ServicesResolved: yes [CHG] Device XX:XX:XX:XX:XX:XX Paired: yes [NEW] Primary Service /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0008 00001801-0000-1000-8000-00805f9b34fb Generic Attribute Profile [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0008/char0009 00002a05-0000-1000-8000-00805f9b34fb Service Changed [NEW] Descriptor /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0008/char0009/desc000b 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Primary Service /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service000c 98241523-4a75-4c14-9ede-c8a5fb84f77b Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service000c/char000d 98241525-4a75-4c14-9ede-c8a5fb84f77b Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service000c/char000f 98241524-4a75-4c14-9ede-c8a5fb84f77b Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service000c/char000f/desc0011 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Primary Service /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0012 00001530-1212-efde-1523-785feabcd123 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0012/char0013 00001532-1212-efde-1523-785feabcd123 Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0012/char0015 00001531-1212-efde-1523-785feabcd123 Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0012/char0015/desc0017 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_XX_XX_XX_XX_XX_XX/service0012/char0018 00001534-1212-efde-1523-785feabcd123 Vendor specific Pairing successful [CHG] Device XX:XX:XX:XX:XX:XX Trusted: yes
En una próxima entrada veremos como programar funciones BLE en Phyton
Comentarios
Publicar un comentario