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.
View the device list using the RemoteNames property.
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.
Create a Bluetooth object called bt using channel 1 of the NXT device.
bt = Bluetooth('C3PO', 1);
Connect to the device.
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.
Note: 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.
|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.