Bluetooth App zeigt fehler!!!

  • 3 Antworten
  • Letztes Antwortdatum
T

Techniker2013

Neues Mitglied
0
Hallo liebe Mitglieder,

ich muss ein Projekt für die Schule realisieren, welches sagt, dass ich mit einer eignen App via Bluetooth und Accelerometer zwei Schrittmotore ansteuere.
Mein Partner und ich haben die Aufgaben geteilt, so dass ich mich um die App kümmere. Da ich noch relativ neu in der Androidprogrammierung bin tue ich mich da noch schwer und sitze schon einige Zeit an der App.

Das Layout ist einfach gestaltet: 3 ToggleButton
Button 1 Programm starten (damit bestätige ich nur ob ein BluetoothAdapter vorhanden ist

Button 2(schaut nach pairedDevices, BroadcastReceiver und ....)

Button 3(Sollte mir meinen Wert des Accelerometer über Bluetooth verschicken)

Hier das Programm: bei der Zeile mHandler.obtainMessage() zeigt es mir einen Fehler an

***********************************************

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ToggleButton;

public class Icarus_V4 extends Activity implements OnItemClickListener{

//IV
private static final int REQUEST_ENABLE_BT = 0;

public static final UUID MY_UUID = UUID.fromString
("00001101-0000-1000-8000-00805F9B34FB");

public static final String NAME = null;


//IV Hardware
BluetoothAdapter mBluetoothAdapter;
Set<BluetoothDevice> pairedDevices;
BroadcastReceiver mReceiver;
//IV
ToggleButton ProgrammStarten,
VerbindungAufbauen,
SpielStarten;
ArrayAdapter<String> mArrayAdapter;
IntentFilter filter;
Intent discoverableIntent;

public BluetoothServerSocket mmServerSocket;

public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
ProgrammStartenOnClick();
VerbindungAufbauenOnClick();
SpielStartenOnClick();
}
private void Initializieren() {
// TODO Auto-generated method stub
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast. makeText(getApplicationContext(),
"Kein Bluetooth vorhanden, bitte einschalten",
Toast.LENGTH_LONG).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void ProgrammStartenOnClick() {
// TODO Auto-generated method stub
ProgrammStarten = (ToggleButton) findViewById(R.id.toggleButton1);
if (ProgrammStarten.isChecked()==true){
Initializieren();
}
}

private void EnablingDiscovery(){
discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra (BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity (discoverableIntent);
discoverableIntent.putExtra (BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult (discoverableIntent, 50);
}
private void getBondedDevices(){
pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add (device.getName() + "\n" + device.getAddress());
}
}
}
private void onReceive(){
// Create a BroadcastReceiver for ACTION_FOUND
mBluetoothAdapter. cancelDiscovery();
mBluetoothAdapter. startDiscovery();

mReceiver = new BroadcastReceiver() {
public void onReceive (Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra
(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add (device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
EnablingDiscovery();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver (mReceiver, filter); // Don't forget to unregister during onDestroy
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver (mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
registerReceiver (mReceiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver (mReceiver, filter);
mBluetoothAdapter. cancelDiscovery();
}

private void VerbindungAufbauenOnClick(){
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
VerbindungAufbauen = (ToggleButton) findViewById(R.id.toggleButton2);

if (!mBluetoothAdapter.isEnabled()&&
ProgrammStarten.isChecked()==true&&
VerbindungAufbauen.isChecked()==true){
getBondedDevices();
onReceive();
}
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub

}
private void SpielStartenOnClick() {
// TODO Auto-generated method stub
ProgrammStarten = (ToggleButton) findViewById(R.id.toggleButton2);
VerbindungAufbauen = (ToggleButton) findViewById(R.id.toggleButton2);
SpielStarten = (ToggleButton) findViewById(R.id.toggleButton3);
if(SpielStarten.isChecked()==true&&VerbindungAufbauen.isChecked()==true&&ProgrammStarten.isChecked()==true){

}
}
private class AcceptThread extends Thread {



public AcceptThread() {
// Use a temporary object that is later assigned to mmServerSocket,
// because mmServerSocket is final.
BluetoothServerSocket tmp = null;
try {
BluetoothAdapter mAdapter = null;
// MY_UUID is the app’s UUID string, also used by the client code.
tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) { }
mmServerSocket = tmp;
}

public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned.
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// if a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread).
manageConnectedSocket(socket);
try {
mmServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}

private void manageConnectedSocket(BluetoothSocket socket) {
// TODO Auto-generated method stub

}

/** Will cancel the listening socket, and cause the thread to finish. */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}//Ende des Accept Thread
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final.
BluetoothSocket tmp = null;
mmDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice.
try {
// MY_UUID is the app’s UUID string, also used by the server code.
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}

public void run() {
BluetoothAdapter mAdapter = null;
// Cancel discovery because it will slow down the connection.
mAdapter.cancelDiscovery();

try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception.
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out.
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}

// Do work to manage the connection (in a separate thread).
manageConnectedSocket(mmSocket);
}

private void manageConnectedSocket(BluetoothSocket mmSocket2) {
// TODO Auto-generated method stub

}

/** Will cancel an in-progress connection, and close the socket. */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}// Ende des ConnectThread
private class ConnectedThread extends Thread {


private static final int MESSAGE_READ = 1;
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;

public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;

// Get the input and output streams, using temp objects because
// member streams are final.
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }

mmInStream = tmpIn;
mmOutStream = tmpOut;
}

public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()

// Keep listening to the InputStream until an exception occurs.
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
Object mHandler;

// Send the obtained bytes to the UI Activity.
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) //Hier ist der Fehler und ich weis nicht wie ich diesen heraus bekomme
The method obtainMessage(int, int, int, byte[]) is undefined for the type Object

.sendToTarget();
} catch (IOException e) {
break;
}
}
}

/* Call this from the main Activity to send data to the remote device. */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}

/* Call this from the main Activity to shutdown the connection. */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}// eoc
***************************************

Hier das Programm für den Accelerometer:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccelerometerAuslesen extends Activity implements SensorEventListener{

float values[];
Sensor accelerometer;
SensorManager mySensorManager;
TextView acceleration;

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

mySensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer=mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mySensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

acceleration=(TextView)findViewById(R.id.acceleration);
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
acceleration.setText("X: "+event.values[0]+
"/nY: "+ event.values[1]);
}
}

Ich lese damit nur den Wert der Aktuellenlage aus und zeige ihn in einem Text View
**********************************************************

Ich hoffe mir kann da jemand weiter helfen. ich komme einfach nicht weiter

bedanke mich jetzt schon herzlichst für eure Hilfe

Techniker2013
 
Ganz einfach:
mHandler ist bei dir vom Typ object...

Steht doch auch ganz klar in der Fehlermeldung.

Sollte es nicht lieber vom Typ Handler sein? !

lg. Dagobert
 
Danke war ein Anfängerfehler nicht genau zu lesen.

Dürfte ich fragen ob Sie eine Idee hätten wie/als was ich den Wert des Accelerometer über Bluetooth schicken könnte.
 
Auch dir kann ich nur empfehlen erst die Java Grundlagen zu lernen ;)
Sonst wirst du nicht ganz so weit kommen.

lg. Dagobert
 

Ähnliche Themen

G
Antworten
0
Aufrufe
132
Gerdchen07
G
G
Antworten
1
Aufrufe
384
Gerdchen07
G
G
Antworten
13
Aufrufe
599
Gerdchen07
G
L
Antworten
2
Aufrufe
555
Lexub
L
migi01
Antworten
26
Aufrufe
1.991
migi01
migi01
Zurück
Oben Unten