Main Content

read

Read data from Bluetooth device

Description

example

data = read(device,count) reads the number of values specified by count from the Bluetooth® connection device and returns the data as a row or column vector of doubles or text. The function suspends MATLAB® execution until the specified number of values are read or a timeout occurs.

example

data = read(device,count,datatype) reads count number of values in the form specified by datatype and returns the data. The datatype argument is a character vector of a standard MATLAB data type. For all numeric datatype types, data is a row vector of double values. For the text type datatype values of "char" or "string", data is of the specified type.

Examples

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

Write the values 1:10 to the device.

write(device,1:10)

Since the device is configured as a loopback device, the data you write to the device is returned to MATLAB. Read all the data.

read(device,10)
ans = 1×10

     1     2     3     4     5     6     7     8     9    10

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

Write the string "helloworld" to the device.

write(device,"helloworld","string")

Since the device is configured as a loopback device, the data written to the device is returned to MATLAB. Read the first five values of data as a string.

read(device,5,"string")
ans = 

    "hello"

Use the read command again to read the next five values of data.

read(device,5,"string")
ans = 

    "world"

Input Arguments

collapse all

Bluetooth device connection, specified as a bluetooth object.

Example: data = read(device,5) reads data from the Bluetooth connection device.

Number of values to read, specified as a positive integer value. If count is greater than the NumBytesAvailable property of device, the function suspends MATLAB execution and waits until the specified amount of data is read or a timeout occurs.

Example: read(device,2) reads two values of uint8 data.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size and format of each value, specified as a character vector or string. datatype determines the number of bytes to read for each value and the interpretation of those bytes as a MATLAB data type.

Example: read(device,1,"uint16") reads one value of uint16 data. Each uint16 value is two bytes.

Data Types: char | string

See Also

Functions

Introduced in R2020b