Main Content

configureCallback

Set callback function and trigger condition for communication with Bluetooth device

Description

example

configureCallback(device,"terminator",callbackFcn) sets the callback function callbackFcn to trigger whenever a terminator is available to be read from the specified Bluetooth® device. The syntax sets the BytesAvailableFcnMode property of device to "terminator" and the BytesAvailableFcn property to callbackFcn.

Set the terminator character using configureTerminator.

example

configureCallback(device,"byte",count,callbackFcn) sets the callback function callbackFcn to trigger whenever a new count number of bytes are available to be read. The syntax sets the BytesAvailableFcnMode property of device to "byte", the BytesAvailableFcnCount property to count, and the BytesAvailableFcn property to callbackFcn.

example

configureCallback(device,"off") turns off callbacks. The syntax sets the BytesAvailableFcnMode property of device to "off".

Examples

collapse all

Create a connection to a nearby Bluetooth device.

device = bluetooth("TestDevice")
device = 
  bluetooth with properties:

                 Name: "TestDevice"
              Address: "BC275C50533C"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

Set the callback to trigger when a terminator is available to be read.

configureCallback(device,"terminator",@callbackFcn)

View the properties to confirm the change.

device.BytesAvailableFcnMode
device.BytesAvailableFcn
ans = 

    "terminator"


ans =

  function_handle with value:

    @callbackFcn

Turn the callback off.

configureCallback(device,"off")

Verify that the callback is off.

device.BytesAvailableFcnMode
ans = 

    "off"

Create a connection to a nearby Bluetooth device.

device = bluetooth("TestDevice")
device = 
  bluetooth with properties:

                 Name: "TestDevice"
              Address: "BC275C50533C"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

Set the callback to trigger when 50 bytes of data are available to be read.

configureCallback(device,"byte",50,@callbackFcn)

View the properties to confirm the change.

device.BytesAvailableFcnMode
device.BytesAvailableFcnCount
device.BytesAvailableFcn
ans = 

    "byte"


ans =

    50


ans =

  function_handle with value:

    @callbackFcn

Turn the callback off.

configureCallback(device,"off")

Verify that the callback is off.

device.BytesAvailableFcnMode
ans = 

    "off"

Input Arguments

collapse all

Bluetooth device connection, specified as a bluetooth object.

Example: configureCallback(device,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read from the Bluetooth connection device.

Number of bytes of available data to trigger the callback, specified as a positive integer value. Set the BytesAvailableFcnCount property using this argument.

Example: configureCallback(device,"byte",128,@callbackFcn) sets the callbackFcn callback to trigger each time 128 bytes of new data are available to be read.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Callback function to run when trigger condition is met, specified as a function handle. The function handle can be a named function handle or an anonymous function with input arguments. Set the BytesAvailableFcn property using this argument.

Example: configureCallback(device,"terminator",@callbackFcn) sets the callbackFcn callback to trigger when a terminator is available to be read.

Data Types: function_handle

See Also

Functions

Introduced in R2020b