안녕하세요.
현재 ble를 가지고 휴대폰<->ble기기 연결을 테스트 하고있습니다.
ble기기 주소값과 이름은 알고 있는 상태입니다.
샘플소스를 이용해
1.앱실행하면 device리스트 팝업[현재 켜져있는 기기들을 리스트에 출력]
2.원하는 기기를 선택하면 연결.
이런식으로 구현했습니다. 그런데 매번 리스트를 팝업한 후 선택하는일이 번거로워
처음 앱을 실행한 경우 ble주소값을 sharedpreference로 저장했다가,
그 뒤로 그 저장된 값을 체크해서 자동연결을 하고싶습니다.
소스는 아래와 같습니다
1.onCreate에서 주소값이 저장되어있는지 확인 후,
없으면[즉 처음 앱을 실행] 디바이스액티비티로 넘어가고, 저장되어있으면
핸들러를 호출해줍니다.
if (Address.length() > 0) {
Handler.sendEmptyMessageDelayed(CONNECT, 5000);
} else {
Intent devicescan = new Intent(MainActivity.this,
DeviceActivity.class);
startActivityForResult(devicescan, REQUEST_SELECT_DEVICE);
}
2.핸들러
case CONNECT:
connectDevice(Address);
break;
3.connectDevice
public void connectDevice(String DeviceAddr) {
mConnected = true;
Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
4.mServiceConnection
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();
}
// Automatically connects to the device upon successful start-up
// initialization.
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBluetoothLeService = null;
}
};
이런식으로 구현했습니다.
그런데 기기가 연결되지 않았는데도 연결되었다고 인식해버리는 경우가 있습니다.
예를 들어 기기는 꺼져있는데 주소값만 가지고 연결하다가 그런건지....
인식률이 100퍼센트 정확하지 않습니다. 주소값을 가지고 앱을 실행-자동연결할 때의 문제점을 보완할 수 없을까요?