我试图通过蓝牙从两个不同的活动发送数据。但我没有成功。 这是我的代码:
public class MainActivity extends Activity {
private BluetoothDevice btDevice;
private CommunicationThread cmThread = null;
TextView y;
private static final UUID sppUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
Button activityci, dato1;
TextView texto;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityci=(Button)findViewById(R.id.idboton);
texto=(TextView)findViewById(R.id.idtexto);
activityci.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent comenzarPrincipal = new Intent(getBaseContext(), SegundoActivity.class);
startActivity(comenzarPrincipal);
cmThread.write((char)120);
}
});
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent startIntent = this.getIntent();
String btDevAddress = startIntent.getStringExtra(BuscarDispositivos.EXTRA_DEV_ADDRESS);
btDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(btDevAddress);
BluetoothSocket btSocket = null;
try
{
btSocket = btDevice.createRfcommSocketToServiceRecord(sppUUID);
cmThread = new CommunicationThread(btSocket);
cmThread.start();
}
catch (Exception e)
{
Toast.makeText(this, "Necesita Conectarse!", Toast.LENGTH_SHORT).show();
finish();
return;
}
}
}]
</i>
我的第二项活动:
public class SegundoActivity extends Activity {
private CommunicationThread cmthread=null;
Button btn2;
TextView text2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prueba2);
btn2=(Button)findViewById(R.id.idboton2);
text2=(TextView)findViewById(R.id.idtexto2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int dato=250;
char data=(char)dato;
text2.setText(""+dato);
cmthread.write(data);
}
});
}
}
从第一个活动开始,我可以通过蓝牙发送数据。但是当我从第二个活动发送时,我得到错误。 有人知道这段代码有什么问题吗?
答案 0 :(得分:0)
好的,首先你可以创建一个ViewFlipper。然后,您不需要创建新的Activity,只需要一个新的布局。如果你这样做,你就不会遇到必须从另一个活动中发送它的问题。
我在这里举个例子:
<强> main_layout.xml 强>:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333333">
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
<include android:id="@+id/secondActivity" layout="@layout/second_layout"/>
</ViewFlipper>
</RelativeLayout>
<强> second_layout.xml 强>:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/secondActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333333">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
<强> YourActivity.java 强>:
OnCreate中:
ViewFlipper viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper1);
Button btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Here you go in the next layout
YourActivity.this.viewFlipper.showNext();
}
});
如果你这样做,我保证你不会因为蓝牙而崩溃。