Bluetooth chat between two or more devices using J2ME
This Java ME code example uses Bluetooth to establish communication between four devices. The devices all send a message to the server, after which the server displays all the messages on all of the devices. Source code Media:BluetoothChat.zip
Screenshots (from WTK emulator)
In this example I created six classes: MyMIDlet, BluetoothClient, BluetoothServer, MessageForm, NumberClientesForm and OptionsForm.
The most important classes are: BluetoothClient and BluetoothServer because they are responsible for Bluetooth communication. The BluetoothServer class is responsible for establish connection with all clients and receive a message from each device.
The function createConnections() opens a new Connection and a new DataInputStream for each connected client and the function receiveMessageFromAll() reads all messages sent from other devices.
The BluetoothClient class is responsible for searching other devices, establish contact with the server, verify the service number, and then send to the server the user’s message. This class must implement DiscoveryListener class so that it will be able to discover available Bluetooth devices. In this class there is a vector called “devices” where all discovered devices are stored and a List called “deviceList” where I store all discovered device’s name to show later on the screen.