簡體   English   中英

Android藍牙通訊

[英]Android bluetooth communication

我做一個項目,將字符串從藍牙發送到其他藍牙設備。 我的問題是,如果我在第一次活動中建立了與藍牙設備的連接,是否需要在第二次活動中再次建立連接?

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課程

private class ConnectBT extends AsyncTask<Void, Void, Void>  // UI thread
{
    private boolean ConnectSuccess = true; //if it's here, it's almost connected

    @Override
    protected void onPreExecute()
    {
        progress = ProgressDialog.show(ledVentControl.this, "Свързва се...", "Моля изчакайте!!!");  //show a progress dialog
    }

    @Override
    protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
    {
        try
        {
            if (btSocket == null || !isBtConnected)
            {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
                BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
                btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();//start connection
            }
        }
        catch (IOException e)
        {
            ConnectSuccess = false;//if the try failed, you can check the exception here
        }
        return null;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM