안녕하세요. onCharacteristicRead에서 여러 특성을 한번에 읽고 싶은데 잘 안되서 질문드립니다.
안드로이드 앱에서 보드 스위치 값이 눌린 순서를 검사하는 걸 구현 중입니다. 보드는 타이니타일 2개 사용중이고 안드로이드 버전은 3.1이고 한 보드에 스위치 2개씩 총 4개가 연결된 상태입니다.
스위치에 서로 다르게 uuid를 부여해서 onCharacteristicRead 함수로 스위치 값을 읽으면 될 줄 알았는데 스위치 값을 어떻게 구분해서 읽을 지 모르겠어요. 인터넷에는 모든 과정을 두 번씩 하라고 하는데 onCharacteristicRead도 두 번 쓰면 되나요?
스위치 값 읽히면 배열에 저장해서 입력순서를 검사하는 방향으로 코드 짜고 싶은데 제대로 하고 있는 건지 궁금합니다..
코드 첨부합니다
public class DeviceActivity extends Activity implements View.OnClickListener {
private static final String TAG = "BLEDevice";
public static final String EXTRA_BLUETOOTH_DEVICE = "BT_DEVICE";
private BluetoothAdapter mBTAdapter,mBTAdapter2;
private BluetoothDevice mDevice,mDevice2;
private BluetoothGatt mConnGatt,mConnGatt2;
private int mStatus,mStatus2;
Button mTurnOnLEDButton,mTurnOffLEDButton,mReadStateButton;
private final BluetoothGattCallback mGattcallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
mStatus = newState;
mConnGatt.discoverServices();
mStatus2 = newState;
mConnGatt2.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mStatus = newState;
mStatus2 = newState;
runOnUiThread(new Runnable() {
public void run() {
};
});
}
};
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
};
@Override // 아두이노 수신부
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
final int i = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,0);
runOnUiThread(new Runnable() {
public void run() {
TextView lefttv = (TextView) findViewById(R.id.lefttv);
lefttv.setText("value="+i);
// TextView righttv = (TextView) findViewById(R.id.righttv);
// righttv.setText("right value="+i);
};
});
}
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
};
};
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "val=", Toast.LENGTH_LONG);
if (v.getId() == R.id.read_state) {
BluetoothGattService disService = mConnGatt.getService(UUID.fromString("19B10000-E8F2-537E-4F6C-D104768A1214"));
if (disService == null) {
Log.d("", "Dis service not found!");
return;
}
BluetoothGattService newdisService = mConnGatt2.getService(UUID.fromString("19B10000-E8F2-537E-4F6C-D104768A1215"));
if (newdisService == null) {
Log.d("", "Dis2 service not found!");
return;
}
BluetoothGattCharacteristic characteristic = disService.getCharacteristic(UUID.fromString("19B10001-E8F2-537E-4F6C-D104768A1214"));
if (characteristic == null) {
Log.d("", " charateristic not found!");
return;
}
BluetoothGattCharacteristic newcharacteristic = newdisService.getCharacteristic(UUID.fromString("19B10001-E8F2-537E-4F6C-D104768A1215"));
if (newcharacteristic == null) {
Log.d("", " charateristic2 not found!");
return;
}
boolean result = mConnGatt.readCharacteristic(characteristic);
if (result == false) {
Log.d("", "reading is failed!");
}
boolean newresult = mConnGatt2.readCharacteristic(newcharacteristic);
if (newresult == false) {
Log.d("", "reading2 is failed!");
}
}
}
private void init() {
// connect to Gatt
if ((mConnGatt == null)
&& (mStatus == BluetoothProfile.STATE_DISCONNECTED)) {
// try to connect (0. 접속 처리)
mConnGatt = mDevice.connectGatt(this, false, mGattcallback);
mStatus = BluetoothProfile.STATE_CONNECTING;
} else {
if (mConnGatt != null) {
// re-connect and re-discover Services
mConnGatt.connect();
mConnGatt.discoverServices();
} else {
Log.e(TAG, "state error");
finish();
return;
}
}
// connect to Gatt
if ((mConnGatt2 == null)
&& (mStatus2 == BluetoothProfile.STATE_DISCONNECTED)) {
// try to connect
mConnGatt2 = mDevice.connectGatt(this, false, mGattcallback);
mStatus2 = BluetoothProfile.STATE_CONNECTING;
} else {
if (mConnGatt2 != null) {
// re-connect and re-discover Services
mConnGatt2.connect();
mConnGatt2.discoverServices();
} else {
Log.e(TAG, "state error");
finish();
return;
}
}
}
}