J
jdajul
Ambitioniertes Mitglied
- 1
Hallo ich habe bei meiner App einen Button eingebaut über den sich eine neue View öffnet in dem alle Bluetoothgeräte in der Umgebung angezeigt werden. Wenn ich dort ein Gerät auswähle und dann auf die zurück-Taste gehe, so öffnet sich zwar wieder die ursprüngliche View, allerdings sind die Elemente dieser View alle inaktiv. Hier meinen Code: Wäre toll wenn mir jemand helfen könnte.
Viele Grüße,
jdajul
Viele Grüße,
jdajul
Code:
package jritter.stamer.bluetoothtest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.media.audiofx.Visualizer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.bluetooth.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.Set;
//https://github.com/TriggerTrap/SeekArc
public class MainActivity extends ActionBarActivity {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
static final boolean D = true;
static final int REQUEST_ENABLE_BT = 2;
static final String TAG = "Bluetooth";
// SPP UUID service - this should work for most devices
static final UUID BTMODULEUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothSocket btSocket = null;
ConnectedThread mConnectedThread;
VerticalSeekBar Master, Channel1, Channel2, Channel3, Channel4, Channel5, Channel6;
CheckBox MuteMaster, Mute1, Mute2, Mute3, Mute4, Mute5, Mute6;
Button connect;
TextView textView;
ArrayList arrayList;
ListView lv;
BroadcastReceiver mReceiver;
ArrayAdapter<String> arrayAdapter;
Spinner effects;
List<String> spinnerArray = Arrays.asList("no effect", "Hall 1", "Hall 2 ", "Compressor", "Chorus" );
private static long back_pressed;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();
turnBluetoothOn();
bluetooth = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
deviceSupportBluetooth();//?
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something...
getSurroundingDevicesandConnect();
}
});
}
private void initializeVariables() {
Master = (VerticalSeekBar) findViewById(R.id.Seekbar);
Channel1 = (VerticalSeekBar) findViewById(R.id.Seekbar1);
Channel2 = (VerticalSeekBar) findViewById(R.id.seekbar2);
Channel3 = (VerticalSeekBar) findViewById(R.id.seekbar3);
Channel4 = (VerticalSeekBar) findViewById(R.id.seekbar4);
Channel5 = (VerticalSeekBar) findViewById(R.id.seekbar5);
Channel6 = (VerticalSeekBar) findViewById(R.id.seekbar6);
textView = (TextView) findViewById(R.id.textView);
MuteMaster = (CheckBox) findViewById(R.id.muteMaster);
Mute1 = (CheckBox) findViewById(R.id.mute1);
Mute2 = (CheckBox) findViewById(R.id.mute2);
Mute3 = (CheckBox) findViewById(R.id.mute3);
Mute4 = (CheckBox) findViewById(R.id.mute4);
Mute5 = (CheckBox) findViewById(R.id.mute5);
Mute6 = (CheckBox) findViewById(R.id.mute6);
connect = (Button) findViewById(R.id.connect);
effects = (Spinner) findViewById(R.id.spinner2);
arrayList = new ArrayList();
seekbarConfig(Master, "Master");
seekbarConfig(Channel1, "Channel1");
seekbarConfig(Channel2, "Channel2");
seekbarConfig(Channel3, "Channel3");
seekbarConfig(Channel4, "Channel4");
seekbarConfig(Channel5, "Channel5");
seekbarConfig(Channel6, "Channel6");
muteConfig(MuteMaster);
muteConfig(Mute1);
muteConfig(Mute2);
muteConfig(Mute3);
muteConfig(Mute4);
muteConfig(Mute5);
muteConfig(Mute6);
effectsConfig(effects);
}
//android...
@Override
public void onStart() {
super.onStart();
if (D) Log.e(TAG, "++ ON START ++");
}
@Override
public void onDestroy() {
super.onDestroy();
if (D) Log.e(TAG, "++ ON DESTROY ++");
if (mReceiver!=null){ unregisterReceiver(mReceiver);
System.out.println("MReceiver closed");}
}
@Override
public void onBackPressed(){
setContentView(R.layout.activity_main);
onResume();
if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
else Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}
// @Override
// public synchronized void onResume() {
// super.onResume();
//
// if (D) Log.e(TAG, "+ ON RESUME +");
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
// TODO GUI...
public void effectsConfig(Spinner spinner){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Spinners abc = new Spinners();
abc.setSpinner(spinner);
abc.setmConnectedThread(mConnectedThread);
spinner.setOnItemSelectedListener(abc);
}
public void muteConfig(CheckBox mute){
Checkboxes abc = new Checkboxes();
abc.setmConnectedThread(mConnectedThread);
mute.setOnClickListener(abc);
}
public void seekbarConfig(VerticalSeekBar seekBar, final String fadername) {
Seekbars abc = new Seekbars();
abc.setFadername(fadername);
abc.setmConnectedThread(mConnectedThread);
seekBar.setOnSeekBarChangeListener(abc);
}
// TODO Bluetooth handling...
public BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
return device.createInsecureRfcommSocketToServiceRecord(BTMODULEUUID);
//creates secure outgoing connecetion with BT device using UUID
}
public void getSurroundingDevicesandConnect() {
setContentView(R.layout.popup);
bluetooth.startDiscovery();
if (bluetooth.startDiscovery()) {
System.out.println("discovery started");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
arrayAdapter.add(device.getName() + "\n" + device.getAddress());
System.out.println("while onreceive" + arrayList);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.v(TAG, "Entered the Finished ");
bluetooth.cancelDiscovery();
} else {
System.out.println("hoppala da funktioniert was nciht");
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
setListView();
}
public void deviceSupportBluetooth() {
if (bluetooth == null) {
Toast.makeText(this, "Your Device does not support Bluetooth", Toast.LENGTH_LONG).show();
finish();
return;
}
}
public void turnBluetoothOn() {
if (!bluetooth.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the control session
}
}
public void setListView() {
lv = (ListView) findViewById(R.id.listView);
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.select_dialog_item,
arrayList
);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
String devicehelper = arrayList.get(position).toString();
String[] device = devicehelper.split("\n");
String deviceName = device[0];
String deviceMAC = device[1];
boolean connected = connectWithDeviceNow(deviceMAC);
if (connected) {
System.out.println("Connected!");
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
} else {
System.out.println("Connection Failure");
Toast.makeText(getApplicationContext(), "Connection Failure", Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), "Mit Gerät " + deviceName + " verbunden", Toast.LENGTH_LONG);
}
});
}
public boolean connectWithDeviceNow(String MAC) {
// create device and set the MAC address
BluetoothDevice device = bluetooth.getRemoteDevice(MAC);//mydeviceaddress
System.out.println("devicename: " + device + device.getName());
bluetooth.cancelDiscovery();
try {
btSocket = createBluetoothSocket(device);
} catch (IOException e) {
System.out.println("socket creation failed");
return false;
}
// Establish the Bluetooth socket connection.
try {
btSocket.connect();
System.out.println("mit geraet verbunden");
} catch (IOException e) {
System.out.println("geraet nicht in eichweite");
try {
btSocket.close();
} catch (IOException e2) {
System.out.println("Socket closing doesnt work!");
}
return false;
}
// System.out.println("btconn is: "+btconn);
mConnectedThread = new ConnectedThread(btSocket);
mConnectedThread.start();
return true;
}
}