bluetooth
InterfaceA new set of MATLAB® functions and properties provides support for communicating with Bluetooth® devices. The Bluetooth
function, its object functions, and
its properties are not recommended. Use the bluetooth
(case-sensitive) interface instead.
Bluetooth Interface | bluetooth Interface | Example |
---|---|---|
instrhwinfo | bluetoothlist | Discover Bluetooth Devices |
Bluetooth and fopen | bluetooth | Connect to Bluetooth Device |
fwrite and fread | write and
read | Write and Read |
fprintf | writeline | Send Command |
Write and Read Back Data | ||
fscanf , fgetl , and
fgets | readline | Read Terminated String |
Write and Read Back Data | ||
Read and Parse String Data | ||
flushinput and flushoutput | flush | Flush Data from Memory |
Terminator | configureTerminator | Set Terminator |
BytesAvailableFcnCount ,
BytesAvailableFcnMode , BytesAvailableFcn , and
BytesAvailable | configureCallback | Set Up Callback Function |
Bluetooth Properties | bluetooth Properties |
This example shows how to discover Bluetooth devices using the recommended functionality.
Functionality | Use This Instead |
---|---|
instrhwinfo('Bluetooth') |
bluetoothlist |
For more information, see bluetoothlist
.
These examples show how to connect to a Bluetooth device and disconnect from it using the recommended functionality.
Functionality | Use This Instead |
---|---|
b = Bluetooth('NXT',3)
fopen(b) |
b = bluetooth("NXT",3) |
fclose(b)
delete(b)
clear b |
clear b |
For more information, see bluetooth
.
These examples use a loopback device to show how to perform a binary write and read, write nonterminated string data, and read fixed-length string data using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object
fwrite(b,1:5)
data = fread(b,5) data = 1 2 3 4 5 |
% b is a bluetooth object
write(b,1:5)
data = read(b,5) data = 1 2 3 4 5 |
% b is a Bluetooth object command = "start"; fwrite(b,command,"char") |
% b is a bluetooth object command = "start"; write(b,command,"char") |
% b is a bluetooth object command = "start"; write(b,command,"string") | |
% b is a Bluetooth object length = 5; data = fread(b,length,"char") resp = 104 101 108 108 111 data = char(data)' resp = 'hello' |
% b is a bluetooth object length = 5; data = read(b,length,"string") data = "hello" |
These examples show how to perform a terminated string read using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object data = fscanf(b,"%e") data = 11.9000 For
the format specifier |
% b is a bluetooth object
data = readline(b) data = "11.9" data = sscanf(data,"%e") data = 11.9000 |
% b is a Bluetooth object
data = fgetl(b) data = 'hello'
| % b is a bluetooth object
data = readline(b) data = "hello"
|
% b is a Bluetooth object
data = fgets(b) data = 'hello '
|
For more information, see readline
.
This example shows how to write terminated string data using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object b.Terminator = "CR/LF" channel = 1; fprintf(b,"id is %d",channel); | % b is a bluetooth object configureTerminator(b,"CR/LF") channel = 1; str = sprintf("id is %d",channel); writeline(b,str)
|
For more information, see configureTerminator
or writeline
.
This example shows how to write text and read back data using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object data = query(b,'ctrlcmd') data = 'success' |
% b is a bluetooth object writeline(b,"ctrlcmd") data = readline(b) data = "success" |
This example shows how to read and parse string data using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object data = scanstr(b,';') data = 3×1 cell array {'a'} {'b'} {'c'} |
% b is a bluetooth object
data = readline(b) data = "a;b;c" data = strsplit(data,";") data = 1×3 string array "a" "b" "c" |
For more information, see readline
.
This example shows how to flush data from the buffer using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object
flushinput(b)
|
% b is a bluetooth object flush(b,"input") |
% b is a Bluetooth object
flushoutput(b)
|
% b is a bluetooth object flush(b,"output") |
% b is a Bluetooth object
flushinput(b)
flushoutput(b)
|
% b is a bluetooth object
flush(b) |
For more information, see flush
.
This example shows how to set the terminator using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object b.Terminator = "CR/LF"; |
% b is a bluetooth object configureTerminator(b,"CR/LF") |
% b is a Bluetooth object b.Terminator = {"CR/LF" [10]}; |
% b is a bluetooth object configureTerminator(b,"CR/LF",10) |
For more information, see configureTerminator
.
This example uses a loopback device to show how to set up a callback function using the recommended functionality.
Functionality | Use This Instead |
---|---|
% b is a Bluetooth object b.BytesAvailableFcnCount = 5 b.BytesAvailableFcnMode = "byte" b.BytesAvailableFcn = @mycallback function mycallback(src,evt) data = fread(src,src.BytesAvailableFcnCount); disp(evt) disp(evt.Data) end Type: 'BytesAvailable' Data: [1×1 struct] AbsTime: [2019 12 21 16 35 9.7032] |
% b is a bluetooth object configureCallback(b,"byte",5,@mycallback); function mycallback(src,evt) data = read(src,src.BytesAvailableFcnCount); disp(evt) end ByteAvailableInfo with properties: BytesAvailableFcnCount: 5 AbsTime: 21-Dec-2019 12:23:01 |
% b is a Bluetooth object b.Terminator = "LF/CR" b.BytesAvailableFcnMode = "terminator" b.BytesAvailableFcn = @mycallback function mycallback(src,evt) data = fscanf(src,'%s'); disp(evt) disp(evt.Data) end Type: 'BytesAvailable' Data: [1×1 struct] AbsTime: [2019 12 21 16 35 9.7032] |
% b is a bluetooth object configureTerminator(b,"LF/CR") configureCallback(b,"terminator",@mycallback); function mycallback(src,evt) data = readline(src); disp(evt) end TerminatorAvailableInfo with properties: AbsTime: 21-Dec-2019 12:23:01 |
For more information, see configureCallback
.