【发布时间】:2017-03-20 20:13:30
【问题描述】:
我做了一个项目,将字符串从蓝牙发送到其他蓝牙设备。我的问题是,如果我在第一个活动中连接到蓝牙设备,我是否需要再次连接到第二个活动?
private ProgressDialog progress;
public BluetoothAdapter myBluetooth = null;
public static BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent newint = getIntent();
address = newint.getStringExtra(DeviceList.EXTRA_ADDRESS);
setContentView(R.layout.activity_led_vent_control);
init();
leds1 = (Switch) findViewById(R.id.switch1);
leds2 = (Switch) findViewById(R.id.switch2);
leds3 = (Switch) findViewById(R.id.switch3);
leds4 = (Switch) findViewById(R.id.switch4);
leds5 = (Switch) findViewById(R.id.switch5);
brightness = (SeekBar)findViewById(R.id.seekBar);
lumn=(TextView)findViewById(R.id.lumn);
btnDis=(Button)findViewById(R.id.btnDis);
new ConnectBT().execute(); //Call the class to connect
//commands to be sent to bluetooth
leds1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(leds1.isChecked())
turnOnLed1(); //method to turn on
else turnOffLed1();
}
});
}
private void turnOnLed1()
{
if (btSocket!=null)
{
try
{
btSocket.getOutputStream().write("A".toString().getBytes());
l1=true;
}
catch (IOException e)
{
msg("Error");
}
}
}
【问题讨论】:
-
这取决于您的
ConnectBT()课程。你能把它的代码贴在这里吗? -
也许this post 对你有帮助?