Bluetooth

Create Bluetooth object

Description

example

B = Bluetooth(RemoteName,Channel) creates a Bluetooth® object associated with the RemoteName and Channel. RemoteName is a friendly way to identify the RemoteID. If not specified, the default channel is 0.

The Instrument Control Toolbox™ Bluetooth interface lets you connect to devices over the Bluetooth interface, and to transmit and receive ASCII and binary data. Instrument Control Toolbox supports the Bluetooth Serial Port Profile (SPP). You can identify any SPP Bluetooth device and establish a two-way connection with that device.

B = Bluetooth(RemoteID,Channel) creates a Bluetooth object directly from the RemoteID and Channel.

To connect with the Bluetooth device, use the fopen function. When the Bluetooth object is created, its status property is closed. When the object is connected to the remote device with the fopen function, the status property is set to open.

B = Bluetooth(___,Name,Value) creates a Bluetooth object using the specified property values. If an invalid property name or property value is specified the object is not created.

For information on other functions that can be used with Bluetooth, a full example using the Bluetooth interface, events and callbacks, and usage guidelines see Bluetooth Interface Overview.

Examples

collapse all

This example shows how to identify and connect to a Bluetooth device, send a message, and read data.

Find available Bluetooth devices.

 instrhwinfo('Bluetooth')

Create a Bluetooth object called b using channel 3 of a Lego Mindstorm robot with a RemoteName of NXT.

b = Bluetooth('NXT',3);

Connect to the remote device.

fopen(b)

Send a message to the remote device.

fwrite(b,uint8([2,0,1,155]));

Read data from the remote device.

name = fread(b,35);

Disconnect the device.

 fclose(b);

Clean up by deleting and clearing the object.

 delete(b)
 clear b

Input Arguments

collapse all

"Friendly name" for the Bluetooth device, specified as a character vector or string. For example, in the case of an iPhone, it might be simply 'iPhone' or a name like 'Zor'. If it is empty, use the RemoteID to communicate with the device.

Example: 'NXT'

Data Types: char | string

Internal ID of the Bluetooth device, equivalent to the Device ID, specified as a character vector or string. Every device has a device ID, which is usually a 12-digit character vector that starts with 'btspp://'. You can use this or the RemoteName to communicate with the device.

Example: 'btspp://0016530FD65F'

Data Types: char | string

Device channel, specified as a numeric value, if the device has channels. If no channel is specified, it defaults to 0.

Example: 3

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Timeout',60

Name of interface object, specified as a character vector or string.

Example: 'BTdev1'

Data Types: char | string

Time limit in seconds for communication, specified as a numeric value.

Example: 60

Data Types: double

Output Arguments

collapse all

Bluetooth device interface, returned as an interface object.

Introduced in R2011b