This example shows how use CA-Mobile API to connect PC and Mobile Phones over Bluetooth connections. In this case, I show you how to implement a Server Bluetooth in a PC and we will use a mobile app for chat between peer (PC or Phone).
Using the CA-Mobile Framework you can implements collaborative applications in a simple way, because the API simplifies the hard work to control bluetooth communications.
The CA-Mobile API use bluecove bluetooth library for SE version, and you need add both libraries in your proyects: CA-Mobile Framework API and bluecove 2.1.X.
See the library package in the image:
Well, the first code is an Server Control class (MyBTServer.java). The code Here:
package mx.camobile.bluetooth;
import emo.comm.connector.bluetooth.server.BTMultiServer;
import emo.comm.message.common.IMsgProcessor;
import emo.comm.message.common.Message;
import emo.comm.message.common.chat.ChatRoster;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class MyBTServer {
//Bluetooth Server Object
private final BTMultiServer btServer;
public MyBTServer(String nickName, IMsgProcessor processor) {
/* Create an instance of Bluetooth MultiServer
Parameters:
1) The UUID (0x11111) for the published bluetooth service (it will be the same in the mobile app)
2) One MsgProcessor (in this case, the same class process all incomming messages)
3) One Roster instance, the Mobile API includes a simple ChatRoster class.
4) The mode of messages propagation, in this case the sender of messages
* is excludes of propagation to avoid repeated messages.
5) Optional, the name of this server. If not specifies a nickname, the bluetooth
* device ID will be used like nickName.
*/
btServer = new BTMultiServer(0x11111, processor, new ChatRoster(),
BTMultiServer.EXCLUDE_SENDER_BROADCAST, nickName);
}
public void startServer() {
//Start Bluetooth Server
btServer.startServer();
}
public void sendMessage(Message msg) {
//Send one message to all connected peers.
btServer.sendMessage(msg);
}
}
The follow code is for create a GUI for the Chat Server, however I only comment the lines related with the Bluetooth Server. Here is the code for GUI of Bluetooth Server and Chat:
package mx.camobile.gui;
import emo.comm.message.common.IMsgProcessor;
import emo.comm.message.common.Message;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import mx.camobile.bluetooth.MyBTServer;
/**
*
* @author René Cruz - G-Cross Studio 2011
*/
public class SimpleGUI extends JFrame implements ActionListener, IMsgProcessor {
private JTextField txtNickName;
private JTextField txtMessage;
private JButton btnConnect;
private JButton btnSend;
private JTextPane txtHistory;
private JScrollPane panel;
private JPanel panelNorth;
private JPanel panelSouth;
private MyBTServer btServer;
public SimpleGUI() {
setSize(400, 200);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panelNorth = new JPanel();
panelNorth.add(new JLabel("Nickname:"));
txtNickName = new JTextField(20);
panelNorth.add(txtNickName);
btnConnect = new JButton("Connect");
btnConnect.addActionListener(this);
panelNorth.add(btnConnect);
add(panelNorth, BorderLayout.NORTH);
txtHistory = new JTextPane();
panel = new JScrollPane();
panel.setViewportView(txtHistory);
add(panel, BorderLayout.CENTER);
panelSouth = new JPanel();
panelSouth.add(new JLabel("Message:"));
txtMessage = new JTextField(20);
panelSouth.add(txtMessage);
btnSend = new JButton("Send");
btnSend.addActionListener(this);
panelSouth.add(btnSend);
add(panelSouth, BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
SimpleGUI simpleGUI = new SimpleGUI();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnConnect) {
//Create a instance of BTServer controller
btServer = new MyBTServer(txtNickName.getText(), this);
//Start BTServer
btServer.startServer();
btnConnect.setEnabled(false);
txtHistory.setText("Server started\n");
} else if (e.getSource() == btnSend) {
/*
* Create an instance of Message class, to send it over BluetoothServer
* Parameters:
* 1) Type of message (LOGIN_MESSAGE, LOGOUT_MESSAGE, NORMAL_MESSAGE, ROSTER_MESSATE, SYSTEM_MESSAGE)
* 2) NickName of sender
* 3) Body or content of Message
*/
Message message = new Message(Message.NORMAL_MESSAGE, txtNickName.getText(), txtMessage.getText());
//Send message over bluetooth server
btServer.sendMessage(message);
txtMessage.setText("");
}
}
public void processMsg(String string) {
// not used in this example...
}
public void processMsg(Message msg) {
// Extract the Type of Message
switch (msg.getType()) {
case Message.LOGIN_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + " is connected\n");
}
break;
case Message.LOGOUT_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + " was disconnected\n");
}
break;
case Message.NORMAL_MESSAGE: {
txtHistory.setText(txtHistory.getText() + msg.getSender() + ": " + msg.getMsgBody() + "\n");
}
break;
}
}
public void processMsg(int i) {
// not used in this example...
}
}
For test this Bluetooth Server, I used a mobile appication that was developed using CA-Mobile API ME version, however I will show you the code in the next examples. The mobile chat application was tested using a Sony Ericsson K790 mobile phone, and works great!
The libraries used in this example:
Download
CA-Mobile API ME version beta 1.3 Download
CA-Mobile API SE version beta 1.0Download
Bluecove Bluetooth Library 2.1 (x86)
Download
Bluecove Bluetooth Library 2.1.1 (x64)
Test the code and comment, thanks!.
Download the Netbeans project and mobile application J2ME for testing(.jar/.jad)
Here!