안녕하세요.
ble를 이용해 폰<->블루투스 기기를 연결하는것에 대해 궁금한것이 있습니다.
브로드캐스트를 이용해서 커넥트/디스커넥트 감지도 되었고
그에 따른 처리[이미지를 달리 주는 등]도 하였습니다.
true/false 변수를 하나 두어서, 커넥트 상태일 땐 true,디스커넥트일 땐 false가 되도록 하였습니다.
그런데, ble[혹은 블루투스] 상태 인식률?이 어느정도 되는지 궁금합니다.
전체적으로 커넥트/디스커넥트/중간에 기기가 꺼져서 블루투스 연결이 끊긴 경우 재연결시도 기능은 되는데,
1.간혹가다가 재연결이 안되는 경우도 있고, [핸들러를 이용하여 3번까지 연결시도를 합니다]
2.connect 되었다고 표시는 되었는데[이미지가 변경됩니다.] 막상 통신은 안되는 경우도 있습니다.
3.기기가 켜져있는데 디바이스 리스트에 뜨지 않는 경우도 있는데..
인식률이 100퍼센트이지는 못하는 게 정상인가요?
아님 코드내에서 제가 따로 처리 해야 하는 것이 있는것인지 궁금합니다.
블루투스 연결쪽 코드도 올려봅니다.. 조언 주시면 감사하겠습니다 (__)
public final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
//가끔연결안되었는데 되었다고 출력됨\
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action))
{
mConnected=true;
Log.d("mainactivity-ACTION_GATT_CONNECTED", "ACTION_GATT_CONNECTED");
//연결이 되었으면, 아이콘을 바꿔준다.
penHandler.sendEmptyMessage(CONN);
}
else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action))
{
mConnected = false;
// 디스커넥트
Log.d("disconnect", "기기와 연결해제.action_gatt_disconnected");
btn_disconn.setVisibility(View.VISIBLE);
btn_conn.setVisibility(View.INVISIBLE);
if (count == 0)
{
penHandler.sendEmptyMessageDelayed(CONN, 0);
count++;
}
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED
.equals(action))
{
displayGattServices(mBluetoothLeService.getSupportedGattServices());
}
//생략
private Handler penHandler = new Handler()
{
public void handleMessage(Message msg) {
switch (msg.what) {
case CONNECT:
Log.d("handler connect-address",temp_penAddress);
Log.d("connect", "connect시작");
connectDevice(temp_penAddress);
break;
case CONN:
try
{
if (mConnected == false) // 연결되지 않았다면
{
// 연결되지 않았다고 표시해주기~
btn_disconn.setVisibility(View.VISIBLE);
btn_conn.setVisibility(View.INVISIBLE);
if (count < 3)
{
count++;
Toast.makeText(getApplicationContext(),
count + "회 연결 시도중입니다...:",
Toast.LENGTH_LONG).show();
mBluetoothLeService.connect(temp_penAddress);// mDeviceAddress
Log.e("CONN/연결 시도중......", temp_penAddress);
penHandler.sendEmptyMessageDelayed(CONN, 5000);
}
else
{
count = 0;
}
}
else
{
btn_disconn.setVisibility(View.INVISIBLE);
btn_conn.setVisibility(View.VISIBLE);
count = 0;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case DISCONN: // 연결해제
btn_disconn.setVisibility(View.VISIBLE);
btn_conn.setVisibility(View.INVISIBLE);
mConnected = false;
mBluetoothLeService.disconnect();
break;
}
};
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (!ba.isEnabled())
{
finish();
}
Log.d("onActivityResult", "...........");
switch (requestCode) {
case REQUEST_SELECT_DEVICE:
if (resultCode == Activity.RESULT_OK && data != null) {
Log.e("test2", "device....");
mDeviceName = data.getStringExtra("DEVICE_NAME");
mDeviceAddress = data.getStringExtra("DEVICE_ADDRESS");
// 주소를 가져온후 주소가 저장된 적이 있는지 확인후 저장되어 있지 않으면 저장해둔다.
// 리스트에서 선택할때는 분기에서 저장된 값이 없는 거니 바로 저장해준다.
SharedPreferences.Editor editor = penspf.edit();
editor.putString(penspf_key, mDeviceAddress);
editor.commit();
connectDevice(mDeviceAddress);
}
break;
}
};// onActivityResult
// jin
public void connectDevice(String DeviceAddr)
{
mConnected = true;
Log.d("커넥트디바이스1", "커넥트디바이스");
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service)
.getService();
if (!mBluetoothLeService.initialize()) {
Log.e("BLE", "Unable to initialize Bluetooth");
finish();
}
penspf = getSharedPreferences(penspf_name, MODE_PRIVATE);
String newAddress = penspf.getString(penspf_key, "");
if (newAddress.length() > 0)
{
mBluetoothLeService.connect(newAddress);//mDeviceAddress
}
else
{
mBluetoothLeService.connect(mDeviceAddress);//mDeviceAddress
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBluetoothLeService = null;
}
};