我正在开发一个基于 BluetoothChat 示例的应用程序,其中我有 2 个按钮的主要活动:
1. Find devices
2. Configuration
使用第一个按钮,我搜索具有与上述示例相同的代码的设备。
使用第二个按钮,我进入另一个活动,其中有 3 个搜索栏,每个搜索栏下都有文本视图,显示当前搜索栏的值。
我需要修改 BluetoothChat 示例的代码以发送包含这 3 个值而不是文本的数组。
这是我正在尝试开发的此活动的代码。
我怎样才能做到这一点?
/*********************
*
* ONCREATE
*
********************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
bar1 = (SeekBar)findViewById(R.id.fastbar);
bar1.setOnSeekBarChangeListener(this);
progress1 = (TextView)findViewById(R.id.out1);
bar2 = (SeekBar)findViewById(R.id.climbbar);
bar2.setOnSeekBarChangeListener(this);
progress2 = (TextView)findViewById(R.id.out2);
bar3 = (SeekBar)findViewById(R.id.platformbar);
bar3.setOnSeekBarChangeListener(this);
progress3 = (TextView)findViewById(R.id.out3);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
/**Initialize the buffer for outgoing messages*/
GlobalVar.mOutStringBuffer = new StringBuffer("");
}
/*********************
*
* SEEKBAR
*
********************/
/**Method to get each seekbar's value and save it into a textview*/
@Override
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) {
switch (bar.getId()) {
case R.id.fastbar:
progress1.setText(progress + "%");
break;
case R.id.climbbar:
progress2.setText(progress + "%");
break;
case R.id.platformbar:
progress3.setText(progress + "%");
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setSecondaryProgress(seekBar.getProgress());
}
/*********************
*
* ONRESUME
*
********************/
/**Load seekbar's values from sharedpreferences*/
@Override
protected void onResume() {
super.onResume();
loadedProgress1 = mPreferences.getInt(Key_PROGRESS_1, 50);
bar1.setProgress(loadedProgress1);
loadedProgress2 = mPreferences.getInt(Key_PROGRESS_2, 90);
bar2.setProgress(loadedProgress2);
loadedProgress3 = mPreferences.getInt(Key_PROGRESS_3, 70);
bar3.setProgress(loadedProgress3);
}
/*********************
*
* ONPAUSE
*
********************/
/**Save seekbar's values on sharedfreferences*/
@Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor editor = mPreferences.edit();
savedProgress1 = bar1.getProgress();
editor.putInt(Key_PROGRESS_1, savedProgress1);
savedProgress2 = bar2.getProgress();
editor.putInt(Key_PROGRESS_2, savedProgress2);
savedProgress3 = bar3.getProgress();
editor.putInt(Key_PROGRESS_3, savedProgress3);
editor.commit();
}
/*********************
*
* OPTIONS
*
********************/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**Inflate the menu; this adds items to the action bar if it is present.*/
getMenuInflater().inflate(R.menu.menu_conf, menu);
return true;
}
/**Options menu where I call the send function*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.send:
sendValues();
break;
case R.id.reset:
resetValues(null);
break;
}
return true;
}
/*********************
*
* SEND & RESET
*
********************/
public void sendValues() {
/**Seekbars values*/
String message1 = progress1.getText().toString();
String message2 = progress2.getText().toString();
String message3 = progress3.getText().toString();
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**
*
*HERE IS WHERE I'M TRYING TO CREATE A FUNCTION TO SEND THE 3 VALUES IN A STRING TYPE MESSAGE
*THIS CODE IS FROM THE SETUPCHAT() FUNCTION, SO IS WRONG IN THIS CASE
*/
/**Get the message bytes and tell the to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}
public void resetValues() {
defaultValue1 = 50;
bar1.setProgress(defaultValue1);
defaultValue2 = 90;
bar2.setProgress(defaultValue1);
defaultValue3 = 70;
bar3.setProgress(defaultValue1);
}
这是创建的新函数:
public void sendValues() {
/**Send the seekbars values*/
String message = progress1.getText().toString()+":"+progress2.getText().toString()+":"+progress3.getText().toString();
String[] values = message.split(":");
for (String value : values) {
int number = Integer.valueOf(value);
}
/**Check that we're actually connected before trying anything*/
if (GlobalVar.mTransmission.getState() != GlobalVar.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
/**Get the message bytes and tell the Transmission to write*/
byte[] send = message.getBytes();
GlobalVar.mTransmission.write(send);
/**Reset out string buffer to zero*/
GlobalVar.mOutStringBuffer.setLength(0);
}