Android 通过蓝牙连续传输位图生成多个inputstream.read()
我正在开发一个android应用程序,将图像从一个设备传输到另一个设备。接收到的图像将存储在SQLite帮助创建的应用程序的本地数据库中。为了实现我的目标,我将位图转换成字节[],并通过outputStream将其传输到另一个设备。我确实从上述实现中获得了成功,但未能在连接的设备上传输第二个映像 我面对的问题如下: 使用单个outputStream.write()和单个inputStream.read(),任何图像的第一次传输都是成功的,但是,使用单个outputStream.write()和多个inoutStream.read()方法调用,相同/另一个图像的连续传输失败 我想知道代码中输入和输出流的实现中的错误 提前谢谢 线程启动连接的代码如下所示:Android 通过蓝牙连续传输位图生成多个inputstream.read(),android,bluetooth,inputstream,outputstream,android-bluetooth,Android,Bluetooth,Inputstream,Outputstream,Android Bluetooth,我正在开发一个android应用程序,将图像从一个设备传输到另一个设备。接收到的图像将存储在SQLite帮助创建的应用程序的本地数据库中。为了实现我的目标,我将位图转换成字节[],并通过outputStream将其传输到另一个设备。我确实从上述实现中获得了成功,但未能在连接的设备上传输第二个映像 我面对的问题如下: 使用单个outputStream.write()和单个inputStream.read(),任何图像的第一次传输都是成功的,但是,使用单个outputStream.write()和多
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){
}
}
}
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();
}
}
}
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();
}
}
});
线程接受连接的代码如下所示:
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){
}
}
}
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();
}
}
}
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();
}
}
});
在上述线程中调用的manageConnectedSocket()方法的代码如下:
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){
}
}
}
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();
}
}
}
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();
}
}
});
线程管理连接设备的代码如下:
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){
}
}
}
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();
}
}
}
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();
}
}
});
在我的应用程序中实现的处理程序代码如下:
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){
}
}
}
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();
}
}
}
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();
}
}
});
重置连接的代码如下所示:
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){
}
}
}
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();
}
}
}
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();
}
}
});
用户将在列表视图中单击配对或找到的设备以发送图像。listview上单击事件的代码如下所示:
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){
}
}
}
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();
}
}
}
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();
}
}
});
DeviceListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
if(startConnectionThread!=null)
{
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.request);
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
compress(bitmap.CompressFormat.PNG,100,baos);
byte[]bytes=baos.toByteArray();
manageConnectedDevicesThread.write(字节);
意图=新意图();
意图、行动(“收到”);
发送广播(意图);
}
否则{
System.out.println(“单击列表视图”);
发送设备滞后=1;
writeCount=0;
系统输出打印项次(“重置后”);
bluetoothAdapter.cancelDiscovery();
字符串地址=新字符串();
地址=arrayAdapter.getItem(arg2);
selectedBluetoothDevice=bluetoothAdapter.getRemoteDevice(address.substring(address.length()-17));
startConnectionThread=新的startConnectionThread(SelectedBluetooth设备);
startConnectionThread.start();
}
}
});
您是遇到了错误还是只是超时了?我没有遇到任何错误。在连续传输任何其他图像或相同图像时,inputstream.read()方法的调用次数超过了对outputstream.write()的单个调用次数。我可能完全错误地解释了这一点,但我认为这是因为在连续传输时,字节[]在从outputstream到inputstream的传输过程中发生故障。如果您能帮助我找到问题的解决方案,我将不胜感激。