Main Content


Write line of ASCII data to Bluetooth device

Since R2020b



writeline(device,data) writes the ASCII text data followed by the terminator to the specified Bluetooth® device. The function suspends MATLAB® execution until the data and terminator are written.


collapse all

Create a connection to a nearby Bluetooth device. In this example, the HC-06 Bluetooth module is configured as a loopback device.

device = bluetooth("HC-06")
device = 
  bluetooth with properties:

                 Name: "HC-06"
              Address: "98D331FB3B77"
              Channel: 1
    NumBytesAvailable: 0
      NumBytesWritten: 0

  Show all properties

Check the default ASCII terminator.

ans = 


Set the terminator to "CR" and write a string of ASCII data. The writeline function automatically appends the terminator to the data.


Write another string of ASCII data with the terminator automatically appended.


Since the device is configured as a loopback device, the data you write to the device is returned to MATLAB. Read a string of ASCII data. The readline function returns data until it reaches a terminator.

ans = 


Read a string of ASCII data again to return the second string that you wrote.

ans = 


Clear the Bluetooth connection.

clear device

Input Arguments

collapse all

Bluetooth device connection, specified as a bluetooth object.

Example: data = writeline(device,data) writes ASCII data to the Bluetooth connection device.

ASCII data to write, specified as a character vector or string scalar of text.

Example: writeline(device,"helloworld") writes the ASCII data "helloworld".

Data Types: string

Version History

Introduced in R2020b