Skip to content

BLE Sim Lua script tutorial

This tutorial will outline how to write a basic lua script to control the BLE simulator. We will cover how to create a characteristic and update its value with the Update function.

Create a new device

dev1 = ble.createDevice ("lua-tutorial-dev-1")

Create the devices GATT attributes

We need to create a service to which will have a characteristic attribute. We also will need to create a descriptor to describe our characteristic.

srvc1 = ble.createService ("0000180d-0000-1000-8000-00805f9b34fb")
char1 = ble.createCharacteristic ("00002a38-0000-1000-8000-00805f9b34fb")
desc1 = ble.createDescriptor ("12345678-1234-5678-1234-56789abcdef2")

Assemble the GATT attributes

We then need to add our newly created objects to their respective parent attriubte.

dev1:addService (srvc1)
srvc1:addCharacteristic (char1)
char1:addDescriptor (desc1)

Register the device and enable it

We then need to register the device with the simulator. After we have registered it, we can then power it's controller and make it discoverable.

ble.registerDevice (dev1)

dev1:powered (true)
dev1:discoverable (true)

Initialise the characteristic's value

We initialise the characteristic's value to zero and also set the characteristic to create a notification when we update it's value.

number = 0
char1:setValue(number,DataType.UINT32)
char1:notifying (true)

Update the characteristic's value

We first need to define our Update function, which will be called by the simulator every 100ms:

function Update

end

We can then set the value of the characteristic each time the function is called and increment it by one:

function Update() 
  char1:setValue(number,DataType.UINT32)
  number = number + 1
end

Full code

--setup devices & their properties
dev1 = ble.createDevice ("lua-tutorial-dev-1")
srvc1 = ble.createService ("0000180d-0000-1000-8000-00805f9b34fb")
char1 = ble.createCharacteristic ("00002a38-0000-1000-8000-00805f9b34fb")
desc1 = ble.createDescriptor ("12345678-1234-5678-1234-56789abcdef2")

dev1:addService (srvc1)
srvc1:addCharacteristic (char1)
char1:addDescriptor (desc1)

ble.registerDevice (dev1)

dev1:powered (true)
dev1:discoverable (true)

number = 0
char1:setValue(number,DataType.UINT32)
char1:notifying (true)

function Update() 
  char1:setValue(number,DataType.UINT32)
  number = number + 1
end
Back to top