NXT Bluetooth Access Functions

The NXT contains an integrated Bluetooth (BT) communications link. BT is an industry standard short-distance (up to 10 meters) wireless communications protocol.

§         BT communications can be used to connect to the PC instead of the USB link. You can do all functions available over the USB link via BT except that NXT firmware can only be upgraded via USB. The USB communications is far faster than BT but requires a tethered link. BT, because it is wireless, is un-tethered.

§         BT communications can be used to connect multiple NXTs together.

§         BT communications can also be used to connect the NXT to another BT device – like a cell phone – as long as the other device communicates with the protocol specifications in the NXT BT Development Kit.

BT communications can operate in either a master or slave mode. The NXT BT device can only be in one mode or the other at any one time.

§         When connected to the PC via RobotC, the NXT is in slave mode and the PC is the master.

§         When two NXTs are connected via BT, one is the master device and one is the slave device. A NXT in master mode can support up to three slaves. A NXT in slave mode only supports a single BT connection.

The NXT GUI (Graphical User Interface) provides manual capabilities to manage the BT configuration on the NXT. This set of functions described here provide the ability to do all of the GUI capabilities, and more, within a user application program. You can set up and tear down connections, search for devices, remove items from the contacts list and many more functions.

The NXT firmware manages its BT communications through two lists: a connected device array and a contacts list.

§         The connected device array has four elements that represent the different possible connections. Index zero (i.e port zero) in the array is always used for a slave NXT to contain information about its single connection. Ports 1 to 3 are used for a NXT in master mode to contain information about its three possible connections to other BT devices.

§         The contacts list contains a list of BT devices that have been previously found by a “search” command. Since this is a historical list, the devices in this list may not all be currently accessible. The NXT remembers in this list devices that have previously connected to it and will automatically allow subsequent connection attempts by these devices without manual password entry – the previously used password will automatically be used.

While a NXT in master mode can connect to three slave devices / NXTs simultaneously, it can only communicate with one at a time. This means that a slave NXT/device cannot simply send a message to the master NXT; the master may be in communications mode with one of the other two possible devices and will not be listening to this particular slave. So, the slave must ‘buffer’ its message and wait for a polling request from the master asking for a buffer message. The BT protocol allows for simultaneous transmission over all links; this one at a time restriction is a function of the NXT firmware and the BT chip that it uses.

When compared with USB communications, BT is fairly slow. It typically takes 30 milliseconds to send a message to another device and get a reply. There have been reports that some third party software drivers achieve faster round trip delays of the order of 10 to 15 milliseconds.

BT is implemented on the NXT via a special purpose “Bluecore” hardware module. The main CPU on the NXT communicates with the Bluecore module via an internal messaging scheme. The main CPU sends a message to the Bluecore and then waits for a response. This means that the functions described below generally do not complete in a single transaction. Your program needs to request the action and then continually check the status until the transaction has completed and the result communicated back to the NXT CPU.

The Bluecore hardware can be in one of two modes: processing BT commands or data transmission mode. It cannot be in both modes at a time. This means, for example, that if in the middle of program execution that is communicating with one BT device you make a connection to another BT device during the time the connection is being set up the communications with the first device is stalled.

BT devices have a unique 7-character (256 possible values per character) address. This can be fairly cumbersome so the BT specifications have included a “friendly name” (i.e. a standard character string of printable characters) that can be used to refer to a device.

 

btConnect(nResult, nPort, sFriendlyName);

Attempts to connect BT device with specified name (sFriendlyName) on port nPort. This NXT will be the master and the other the slave.

 

btDisconnect(nResult, nPort);

Disconnects the BT connection on port nPort.

 

btDisconnectAll(nResult);

Disconnects all existing BT connections.

 

btFactoryReset(nResult);

Resets the NXT BT to the factory configuration. All connections will be dropped. The contacts list will be emptied.

 

btRemoveDevice(nResult, sFriendlyName);

Removes a device with the specified name (sFriendlyName) from the contacts lists

 

btSearch(nResult);      

Begins a search for BT devices and adds new entries to the contacts lists. The search can take a long time (15 seconds).

 

btStopSearch(nResult);  

Stops a search that is in progress.

 

getFirstDevice(nResult, nHandle);

getNextDevice(nResult, nHandle);

These two functions are used to scan through the entries in the contacts list. Get first device will initiate the search from the beginning of the list and get will find the next valid entry in the list.

 

getDeviceAddr(nResult, nHandle, sAddr);

getDeviceClass(nResult, nHandle, sCOD);

getDeviceName(nResult, nHandle, sName);

getDeviceStatus(nResult, nHandle, nStatus);

These functions are used to retrieve information about a particular entry in the contacts list.

 

 

getPortName(nResult, nPort, sPortName);

Gets the name (sPortName) of the device connected on port nPort

 

readLinkQuality(nResult, nQuality);

Reads the BT link quality (future)

 

requestLinkQuality(nResult);

Requests the current BT link quality

 

setBluetoothOff(nResult);

setBluetoothOn(nResult);

Turns Bluetooth ON or OFF on the NXT.

 

setBluetoothVisibility(nResult, bBluetoothVisible);

Sets whether the NXT BT is visible or invisible to searches from other Bluetooth devices. Invisible status means that the NXT will not respond to “search for devices” requests from other BT devices. However, if a device already knows the name/address of this NXT, then it can make a connection to this NXT.

 

setDefaultPIN(nResult, sPIN);

Sets a default PIN to use for Bluetooth connections (future)

 

setFriendlyName(nResult, sFriendlyName);

Sets the sFriendlyName that a NXT will be known by. The friendly names is normally displayed on the top status line of the NXT LCD.

 

transferFile(nResult, nPort, sFileName);

Transfers the file sFileName from this NXT to the NXT connected to port nPort.

 

nBluetoothCmdStatus     

Gets the status/progress of the last BT 'command'

 

nBluetoothStatus        

Gets the current BT status. See the enum for more details. Contains a bit map that indicates whether BT is on/off, visible/invisible and connected/not connected. These status bits are used to update the BT icon on the top line status display on the NXT.

 

nLastBTCommand          

Gets the last BT command issued.