You can read and write both text data (ASCII based) and binary
data. For text data, use the
For binary data, use the
This example uses the LEGO Mindstorm NXT robot with a
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.
View the device list using the
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
The Instrument Control Toolbox™ displays the device information.
Create a Bluetooth object called
1 of the NXT device.
bt = Bluetooth('C3PO', 1);
Connect to the device.
Send a message to the remote device using the
In this example, specific characters are sent to the device
that this particular device (the NXT robot
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
You can see that
which are the four characters sent in
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
C3PO, which is the
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
Clean up by deleting and clearing the object.
This example uses the
To read and write text-based data, use the
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.
The following functions can be used with the Bluetooth object.
|Write binblock data to instrument|
|Read line of text from instrument and discard terminator|
|Remove data from input buffer|
|Connect interface object to instrument|
|Read binary data from instrument|
|Write binary data to instrument|
|Class method names and descriptions|
|Read data asynchronously from instrument|
|Read data from instrument, format as text, and parse|
|Read binblock data from instrument|
|Disconnect interface object from instrument|
|Read line of text from instrument and include terminator|
|Remove data from output buffer|
|Write text to instrument|
|Read data from instrument, and format as text|
|Write text to instrument, and read data from instrument|
|Record data and event information to file|
|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.