Bluetooth module description
Module provides an access to a bluetooth functionality (connect/disconnect devices, set values, get value, etc.).
Bluetooth events
The Bluetooth addon notifies a script subscribed by about state of the Bluetooth world.
Structure of an event is table.
Type of events:
discovery_started — gets sent when bluetooth devices discovery process started
fields | type | description |
---|---|---|
event | string | type of the event |
discovery_stopped — gets sent when bluetooth devices discovery process stopped
fields | type | description |
---|---|---|
event | string | type of the event |
device_discovered — gets sent if bluetooth device discovered
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
device_removed — gets sent when bluetooth device removed
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
device_connected — gets sent when bluetooth device connected
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
device_disconnected — gets sent when bluetooth device disconnected
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
device_paired — gets sent when bluetooth device paired
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
device_unpaired — gets sent when bluetooth device unpaired
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
characteristic_updated — gets sent when bluetooth device characteristic value updated
fields | type | description |
---|---|---|
event | string | type of the event |
deviceAddress | string | bluetooth device mac address |
uuid | string | GATT characteristic UUID |
value | array of bytes | new characteristic value |
Example: (event_handling.lua)
local params = ...
if params.event == "device_connected" then
print( "we got a device_connected event for a " .. params.deviceAddress .. " device" )
end
Bluetooth API
subscribe(script_name[, events_filter])
- Subscribe for Bluetooth events, optionally specifying event filter rules. After subscribing the script will be launched for each event happens on a Bluetooth addon and information about this event will be passed as a parameter.
- params:
set_subscription_filters(subscriber_id, filter_rules)
- Change events filter rules for a subscription . (set_subscription_filters)
- params:
params | type | description |
---|---|---|
subscriber_id | string | subscriber id |
filter_rules | table | events filter rules |
- return: none.
local bluetooth = require "bluetooth"
bluetooth.subscribe("HUB:bluetooth/scripts/events_handling")
bluetooth.set_subscription_filters("HUB:bluetooth/scripts/events_handling", { { event = "node_added" } } )
start_notify
- Start notifications for GATT characteristic.
- params:
stop_notify
- Stop notifications for GATT characteristic.
- params:
start_discovery
- Start bluetooth discovery process.
- params: none.
- return: none.
local bluetooth = require "bluetooth"
bluetooth.start_discovery()
stop_discovery
- Stop bluetooth discovery process.
- return: none.
local bluetooth = require "bluetooth"
bluetooth.stop_discovery()
write_characteristic
- Write new device characteristic value
- params:
params | type | description |
---|---|---|
device address | string | bluetooth device mac address |
characteristic uuid | string | characteristic uuid |
characteristic value | array of bytes | new characteristic value |
- return: none.
local bluetooth = require "bluetooth"
bluetooth.write_characteristic("04:A3:16:9E:FB:0A", "0000ffe9-0000-1000-8000-00805f9b34fb", {0xFF, 0xF0, 0x0F})
read_characteristic
- Read device characteristic value
- params:
params | type | description |
---|---|---|
device address | string | bluetooth device mac address |
characteristic uuid | string | characteristic uuid |
- return: none.
local bluetooth = require "bluetooth"
bluetooth.read_characteristic("04:A3:16:9E:FB:0A", "0000ffe9-0000-1000-8000-00805f9b34fb")