I am developing an Android application that transfers images from one device to another. The resulting image will be saved in the local database of my application created using SQLite. To achieve my goals, I converted the bitmap to byte [] and transferred it to another device using outputStream. I succeeded from the above implementation, but could not transfer the second image to the connected device.
The problem I encountered is this: The first transfer of any image is successful using single outputStream.write () and single inputStream.read (), but sequentially transferring the same / other with a single call to outputStream.Write () and multiple inoutStream. read ().
I would like to know the error in the implementation of input and output streams in my code.
Thanks in advance.
The code to start the thread to connect is as follows:
private class StartConnectionThread extends Thread{
private final BluetoothSocket bluetoothSocket;
private final BluetoothDevice bluetoothDevice;
public StartConnectionThread(BluetoothDevice device){
BluetoothSocket tempBluetoothSocket=null;
bluetoothDevice=device;
try
{
System.out.println(uuid);
tempBluetoothSocket=device.createRfcommSocketToServiceRecord(uuid);
}
catch(IOException ioException)
{
}
bluetoothSocket=tempBluetoothSocket;
}
@Override
public void run() {
System.out.println("StartConnectionThread Run");
bluetoothAdapter.cancelDiscovery();
try
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
bluetoothSocket.connect();
}
catch(IOException ioException)
{
System.out.println(ioException);
try
{
bluetoothSocket.close();
}
catch(IOException cancelIOException)
{
}
return;
}
manageConnectedSocket(bluetoothSocket);
}
public void cancel()
{
try
{
bluetoothSocket.close();
}
catch(IOException ioException)
{
}
}
}
The code for the stream to accept the connection is as follows:
private class AcceptConnectionThread extends Thread
{
private final BluetoothServerSocket bluetoothServerSocket;
public AcceptConnectionThread() {
System.out.println("constructor");
BluetoothServerSocket tempBluetoothServerSocket=null;
try
{
tempBluetoothServerSocket=bluetoothAdapter.listenUsingRfcommWithServiceRecord("My Souvenirs", uuid);
}
catch(IOException ioException)
{
}
bluetoothServerSocket=tempBluetoothServerSocket;
}
@Override
public void run() {
System.out.println("AcceptConnectionThread Run");
BluetoothSocket bluetoothSocket=null;
while(true)
{
try
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(bluetoothServerSocket);
if(bluetoothServerSocket!=null)
{
bluetoothSocket=bluetoothServerSocket.accept();
}
else {
System.out.println("bluetoothserversocket==null");
}
System.out.println("accept");
}
catch(IOException ioException){
System.out.println("exception after accept");
System.out.println(ioException);
break;
}
if(bluetoothSocket!=null)
{
manageConnectedSocket(bluetoothSocket);
try {
bluetoothServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
public void cancel()
{
try{
bluetoothServerSocket.close();
}
catch(IOException ioException){
}
}
}
The code for the manageConnectedSocket () method called in the threads above looks like this:
public void manageConnectedSocket(BluetoothSocket bluetoothSocket)
{
manageConnectedDevicesThread=new ManageConnectedDevicesThread(bluetoothSocket);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.request);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] bytes=baos.toByteArray();
manageConnectedDevicesThread.write(bytes);
System.out.println("manageConnectedSocket() method called");
manageConnectedDevicesThread.start();
System.out.println("success");
Intent intent=new Intent();
intent.setAction("received");
sendBroadcast(intent);
}
The code for the thread to manage connected devices is as follows:
private class ManageConnectedDevicesThread extends Thread
{
private final BluetoothSocket connectedBluetoothSocket;
InputStream tempInputStream=null;
OutputStream tempOutputStream=null;
public ManageConnectedDevicesThread(BluetoothSocket socket) {
connectedBluetoothSocket=socket;
try
{
tempInputStream=socket.getInputStream();
tempOutputStream=socket.getOutputStream();
}
catch(IOException ioException)
{
}
inputStream=tempInputStream;
outputStream=tempOutputStream;
}
@Override
public void run() {
System.out.println("ManageConnectedDevicesThread Run");
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] buffer=new byte[1024*8];
int bytes;
while(true)
{
try
{
bytes=inputStream.read(buffer);
handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
System.out.println("handler");
}
catch(IOException ioException)
{
System.out.println("for handler:" +ioException);
break;
}
}
}
public void write(byte[] bytes)
{
try
{
outputStream.write(bytes,0,bytes.length);
}
catch(IOException ioException){
System.out.println("exception in write statement of managing connections");
}
}
public void close()
{
try {
connectedBluetoothSocket.close();
} catch (IOException e) {
}
}
}
, , :
private final Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1);
byte[] b=readMessage.getBytes();
if(sendingDeviceFlag==0)
{
Intent intent=new Intent();
intent.setAction("received");
sendBroadcast(intent);
bluetoothDatabase.open();
bluetoothDatabase.insert_slam(readMessage, readBuf);
writeCount++;
System.out.println("write count = "+writeCount);
Cursor cursor=bluetoothDatabase.getSlam();
if(cursor!=null)
{
cursor.moveToFirst();
while(cursor.moveToNext())
{
byte[] barray=cursor.getBlob(cursor.getColumnIndex("img"));
Bitmap bitmap1=BitmapFactory.decodeByteArray(barray, 0, barray.length);
imageView.setImageBitmap(bitmap1);
}
System.out.println("Count = "+cursor.getCount());
}
else {
System.out.println("cursor null");
}
bluetoothDatabase.close();
}
break;
}
};
reset :
void resetConnection()
{
if(inputStream!=null)
{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream!=null)
{
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(startConnectionThread!=null)
{
System.out.println("start wala active tha");
startConnectionThread.cancel();
}
if(acceptConnectionThread!=null)
{
System.out.println("accept wala active tha");
acceptConnectionThread.cancel();
acceptConnectionThread.interrupt();
acceptConnectionThread=new AcceptConnectionThread();
acceptConnectionThread.start();
}
if(manageConnectedDevicesThread!=null)
{
System.out.println("manage wala active tha");
manageConnectedDevicesThread.close();
}
}
}
, . click on listview :
devicesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(startConnectionThread!=null)
{
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.request);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
byte[] bytes=baos.toByteArray();
manageConnectedDevicesThread.write(bytes);
Intent intent=new Intent();
intent.setAction("received");
sendBroadcast(intent);
}
else {
System.out.println("click on list view");
sendingDeviceFlag=1;
writeCount=0;
System.out.println("after reset");
bluetoothAdapter.cancelDiscovery();
String address=new String();
address=arrayAdapter.getItem(arg2);
selectedBluetoothDevice=bluetoothAdapter.getRemoteDevice(address.substring(address.length()-17));
startConnectionThread=new StartConnectionThread(selectedBluetoothDevice);
startConnectionThread.start();
}
}
});