Bluetooth chat between two or more devices using J2ME
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.