How do I send message from one Android phone to another Android phone through bluetooth?
To send messages from one Android phone to another using Bluetooth, you can follow these methods, each with its own set of steps and considerations:
Using Native Android Bluetooth Feature
This method is not widely recommended due to its complexity and security concerns, but here’s how you can do it:
- Enable Bluetooth on both devices and pair them by confirming the passcode.
- On the source device, go to the Messaging app, select the messages you want to transfer, and choose to "Send" or "Share" the selected SMS threads via Bluetooth.
- Select the target device from the list of available devices.
- On the target device, accept the incoming SMS data prompt to start the transfer process3.
Using SMS Backup & Restore App
This method is more reliable and faster than using native Bluetooth:
- Download and install the SMS Backup & Restore app on both devices.
- Launch the app on both devices and choose to "Transfer" your messages.
- On the source phone, select "Send from this phone," and on the target device, select "Receive on this phone."
- The target device will wait for the transfer. On the source device, select the target device from the list of available devices.
- Accept the connection invitation on the target device.
- Choose the relevant option (e.g., send all text messages) on the source device and start the transfer process3.
Developing a Custom Bluetooth App
If you are looking to create a custom solution, you can develop an Android app that uses Bluetooth for message transfer. Here’s a general outline:
- Establish a Bluetooth connection between the two devices using
BluetoothSocket
. - Use the
InputStream
andOutputStream
associated with theBluetoothSocket
to read and write data. - Implement a dedicated thread for reading from and writing to the streams, as these operations are blocking calls4.
Here is a simplified example of how you might set up the communication in a custom app:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "Temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] mmBuffer = new byte[1024];
int numBytes;
while (true) {
try {
numBytes = mmInStream.read(mmBuffer);
// Send the obtained bytes to the UI activity.
Message readMsg = handler.obtainMessage(MessageConstants.MESSAGE_READ, numBytes, -1, mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
Log.d(TAG, "Input stream was disconnected", e);
break;
}
}
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Log.e(TAG, "Error occurred when sending data", e);
}
}
}
This example is based on the Bluetooth Chat sample app provided by Android Developers4.