How to send image data to the other Bluetooth enable phone - android

I am developing Bluetooth based application; here I want to send text data as well as image. I am able to pair my device with the other Bluetooth enable phone, but even after successful pairing I am not able to send require data to targeted phone.
As of my search in Android we didn’t require any Headerset during transferring the data "<"just comparing J2me Bluetooth">". Then how to send data to targeted phone?
The code snippet which I used after successful connection establishment with target phone is herewith.
clientSocket.connect(); //successfully connected with targeted phone
tmpOut = clientSocket.getOutputStream();//out stream opened
tmpOut.write(image, 0, image.length);//trying to send data, didn't get any exception
//closed clientsocket and outputstream

Related

Can I connect 2 android devices via WiFi Direct using QR Codes

I want to make a data sharing app which uses WiFi-Direct where there's one Activity for the sender device where a QR Code would be displayed containing all the info about the device for other peers to connect to this one (like MAC Address , which device is the group owner, etc.)
The receiver devices would then scan the QR code and connect to the device after which the Sender would open the explorer to choose the data to send and then send it.
The Problem:
I am not able to find any way to just connect the devices directly using the give data instead of first scanning and then connecting.
Can anyone help me for this?
I think this tutorial will help you..
https://dzone.com/articles/android-device-matching-with-socket-programming
QRGEncoder qrgEncoder = new QRGEncoder(preSharedKey, null, QRGContents.Type.TEXT, smallerDimension);
ServerSocket server = new ServerSocket(6678);
Socket socket = server.accept();

How to receive images via Bluetooth

I want to receive images via Bluetooth and display that image in Image View.I know how to pass image from one activity to another activity but i don't no how to receive images using Bluetooth.
Android framework provides access to the Bluetooth functionality through the Android Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
Scan for other Bluetooth devices
Query the local Bluetooth adapter for paired Bluetooth devices
Establish RFCOMM channels
Connect to other devices through service discovery
Transfer data to and from other devices
Manage multiple connections
Create a BluetoothSocket and connect to it:
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(<your-device>.getUuids()[0].getUuid());
socket.connect();
Listen to the socket (Get data from the device)
InputStream inStream = socket.getInputStream();
while (inStream.available() > 0) {
inStream.read(); // <-- data from device
}
Write to the socket (Send data to the device)
OutputStream outStream = socket.getOutputStream();
byte[] bytes = <some-data>
outStream.write(bytes);
and for more details you can read Bluetooth Api Documentation here

How do I go about sending data between 2 devices which are paired via Bluetooth in Android Studio?

I'm still new to Android studio and I'm having a lot of trouble with Bluetooth, I'm currently making an app where I need to send data between devices. I'm letting the phone's default Bluetooth setup do the pairing but now I need to know how to send the data, I know I need to use input and output streams but I don't know exactly how.
And yes I have searched all over Google, I've followed a lot of Bluetooth tutorials but none of them seem to really explain how to send data from one device to another.
Thanks in advance.
After you establish a secure/insecure connection via bluetooth the rest is just socket programming simply. That is lets think about sending a text. We convert the text to byte and send that by Java OutputStream. In the same manner for the data received we can get it by InputStream.
But remember you need to maintain bunch of code and thread/handler to maintain state and others. Though the basic thing is simply socket programming over Bluetooth socket using the Bluetooth adapter. Have a look at the below repository in github. This creates a chatroom over bluetooth. i.e it sends and receives string data
https://github.com/zahansafallwa/Android-bluetooth-chat-with-emoji/tree/master/app/src/main/java/com/zahan/safallwa/donttalk
Specially have a look at the BluetoothChatService class. That contains codes related to sending data. BluetoothChatService
Edit:
As per your comment lets think that your devices are paired and also connected. Now you only need to send the text. Declare a outputstream
private final OutputStream mmOutStream;
Suppose you have a string. We convert it to byte. Then get our socket outputstream and send data by write() method
String message="this is test data";
byte[] send = message.getBytes();
mmOutStream = socket.getOutputStream(); // here socket is the bluetooth socket you establish
mmOutStream.write(send);//this is what sends the message
Remember:
Edited code is for your understanding only. It is prescribed to use separate thread for sending data.

How to transfer data from one device to another using wifi network in android 2.2

I need to trnasfer data from one android device to another device over wifi network?
If one device is willing to perform access-point like functionality as part of a portable hotspot, and you select that as a wirelss network on the other, you may achieve a network connection between the two that can be used for custom traffic. Though there are ways the hotspot could be implemented where that would not work. Also of concern, the "client" device will now be sending all of its network traffic through the hotspot device, including not just foreground apps but anything it decides to do in the background.
First you need to know to which device you want to transfer the data over network like to mobile device to pc. Suppose you are trying to send data you need will a continuous socket connection using a thread ... the back end device should accept the socket connection and accept the stream....
consider the frist device as client which will write the stream
Runnable action = new Runnable() {
public void run() {
//create your connection
while (true){
//write your stream here
}
}
};
new Thread(action).start();
and server side the device will open the stream and read it continuously
now in the write your stream code use link the and integrate like the example
Click here!

Bluetooth Inputstream Android 2.2

I'm trying to connect a bluetooth device and to read out information from it.
I've used the Bluetooth chat example and changed the UUID tp SPP mode.
The connection works, but the Information that I get from the Inputstream is wrong.
The transmitted String is 20 signs long, but the Inputstream just returns a 7. The rest of the bufferstream is empty.
Does anyone has a clue?
After write try flushing the transmitter's stream.
And on the receiver you will need to wait till you receive the expected bytes.
SPP does not have packet boundaries. So you can receive the packets in multiple chunks and you need have some logic to determine packet boundaries.

Categories

Resources