Bluetooth-Verbindung geht nicht

  • 1 Antworten
  • Neuester Beitrag
Diskutiere Bluetooth-Verbindung geht nicht im Android App Entwicklung im Bereich Betriebssysteme & Apps.
H

HagenKoeckritz

Neues Mitglied
Hallo zusammen,

ich habe folgendes Problem:

ich möchte in meiner App mich mit deinem Bluetooth-Gerät verbinden. Wollte da meine Tastatur nehmen.

Habe diese über Handy gekoppelt.

Jetzt möchte ich den Datenstrom in meiner App einlesen. Leider kann er keine Socketverbindung aufbauen.

Kann mir jemand einen Leitfaden senden was ich nacheinander machen muss. Vielleicht hängt es ja schon an der Herangehensweise.

hier mein Aufrufender Code:
Code:
package com.phonegap.BluetoothPlugin;

import android.os.Bundle;

import com.phonegap.DroidGap;

/**
 * BluetoothPlugin PhoneGap application.
 * 
 * @author jamesgeisler
 */
public class BluetoothPlugin extends DroidGap {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// super.loadUrl("file:///android_asset/www/index.html");

		Bluetooth bt = new Bluetooth();
		bt.connect(bt.getBtDevice("00:07:61:F6:3A:D2"));
	}

	public void onDestroy() {
		super.onDestroy();
	}
}
und hier die Klasse:
Code:
package com.phonegap.BluetoothPlugin;

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

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

/** 
 * @author David 
 * Class for handling the Bluetooth communication between two devices 
 */ 
public class Bluetooth { 
     
    String TAG = Bluetooth.class.getSimpleName(); 
     
    private final BluetoothAdapter bluetoothAdapter; 
     
    private ServerThread serverThread; 
    private ClientThread clientThread; 
    private ConnectedThread connectedThread; 
     
     
    public Bluetooth() { 
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    } 
     
    public String getMAC () { 
        // Returns the MAC of the bluetoothAdapter 
        return bluetoothAdapter.getAddress(); 
    } 
     
    public boolean checkMac (String mac) { 
        return BluetoothAdapter.checkBluetoothAddress(mac); 
    } 
     
    public BluetoothDevice getBtDevice (String mac) { 
        return bluetoothAdapter.getRemoteDevice(mac); 
    } 
     
    
     
    public synchronized void startServer() { 
        serverThread = new ServerThread(); 
        serverThread.start(); 

        // mStateConnecting = true; 

        Log.i("Bluetooth start()", "ServerThread started"); 
    } 

    public synchronized void connect(BluetoothDevice device) { 
        clientThread = new ClientThread(device); 
        clientThread.start(); 

        // mStateConnecting = true; 
        Log.i("Bluetooth connect()", "ClientThread started"); 
    } 

    public synchronized void connected(BluetoothSocket socket, 
            BluetoothDevice device) { 

        if (socket == null) { 
            Log.e("Bluetooth connected()", "ERROR: socket ist null"); 
        } 

        connectedThread = new ConnectedThread(socket); 
        connectedThread.start(); 

        // smStateConnecting = false; 
        if (connectedThread == null) { 
            Log.e("Bluetooth connected()", "ERROR: connectedThread ist null"); 
        } else { 
            Log.i("Bluetooth connected()", 
                    "Verbindung hergestellt - ConnectedThread started"); 
        } 

       
    } 

    public void write(byte[] out) { 
        // Create temporary object 
        ConnectedThread r; 
        // Synchronize a copy of the ConnectedThread 
        synchronized (this) { 
            r = connectedThread; 
        } 
        // Perform the write unsynchronized 
        if (connectedThread == null) { 
            Log.e("Write", 
                    "ERROR: connectedThread wurde nicht gesetzt / r zeigt auf null"); 
        } else { 
            r.write(out); 
            String testAusgabe = new String(out); 
            Log.i("Write", "folgende Daten wurden geschrieben:" + testAusgabe); 
        } 
    } 

    private class ServerThread extends Thread { 

        private final BluetoothServerSocket mServerSocket; 

        public ServerThread() { 
            // Use a temporary object that is later assigned to mmServerSocket, 
            // because mmServerSocket is final 
            BluetoothServerSocket tmp = null; 
            try { 
                // MY_UUID is the app's UUID string, also used by the client 
                // code 
                tmp = bluetoothAdapter 
                        .listenUsingInsecureRfcommWithServiceRecord( 
                                "SCDActivity", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
                Log.i("Bluetooth ServerThread run()", 
                        "tmp socket wurde gesetzt"); 
            } catch (IOException e) { 
                Log.e("Bluetooth ServerThread run()", 
                        "ERROR Try Catch IOException MYUUID"); 
                Log.e("Bluetooth ServerThread run()", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB").toString()); 
            } 
            mServerSocket = tmp; 
        } 

        public void run() { 
            // Keep listening until exception occurs or a socket is returned 

            BluetoothSocket socket = null; 

            while (true) { 
                Log.i("Bluetooth ServerThread run()", "waiting for socket"); 
                try { 
                    socket = mServerSocket.accept(); 
                    Log.i("Bluetooth ServerThread run()", "socket is returned"); 
                } catch (IOException e) { 
                    Log.e("Bluetooth ServerThread run()", 
                            "ERROR Try Catch IOException BREAK"); 
                    break; 
                } 
                // If a connection was accepted 
                if (socket != null) { 

                    Log.i("Bluetooth ServerThread run()", "Socket is not null"); 

                    // Do work to manage the connection (in a separate thread) 
                    // synchronized (Bluetooth.this) { 
                    // if (mStateConnecting) { 
                    Log.i("Bluetooth ServerThread run()", 
                            "ConnectedServer wurde aufgerufen in ServerThread"); 

                    connected(socket, socket.getRemoteDevice()); 
                    // mStateConnecting = false; 
                    // } 
                    // } 
                    Log.i("Bluetooth ServerThread run()", 
                            "Zweites Break wurde aufgerufen"); 
                    break; 
                } 
            } 
        } 
    } 

    private class ClientThread extends Thread { 
        private final BluetoothSocket mmSocket; 
        private final BluetoothDevice mmDevice; 

        public ClientThread(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.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
                Log.i("Bluetooth ClientThread run()", 
                        "tmp socket wurde gesetzt"); 
            } catch (IOException e) { 
                Log.e("Bluetooth ClientThread run()", 
                        "ERROR Try Catch IOException MYUUID"); 
            } 
            mmSocket = tmp; 
        } 

        public void run() { 
            // Cancel discovery because it will slow down the connection 
            bluetoothAdapter.cancelDiscovery(); 

            try { 
                // Connect the device through the socket. This will block 
                // until it succeeds or throws an exception 
                mmSocket.connect(); 
                Log.i("Bluetooth ClientThread run()", "Empfaenger Connected"); 
            } catch (IOException connectException) { 
                // Unable to connect; close the socket and get out 
            	Log.e("Bluetooth ClientThread run()", 
                        "------------socket aufbau gescheitert"); 
                try { 
                    mmSocket.close(); 
                } catch (IOException closeException) { 
                     
                } 
                Log.e("Bluetooth ClientThread run()", 
                        "Empfaenger unable to connect - es wird returned"); 
                return; 
            } 

            // Do work to manage the connection (in a separate thread) 
            connected(mmSocket, mmDevice); 
            Log.i("Bluetooth ClientThread run()", 
                    "Empfaenger hat connected thread aufgerufen"); 
        } 
    } 

    private class ConnectedThread extends Thread { 
        private final InputStream mmInStream; 
        private final OutputStream mmOutStream; 

        public ConnectedThread(BluetoothSocket 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); 
                    // Send the obtained bytes to the UI activity 
                    
                    Log.i("Bluetooth ConnectedThread run()", 
                            "es wurde zumindest versucht etwas zu lesen"); 

                } catch (IOException e) { 
                    Log.e("Bluetooth ConnectedThread run()", "disconnected", e); 
                    break; 
                } 
            } 
        } 

        // Call this from the main activity to send data to the remote device 
        public void write(byte[] bytes) { 
            try { 
                Log.i("Bluetooth ConnectedThread write()", 
                        "Es wurde etwas geschrieben..."); 
                mmOutStream.write(bytes); 
                mmOutStream.flush(); 
            } catch (IOException e) { 
                Log.e("Bluetooth ConnectedThread write()", "write failed", e); 
            } 
        } 
    } 
}
Ich hoffe mir kann da einer weiter helfen.

Danke schon mal.
 
H

HagenKoeckritz

Neues Mitglied
kann mir keine bei dem Problem helfen?
 
Ähnliche Themen - Bluetooth-Verbindung geht nicht Antworten Datum
8
5
1