我正在开发一个将图像从一个设备传输到另一个设备的Android应用程序。收到的图像将存储在SQLite帮助下创建的应用程序的本地数据库中。为了实现我的目标,我已经将位图转换为byte []并通过outputStream的帮助将其转移到另一个设备。我确实从上面的实现中获得了成功,但是在连接设备上传输第二个图像失败了。
我面临的问题如下: 使用单个 outputStream.write()和单个 inputStream.read()成功进行任何图像的首次传输,但连续传输相同/另一个图片失败,带有单个 outputStream.Write()和多个 inoutStream.read()方法调用。
我想知道我的代码中输入和输出流的实现错误。
提前致谢。
启动连接的线程代码如下:
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() {
// TODO Auto-generated method stub
System.out.println("StartConnectionThread Run");
bluetoothAdapter.cancelDiscovery();
try
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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)
{
}
}
}
接受连接的线程代码如下:
private class AcceptConnectionThread extends Thread
{
private final BluetoothServerSocket bluetoothServerSocket;
public AcceptConnectionThread() {
// TODO Auto-generated constructor stub
System.out.println("constructor");
BluetoothServerSocket tempBluetoothServerSocket=null;
try
{
tempBluetoothServerSocket=bluetoothAdapter.listenUsingRfcommWithServiceRecord("My Souvenirs", uuid);
}
catch(IOException ioException)
{
}
bluetoothServerSocket=tempBluetoothServerSocket;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("AcceptConnectionThread Run");
BluetoothSocket bluetoothSocket=null;
while(true)
{
try
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
public void cancel()
{
try{
bluetoothServerSocket.close();
}
catch(IOException ioException){
}
}
}
上述线程中调用的manageConnectedSocket()方法的代码如下:
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);
}
管理连接设备的线程代码如下:
private class ManageConnectedDevicesThread extends Thread
{
private final BluetoothSocket connectedBluetoothSocket;
InputStream tempInputStream=null;
OutputStream tempOutputStream=null;
public ManageConnectedDevicesThread(BluetoothSocket socket) {
// TODO Auto-generated constructor stub
connectedBluetoothSocket=socket;
try
{
tempInputStream=socket.getInputStream();
tempOutputStream=socket.getOutputStream();
}
catch(IOException ioException)
{
}
inputStream=tempInputStream;
outputStream=tempOutputStream;
}
@Override
public void run() {
// TODO Auto-generated method stub
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) {
// TODO: handle exception
}
}
}
我的应用程序中实现的处理程序代码如下:
private final Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
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;
}
};
重置连接的代码如下:
void resetConnection()
{
if(inputStream!=null)
{
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(outputStream!=null)
{
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(startConnectionThread!=null) //my changes
{
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();
}
}
}
用户将在列表视图中单击已配对或找到的设备以发送图像。 listview上click事件的代码如下:
devicesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
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();
}
}
});