SimpleBLE.jl
SimpleBLE is a cross-platform bluetooth low energy (BLE) library, designed for simplicity and ease of use.
Simple example
# Get an adapter that will handle requests
adapter = get_adapter(0)
# Find a peripheral using adapter
peripheral = find_peripheral(adapter) do id
occursin("peripheral name", id)
end
id = identifier(peripheral)
connect(peripheral) do
SERVICE = "de8d0b82-c7cd-0f33-d15d-c38e1f26673f"
CHARACTER = "6706b606-0c12-3976-ddba-909377d43dc5"
notify(peripheral, SERVICE, CHARACTER) do data
# make sure not to pass the data handle outside the callback
# it is not valid data once this callback is over, if you need to keep it, copy it
println("$id received data:")
println(String(data))
end
println("Press enter to stop listening")
readline()
unsubscribe(peripheral, SERVICE, CHARACTER)
endMost common functions
SimpleBLE.get_adapter — Function
get_adapter(i)Acquire a handle to an adapter. i is an index starting at 0. You can check the number of adapters with adapters_get_count.
SimpleBLE.find_peripheral — Function
find_peripheral(matchfunc, adapter)
find_peripheral(adapter) do identifier::String
# Stuff
return output::Bool
endConvenience function for finding a peripheral. identifier is the name of the peripheral, it can be an empty string.
SimpleBLE.connect — Function
connect(peripheral)Connect to a peripheral. Generally you should disconnect when you're done with it
connect(callback, peripheral)
connect(peripheral) do
# Stuff
endConvenience function for automatically disconnecting from a peripheral once you're done with it
SimpleBLE.set_callback_on_scan_found — Function
set_callback_on_scan_found(callback, adapter)
set_callback_on_scan_found(adapter) do peripheral
# Stuff
endSet a callback that is called when an adapter finds a peripheral.
See also scan_start, scan_stop, scan_for
Base.notify — Function
notify(callback,
peripheral::Peripheral,
service::Union{AbstractString, SBLEUUID, SBLESERVICE},
characteristic::Union{AbstractString, SBLEUUID, SBLECHARACTERISTIC}
)
notify(
peripheral::Peripheral,
service::Union{AbstractString, SBLEUUID, SBLESERVICE},
characteristic::Union{AbstractString, SBLEUUID, SBLECHARACTERISTIC}
) do data
# Stuff
endSet a callback that is called when data is received from a Characteristic. notify is generally faster than indicate because it does not need to acknowledge that it received the data.
See also indicate
SimpleBLE.write_request — Function
write_request(peripheral,
service::Union{AbstractString, SBLEUUID, SBLESERVICE},
characteristic::Union{AbstractString, SBLEUUID, SBLECHARACTERISTIC},
data <: Union{AbstractString, AbstractArray}
)Write a request to a characteristic.
See also write_command
SimpleBLE.peripheral_read — Function
peripheral_read(peripheral,
service::Union{AbstractString, SBLEUUID, SBLESERVICE},
characteristic::Union{AbstractString, SBLEUUID, SBLECHARACTERISTIC},
)Read data from a characteristic.
service and characteristic can be AbstractStrings, SBLEUUIDs, or SBLESERVICE and SBLECHARACTERISTIC acquired by services.