초보 개발자 입니다.
페어링 하면 오류가 발생하지 않는거 같은데 페어링 하기전에 비밀번호를 입력하고 접속할 경우에는 아래와 같은 오류가 발생합니다.
인터넷에 있는 소스를 가져다가 수정하면서 만들고 있는데 어디가 잘못 되었는지 찾을 수가 없네요 ㅠㅠ
고수님들의 답변 부탁드립니다.
- 에러 메세지 -
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:593)
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:604)
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:378)
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at com.sharedbag.smart.android.MainActivity.ConnectToRemoteDevice(MainActivity.java:160)
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at com.sharedbag.smart.android.MainActivity.onResume(MainActivity.java:93)
08-30 16:00:32.841 9600-9600/com.sharedbag.smart.android W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
08-30 16:00:32.846 9600-9600/com.sharedbag.smart.android W/System.err: at android.app.Activity.performResume(Activity.java:5530)
08-30 16:00:32.846 9600-9600/com.sharedbag.smart.android W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3066)
08-30 16:00:32.846 9600-9600/com.sharedbag.smart.android W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3105)
08-30 16:00:32.846 9600-9600/com.sharedbag.smart.android W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
package com.sharedbag.smart.android;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity implements Runnable {
BluetoothAdapter btAdapter;
BluetoothDevice rmDevice;
BluetoothSocket btSocket;
OutputStream outStream;
InputStream inStream;
/**/
/*service/app uuid, 1101 SPP UUID
* The UUID has different uses in Android as it may be customized
* to define your application*/
final String app_UUID = "00001101-0000-1000-8000-00805F9B34FB";
/*remote device mac address*/
/*This variable shall hold the macAddress of the remote dvice we shall connect to*/
//String MACAddress = "70:F3:95:57:BF:FE";
// String MACAddress = "00:21:13:00:DE:0B";
String MACAddress = null;
/*Buttons and TextView from the layout*/
Button btnLeft, btnRight;
ImageButton btnOpen, btnClose;
TextView tv_msg;
/*Data structure to hold the bonded devices*/
Set<BluetoothDevice> devices;
/*Adapter to adapt the data structure elements to the listView*/
ArrayAdapter<String> btArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
//btnLeft = (Button) findViewById(R.id.left);
//btnRight = (Button) findViewById(R.id.right);
btnOpen = (ImageButton)findViewById(R.id.btnOpen);
btnClose = (ImageButton)findViewById(R.id.btnClose);
//tv_msg = (TextView) findViewById(R.id.tv_msg);
MACAddress = getIntent().getExtras().getString("MAC");
Log.d("MAXAddr", MACAddress);
//getSupportActionBar().setIcon(R.mipmap.ic_launcher_a);
//ActionBar actionBar = getSupportActionBar();
//actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
}
@Override
protected void onResume() {
super.onResume();
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage("$O01*");
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage("$C02*");
}
});
ConnectToRemoteDevice();
}
@Override
protected void onPause() {
super.onPause();
try {
/*close the bluetooth socket in case of exiting the application*/
btSocket.close();
} catch (Exception e) {
Log.d("error", "an error occurred while quiting the application");
}
}
/**
* @param msg This function shall be used to send messages to the remote bluetooth device
*/
private void sendMessage(String msg) {
try {
outStream.write(msg.getBytes());
//Log.d("BT", "the message " + msg + " sent successfully");
} catch (Exception e) {
//Log.d("error", "IOException error occurred while writing output stream");
Toast.makeText(MainActivity.this, "연결실패, 블루투스장비를 확인하여 주십시오", Toast.LENGTH_LONG).show();
ActivityCompat.finishAffinity(this);
}
}
void ConnectToRemoteDevice() {
btAdapter = BluetoothAdapter.getDefaultAdapter();
rmDevice = btAdapter.getRemoteDevice(MACAddress);
//Log.d("MACAddress", MACAddress);
//Log.d("getAddress", rmDevice.getAddress());
try {
btSocket = rmDevice.createRfcommSocketToServiceRecord(UUID.fromString(app_UUID));
} catch (Exception e) {
Log.d("error", "UUID is NULL || UUID is not formatted correctly");
}
try {
/* as spotted in SDK it's not preferable to connect to a socket while discovery is on*/
btAdapter.cancelDiscovery();
btSocket.connect();
new Thread(this).start();
//Log.d("BT", "successfully connected");
} catch (Exception e) {
e.printStackTrace();
Log.d("error", "Connection failure");
//finishAffinity();
Toast.makeText(MainActivity.this, "연결실패, 블루투스장비를 확인하여 주십시오", Toast.LENGTH_LONG).show();
ActivityCompat.finishAffinity(this);
}
try {
outStream = btSocket.getOutputStream();
} catch (Exception e) {
Log.d("error", "couldn't create output stream");
}
}
@Override
public void run() {
while (true) {
if (btSocket.isConnected() == false) {
break;
}
}
}
}