2016-10-28 7 views
0

Ich fand hier ein sehr einfaches Beispiel für die Bluetooth-Verbindung mit Arduino. http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/ Das ganze Projekt herunterladen: http://www.instructables.com/id/Android-Bluetooth-Control-LED-Part-2/step9/Download/ Vielen Dank für den Autor Deyson! Es hase zwei acitvities: 1) DeviceList.java Dies zeigt die gekoppelten Geräte und man kann die gewünschte auswählen:Übergeben Sie die Bluetooth-Verbindung zwischen Aktivitäten in Android Studio

package com.led.led; 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Set; 


public class DeviceList extends ActionBarActivity 
{ 
//widgets 
Button btnPaired; 
ListView devicelist; 
//Bluetooth 
private BluetoothAdapter myBluetooth = null; 
private Set<BluetoothDevice> pairedDevices; 
public static String EXTRA_ADDRESS = "device_address"; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_device_list); 

    //Calling widgets 
    btnPaired = (Button)findViewById(R.id.button); 
    devicelist = (ListView)findViewById(R.id.listView); 

    //if the device has bluetooth 
    myBluetooth = BluetoothAdapter.getDefaultAdapter(); 

    if(myBluetooth == null) 
    { 
     //Show a mensag. that the device has no bluetooth adapter 
     Toast.makeText(getApplicationContext(), "Bluetooth Device Not Available", Toast.LENGTH_LONG).show(); 

     //finish apk 
     finish(); 
    } 
    else if(!myBluetooth.isEnabled()) 
    { 
      //Ask to the user turn the bluetooth on 
      Intent turnBTon = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(turnBTon,1); 
    } 

    btnPaired.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
      pairedDevicesList(); 
     } 
    }); 

} 

private void pairedDevicesList() 
{ 
    pairedDevices = myBluetooth.getBondedDevices(); 
    ArrayList list = new ArrayList(); 

    if (pairedDevices.size()>0) 
    { 
     for(BluetoothDevice bt : pairedDevices) 
     { 
      list.add(bt.getName() + "\n" + bt.getAddress()); //Get the device's name and the address 
     } 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), "No Paired Bluetooth Devices Found.", Toast.LENGTH_LONG).show(); 
    } 

    final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); 
    devicelist.setAdapter(adapter); 
    devicelist.setOnItemClickListener(myListClickListener); //Method called when the device from the list is clicked 

} 

private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() 
{ 
    public void onItemClick (AdapterView<?> av, View v, int arg2, long arg3) 
    { 
     // Get the device MAC address, the last 17 chars in the View 
     String info = ((TextView) v).getText().toString(); 
     String address = info.substring(info.length() - 17); 

     // Make an intent to start next activity. 
     Intent i = new Intent(DeviceList.this, ledControl.class); 

     //Change the activity. 
     i.putExtra(EXTRA_ADDRESS, address); //this will be received at ledControl (class) Activity 
     startActivity(i); 
    } 
}; 


@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_device_list, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 
} 

2) ledControl.java Diese Knöpfe und commuincation ist mit:

package com.led.led; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.view.View; 
import android.widget.Button; 
import android.widget.SeekBar; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.ProgressDialog; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.os.AsyncTask; 

import java.io.IOException; 
import java.util.UUID; 


public class ledControl extends ActionBarActivity { 

Button btnOn, btnOff, btnDis; 
SeekBar brightness; 
TextView lumn; 
String address = null; 
private ProgressDialog progress; 
BluetoothAdapter myBluetooth = null; 
BluetoothSocket btSocket = null; 
private boolean isBtConnected = false; 
//SPP UUID. Look for it 
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); //receive the address of the bluetooth device 

    //view of the ledControl 
    setContentView(R.layout.activity_led_control); 

    //call the widgtes 
    btnOn = (Button)findViewById(R.id.button2); 
    btnOff = (Button)findViewById(R.id.button3); 
    btnDis = (Button)findViewById(R.id.button4); 
    brightness = (SeekBar)findViewById(R.id.seekBar); 
    lumn = (TextView)findViewById(R.id.lumn); 

    new ConnectBT().execute(); //Call the class to connect 

    //commands to be sent to bluetooth 
    btnOn.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      turnOnLed();  //method to turn on 
     } 
    }); 

    btnOff.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
      turnOffLed(); //method to turn off 
     } 
    }); 

    btnDis.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Disconnect(); //close connection 
     } 
    }); 

    brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      if (fromUser==true) 
      { 
       lumn.setText(String.valueOf(progress)); 
       try 
       { 
        btSocket.getOutputStream().write(String.valueOf(progress).getBytes()); 
       } 
       catch (IOException e) 
       { 

       } 
      } 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 
} 

private void Disconnect() 
{ 
    if (btSocket!=null) //If the btSocket is busy 
    { 
     try 
     { 
      btSocket.close(); //close connection 
     } 
     catch (IOException e) 
     { msg("Error");} 
    } 
    finish(); //return to the first layout 

} 

private void turnOffLed() 
{ 
    if (btSocket!=null) 
    { 
     try 
     { 
      btSocket.getOutputStream().write("TF".toString().getBytes()); 
     } 
     catch (IOException e) 
     { 
      msg("Error"); 
     } 
    } 
} 

private void turnOnLed() 
{ 
    if (btSocket!=null) 
    { 
     try 
     { 
      btSocket.getOutputStream().write("TO".toString().getBytes()); 
     } 
     catch (IOException e) 
     { 
      msg("Error"); 
     } 
    } 
} 

// fast way to call Toast 
private void msg(String s) 
{ 
    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); 
} 

@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_led_control, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

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(ledControl.this, "Connecting...", "Please wait!!!"); //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; 
    } 
    @Override 
    protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine 
    { 
     super.onPostExecute(result); 

     if (!ConnectSuccess) 
     { 
      msg("Connection Failed. Is it a SPP Bluetooth? Try again."); 
      finish(); 
     } 
     else 
     { 
      msg("Connected."); 
      isBtConnected = true; 
     } 
     progress.dismiss(); 
    } 
    } 
} 

Also habe ich noch eine Taste in er die dritte Seite zu öffnen ledControl.java zu, genannt Programing:

btnProgram.setOnClickListener(new View.OnClickListener() //Open programing page 
{ 
    @Override 
    public void onClick(View v) 
    {     
    Intent i = new Intent(getApplicationContext(), Programing.class); 
    startActivity(i); 
    } 
}); 

Das Problem ist, dass sich die dritte Seite nicht an die Bluetooth-Verbindung "erinnert", so dass ich keine Informationen senden kann. Ich weiß, dass einige Beiträge hier im gleichen Thema sind. Ich habe viele Möglichkeiten ausprobiert, aber ohne Erfolg. Wenn jemand einen einfachen Code vorschlagen kann, um die BT Verbindung zwischen den Aktivitäten am Leben zu erhalten, wird es für die Anfänger wie mich sehr nützlich sein. Also wie ist es möglich die BT Verbindung auf eine einfache Weise zu übertragen? Ich frage Sie, bitte setzen Sie Ihren Vorschlag in den obigen Codes. Vielen Dank für Ihre Hilfe im Voraus.

Antwort

0

Sie könnten eine benutzerdefinierte Application-Unterklasse implementieren und die Bluetooth-Verbindung dort speichern. Dann könnte eine Aktivität von dort aus danach fragen, da es für den Lebenszyklus Ihrer Anwendung verfügbar ist.

public class MyApplication extends Application 
{ 

    private static MyApplication sInstance; 

    public static MyApplication getApplication() { 
     return sInstance; 
    } 

    BluetoothSocket btSocket = null; 

    public void onCreate() { 
     super.onCreate(); 

     sInstance = this; 
    } 

    public void setupBluetoothConnection() 
    { 
     // Either setup your connection here, or pass it in 
    } 

    public BluetoothSocket getCurrentBluetoothConnection() 
    { 
     return btSocket; 
    } 
} 

Und in Ihrer Tätigkeit Aufruf:

MyApplication.getInstance().setupBluetoothConnection(); 

Und später:

MyApplication.getInstance().getCurrentBluetoothConnection(); 

Sie müssen angeben, dass Ihre Anwendung verwendet diese Anwendungsklasse in AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name" 
    android:name="com.yourpackage.MyApplication"> 

Ein anderer Option wäre, einen benutzerdefinierten Dienst zu implementieren, der die Bluetooth-Verbindung einrichtet/speichert, aber das wird mehr Aufwand und Wartung sein.

+0

Vielen Dank für Ihre schnelle Antwort. Ich habe angefangen, an Ihrer Lösung zu arbeiten. Bis jetzt existiert die Datei 'MyAppliCation.java' ohne Fehler (ich habe 'import android.app.Application;' hinzugefügt). Die BT-Verbindung, die bereits in der privaten Klasse ConnectBT eingerichtet wurde, erweitert AsyncTask // UI-Thread und wird in der Prozedur 'protected void onPostExecute (Void result)' bestätigt. Der nächste Schritt ist, wie Sie geschrieben haben, um diese Verbindung in Ihrem Code, in der Prozedur 'public void setupBluetoothConnection()' zu übergeben. Bitte informieren Sie mich, wie kann ich tun?Wo muss ich den Code in meinem Programm schreiben? Danke für die Hilfe. –

1

Nur ein Gedanke, warum nicht alle Bluetooth-Zeug & Kommunikation in einer separaten Service-Klasse, die nicht an Activity Lifecycle gebunden ist?

Sie können mit dem Service über Binder/Intents oder EventBus kommunizieren und von der Benutzeroberfläche entkoppeln.

Als Bonus von dieser Lösung werden Sie die gesamte Kommunikation (Senden und Empfangen) nur einmal und nur an einem einzigen Ort implementieren, anstatt es in mehreren Aktivitäten zu haben.

Verwandte Themen