Android デバイスとハードウェア デバイス間の接続を作成した Bluetooth アプリケーションに取り組んでいます。
ハードウェア デバイスとのテキスト データの送受信に成功しました。
ハードウェア デバイスから Android デバイスへの IMAGE TRANSFER で問題が発生しています。
以下のコードを使用して、BYTE ARRAY から BITMAP を作成し、SDCard に保存しています。
public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
if (D) Log.d(TAG, "connected");
// Cancel the thread that completed the connection
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to manage the connection and perform transmissions
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
//CREATE IMAGE FILE
File empowered=new File(Environment.getExternalStorageDirectory(), "ImageFile.jpeg");
if (empowered.exists()) {
empowered.delete();
}
try {
fos = new FileOutputStream(empowered.getPath());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
//fos.write(buffer);
bitmap = BitmapFactory.decodeByteArray(buffer , 0, buffer.length);
bitmap.compress(CompressFormat.JPEG, 100, fos);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
setState(STATE_CONNECTED);
}
受信コード:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
// Keep listening to the InputStream while connected
//while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
if (bytes != -1) {
//ensure DATAMAXSIZE Byte is read.
int byteNo2 = bytes;
int bufferSize = 7340;
while(byteNo2 != bufferSize){
bufferSize = bufferSize - byteNo2;
byteNo2 = mmInStream.read(buffer,bytes,bufferSize);
if(byteNo2 == -1){
break;
}
bytes = bytes+byteNo2;
}
}
}
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
testModeOnScreen.sendMailTest("emailAddress","Received bytes"+e.toString(),"IOEXCEPTION");
connectionLost();
//break;
}
}