我正在尝试通过蓝牙从两个不同的活动中发送数据。但我没有成功。这是我的代码:
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);
}
});
}
}
从第一个活动开始,我可以通过蓝牙发送数据。但是当我从第二个活动发送时,我得到了错误。有人知道这段代码有什么问题吗?