Per Bluetooth, HEX Werte senden und empfangen

J

Javist

Neues Mitglied
0
Hallo, ich bin kpl. neu in der Androide-Welt und komme von der C# Ecke, und am Anfangen direkt eine Nuss für mich.

Aber erst das Positive

Ich finde ein Bluetoothgerät und kann es in einer ListView anzeigen.

PHP:
public void verb (View view){
         if (!BA.isEnabled()) {
            Intent Verb = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(Verb, 0);
            Toast.makeText(getApplicationContext(),"Turned on" ,Toast.LENGTH_LONG).show();
  
         }
        else{
            Toast.makeText(getApplicationContext(),"Already on",
                    Toast.LENGTH_LONG).show();
        }
     }
 
    public void suchen (View view){
        Intent Suchen = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(Suchen, 0);
        pairedDevices = BA.getBondedDevices();
        ArrayList list = new ArrayList();
        for (BluetoothDevice bt : pairedDevices)
            list.add(bt.getName()+"\n"+bt.getAddress());
         Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show();
        final ArrayAdapter adapter = new ArrayAdapter
                (this,android.R.layout.simple_list_item_1, list);
        LV.setAdapter(adapter);
 
    }
In einer Eigenen Klasse habe ich das

PHP:
  private class AcceptThread extends Thread {
        private final BluetoothServerSocket mmServerSocket;
         public AcceptThread() {
            // 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 = BA.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);
                    mmServerSocket.close();
                    break;
                }
            }
        }
         /** Will cancel the listening socket, and cause the thread to finish. */
        public void cancel() {
            try {
                mmServerSocket.close();
            } catch (IOException e) { }
        }
    }
    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() {
            // Cancel discovery because it will slow down the connection.
            BA.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);
        }
         /** Will cancel an in-progress connection, and close the socket. */
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }
    private class ConnectedThread extends Thread {
        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);
                    // Send the obtained bytes to the UI Activity.
                    mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                            .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) { }
        }
    }
 
}
Ja genau das ist das Grundgerüst von der Androide Developer Seite.

Ich habe schon viele Bsp gelesen, und bin eher verwirrt als das es mir geholfen hat. Vorallem weiß ich nicht wo was hin kommt..

Was ich vorhabe, ich finde mein Gerät, es wird in der ListBox angezeigt. (Das klappt ja ;D) Über einen weitern Button verbinde ich mich dann, und sende die fest programmierten Hex-Werte, die Antwort wird dann in einem Label angezeigt. Achso der Befehl sollte alle 0,5s erneut gesendet werden.

Hätte jmd Lust mich zu Lotzen.

Danke
 
Hallo, ist die Frage so triviale?
 

Ähnliche Themen

5
Antworten
22
Aufrufe
1.445
590239
5
R
  • RalfKahl
Antworten
10
Aufrufe
327
RalfKahl
R
B
Antworten
6
Aufrufe
1.062
jogimuc
J
Zurück
Oben Unten