현재 아두이노라는 장치에 압력센서를 연결한후 안드로이드에서 그 값을 받아 정수값으로 추출하는 작업을 하고있습니다.
아두이노 코딩으로는
void setup(){
Serial.begin(9600);
int val;
int vol;
val = analogRead(0);
vol = analogRead(0);
Serial.println(val.DEC);
Serial.println(vol.DEC);
delay(100);
}
원리는 아두이노 코딩에서 Serial.println 되는 값을 받아 프린트하는것이더군요
안드로이드 코딩입니다.
CommunicatorActivity.java
BroadcastReceiver mReceiver = new BroadcastReceiver() {
private void handleTransferedData(Intent intent, boolean receiving) {
if (mIsReceiving == null || mIsReceiving != receiving) {
mIsReceiving = receiving;
mTransferedDataList.add(new ByteArray());
}
final byte[] newTransferedData = intent.getByteArrayExtra(ArduinoCommunicatorService.DATA_EXTRA);
ByteArray transferedData = mTransferedDataList.get(mTransferedDataList.size() - 1);
//byte->int transfer
ByteBuffer buff = ByteBuffer.allocate(4);
buff=ByteBuffer.wrap(newTransferedData);
buff.order(ByteOrder.BIG_ENDIAN);
buff.getInt();
ddd=IntSource;
transferedData.add(newTransferedData);
mTransferedDataList.set(mTransferedDataList.size() - 1, transferedData);
mDataAdapter.notifyDataSetChanged();
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
//if (DEBUG) Log.d(TAG, "onReceive() " + action);
if (ArduinoCommunicatorService.DATA_RECEIVED_INTENT.equals(action)) {
handleTransferedData(intent, true);
} else if (ArduinoCommunicatorService.DATA_SENT_INTERNAL_INTENT.equals(action)) {
handleTransferedData(intent, false);
}
}
};
intent.getByteArrayExtra(ArduinoCommunicatorService.DATA_EXTRA); 가 아두이노에서 입력받는 데이터를 어떤 형태로 받아오는지, 그리고 어떻게해야 정수형으로 받아오는지가 알고싶은데 쉽지않습니다.(현재 위의 형변환 코드는 getInt()에서 애러가 생기는것같습니다 ㅠㅠ)