This section provides a description
of how to use the demo application to connect two configured board and communicate
over Bluetooth. Bluetooth SPP is a simple Client-Server connection process. We will
setup one of the boards as a server and the other board as a client. We will then
initiate a connection from the client to the server. Once connected, we can transmit
data between the two devices over Bluetooth.
Server setup on the demo
application
- We will setup the first board as
a server. Perform the steps mentioned earlier in "Running the Bluetooth Code"
section to initialize the application. Once initialized, note the Bluetooth
address of the server. We will later use this to initiate a connection from the
client.
- On the "Choose mode>" prompt,
enter Server.
- You will see a list of all
possible commands at this time for a server. You can see this list at any time
by entering Help at the Server> prompt.
- Now we are ready to open a
server. To open a server, at the "Server>" prompt, enter Open 1. You
can replace 1 with any number between 1 and 30, as long as there is no server
open on that port. Once you see "Server opened: 1", you have a SPP server open
on port 1.
Client setup and device discovery on the demo application
- We will setup the second board as
a client. Perform the steps mentioned earlier in "Running the Bluetooth Code"
section to initialize the application. On the "Choose mode>" prompt, enter
Client.
- You will see a list of all
possible commands at this time for a Client. You can see this list at any time
by entering Help at the Client> prompt.
- At the "Client>" prompt, enter
Inquiry. This will initiate the Inquiry process. Once it is complete,
you will get a list of all discovered devices.
- You can access this list any time
by choosing DisplayInquiryList at the Client prompt.
Initiating connection from the client
- Note the index number of the
first board that was configured as a server. [If the list is not on the screen,
issue DisplayInquiryList command on the client to display the list of
discovered devices again.]
- Issue a Open <index
number> <server port number> command at the command
prompt.
- Wait for SPP Open confirmation.
l) When a client successfully connects to a server, the server will see the open
indication.
Data Transfer between Client and Server
- Now we have a SPP connection
established and both devices are ready to transmit data to each other.
- On Client or Server you can send
some data to the remote side by issuing a Write command. This command
sends a hardcoded test string to the other side.
- The remote side will receive a
data indication
- The user can read the data by
issuing a Read command.
- The connection can be closed on
either side by issuing the close command. In the example the client closes the
connection and the server receives a close indication.
Example connection using BluetermWe will demonstrate a SPP connection
using Blueterm. Blueterm is an app that can be used to connect an Android device to
the SPPDemo. For more about the app refer to BlueTerm-Google Play.
Note: The app requires Android V2.1 and
above.
- Open a server port on the
MSP430.
- To connect to the device, open
blueterm, press the menu button and hit the connect icon.
- The app should show a list of
paired devices. If the device is not already paired, select scan. It should
search for available bluetooth devices.
- Select and Pair it with the
device running the SPPdemo. If it needs a pincode enter a pincode on the
app/phone. The MSP430 will prompt for a PINCodeResponse. Type
PINCodeResponse <the pin code you used for the phone> in the
prompt. It will pair and you should see an open indication on the MSP430.
- The two devices are now
connected. Data can be sent/received from the two devices as shown here.
Note: If you are having difficulty
connecting to the sppdemo, first pair with the device under your phone's bluetooth
settings and then select the device from the list of paired devices when you select
connect device in blueterm. In the shipped SPP application that is a part of the
SPP/SPPLE application , using the write command sends 76 bytes to the other device.
It is not possible for the sender to send all 76 bytes at once because of the
limitations on buffers that are smaller to reduce RAM usage. So the code sends 31
bytes and waits until the other side is ready to receive more. The other side will
not be ready until it reads the sent data by calling a read operation. So the
idea is to call Read whenever the receiver receives an SPP Data Indication and all
data that was sent will be read.
An example
communication:
SPP+LE>write
Wrote: 76.
SPP+LE>
Transmit Buffer Empty Indication, ID: 0x0001
Here is the Server side (who is receiving the data):
SPP+LE>
SPP Data Indication, ID: 0x0001, Length: 0x0026.
SPP+LE>read
Read: 31.
Message: ~!@#$%^&*()_+`1234567890-=:;"'<
Read: 7.
Message: >?,./@A
Read: 0.
SPP+LE>
SPP Data Indication, ID: 0x0001, Length: 0x0026.
SPP+LE>read
Read: 31.
Message: BCDEFGHIJKLMNOPQRSTUVWXYZ[\]`ab
Read: 7.
Message: cdefghi
Read: 0.
So we have:
31+31+7+7=76.