Transmitting Data Over the Bluetooth Interface

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:

  1. Determine what Bluetooth® devices are accessible from your computer.

  2. View the device list using the RemoteNames property.

  3. 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.


    The Instrument Control Toolbox™ displays the device information.

  4. Create a Bluetooth object called bt using channel 1 of the NXT device.

    bt = Bluetooth('C3PO', 1);
  5. Connect to the device.

  6. 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.

  7. 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.

  8. Clean up by deleting and clearing the object.



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.

Other Functionality

The following functions can be used with the Bluetooth object.

binblockwriteWrite binblock data to instrument
fgetlRead line of text from instrument and discard terminator
flushinputRemove data from input buffer
fopenConnect interface object to instrument
freadRead binary data from instrument
fwriteWrite binary data to instrument
methodsClass method names and descriptions
readasyncRead data asynchronously from instrument
scanstrRead data from instrument, format as text, and parse
binblockreadRead binblock data from instrument
fcloseDisconnect interface object from instrument
fgetsRead line of text from instrument and include terminator
flushoutputRemove data from output buffer
fprintfWrite text to instrument
fscanfRead data from instrument, and format as text
queryWrite text to instrument, and read data from instrument
recordRecord data and event information to file
stopasyncStop 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.