You can read and write both text data (ASCII based) and binary
data. For text data, use the fscanf and fprintf functions.
For binary data, use the fread and fwrite functions.
This example uses the LEGO Mindstorm NXT robot with a RemoteName of C3PO that
you discovered in Viewing Bluetooth Device Properties. See that section for
more details on device discovery and viewing properties.
To communicate with the NXT device:
Determine what Bluetooth® devices are accessible from your computer.
instrhwinfo('Bluetooth')View the device list using the RemoteNames property.
ans.RemoteNames
In this case, C3PO is the remote
name of the NXT robot and is shown in the output. Display the information
about this device using the Bluetooth interface
and the RemoteName property.
instrhwinfo('Bluetooth','C3PO')The Instrument Control Toolbox™ displays the device information.

Create a Bluetooth object called bt using
channel 1 of the NXT device.
bt = Bluetooth('C3PO', 1);Connect to the device.
fopen(bt)
Send a message to the remote device using the fwrite function.
In this example, specific characters are sent to the device
that this particular device (the NXT robot C3PO)
understands. You can write to the device, then query the object, as
shown here, to see that the values were sent.

Read data from the remote device using the fread function.
You can see that ValuesSent is 4,
which are the four characters sent in fwrite (2,0,1,155).
This also shows that 35 bytes are available. So
you can then use the fread function and give it
35 bytes to read the characters from the remote device.
![]()

The device returns the characters shown here. The returned characters
are C3PO, which is the RemoteName of
the device. That was a reply to the instructions that were sent to
it. See the documentation for your device for this type of device-specific
communication information.
Clean up by deleting and clearing the object.
fclose(bt);
clear('bt');This example uses the fread and fwrite functions.
To read and write text-based data, use the fscanf and fprintf functions.
You can do asynchronous reading and writing of data using the Bluetooth interface. This is similar to the same operations using the Instrument Control Toolbox Serial interface. For more information, see Asynchronous Write and Read Operations.
Other Functionality
The following functions can be used with the Bluetooth object.
| Function | Purpose |
|---|---|
binblockwrite | Write binblock data to instrument |
fgetl | Read line of text from instrument and discard terminator |
flushinput | Remove data from input buffer |
fopen | Connect interface object to instrument |
fread | Read binary data from instrument |
fwrite | Write binary data to instrument |
methods | Class method names and descriptions |
readasync | Read data asynchronously from instrument |
scanstr | Read data from instrument, format as text, and parse |
binblockread | Read binblock data from instrument |
fclose | Disconnect interface object from instrument |
fgets | Read line of text from instrument and include terminator |
flushoutput | Remove data from output buffer |
fprintf | Write text to instrument |
fscanf | Read data from instrument, and format as text |
query | Write text to instrument, and read data from instrument |
record | Record data and event information to file |
stopasync | Stop asynchronous read and write operations |
For more information about these functions, see the functions documentation.
To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB® command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.