Java ME Bluetooth verbindung parameter

Knexi

Mitglied
Hallo

ich möchte gerne ein Programm schreiben mit dem man Bluetooth geräte suchen, mit ihnen verbinden, die dienste suchen und eine Nachricht senden kann.

Das Programm sieht im Moment so aus:

Java:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import java.util.Vector;
import java.io.*;
import javax.microedition.io.*;

/**
 * @author Knexi
 */
public class bt_test extends MIDlet implements
        Runnable,CommandListener,DiscoveryListener{
    private List deviceList;
    private List SRListe;
    private DiscoveryAgent agent;
    private Vector gefundeneGeraete=new Vector();
    private boolean aktiveSuche=false;
    private static final Command CMD_NAME=new Command("Gerätenamen",Command.ITEM,3);
    private static final Command CMD_EXIT=new Command("Ende",Command.EXIT,0);
    private static final Command CMD_INQUIRY=new Command("Inquiry",Command.ITEM,1);
    private static final Command CMD_SIMPLE=new Command("Einfache Suche",Command.ITEM,2);
    private static final Command CMD_DIENST=new Command("Dienst Suche",Command.ITEM,2);
    private static final Command CMD_SEND=new Command("Senden",Command.ITEM,4);
    protected void pauseApp() {}
    
     protected void destroyApp(boolean arg0) {}
    
    protected void startApp() {new Thread(this).start();}

    public void commandAction(Command c,Displayable d)
    {
        if(c==CMD_NAME)
        {
            if(aktiveSuche)
            {
                agent.cancelInquiry(this);
                aktiveSuche=false;
            }
            if(deviceList.getSelectedIndex()>=0)
                ZeigeInfo((RemoteDevice)gefundeneGeraete.elementAt(deviceList.getSelectedIndex()));
        }
        if(c==CMD_EXIT)
        {
            agent.cancelInquiry(this);
            aktiveSuche=false;
            notifyDestroyed();
        }
        if(c==CMD_INQUIRY)
        {
            Alert info=new Alert("Inquiry","Suche gestartet.",null,AlertType.INFO);
            info.setTimeout(3000);
            Display.getDisplay(this).setCurrent(info);
            try
            {
                agent.startInquiry(DiscoveryAgent.GIAC,this);
            }catch(BluetoothStateException e){//behandeln
            }
            aktiveSuche=true;
        }
        if(c==CMD_SIMPLE)
        {
            EinfacheGeraeteSuche();
        }
        if(c==CMD_DIENST)
        {
            if(aktiveSuche)
            {
                agent.cancelInquiry(this);
                aktiveSuche=false;
            }
            if(deviceList.getSelectedIndex()>=0)
                dienstSuche((RemoteDevice)gefundeneGeraete.elementAt(deviceList.getSelectedIndex()));
            //Implementierung siehe weiter unten
        }
    }
    public void run()
    {
        try{
            LocalDevice local=LocalDevice.getLocalDevice();
            agent=local.getDiscoveryAgent();
        }catch(BluetoothStateException e){
            //Exception behandeln
        }
        deviceList=new List("Geräteliste",List.IMPLICIT);
        deviceList.addCommand(CMD_EXIT);
        deviceList.addCommand(CMD_INQUIRY);
        deviceList.addCommand(CMD_SIMPLE);
        deviceList.addCommand(CMD_NAME);
        deviceList.setCommandListener(this);
        Display.getDisplay(this).setCurrent(deviceList);
    }

    public void deviceDiscovered(RemoteDevice device,DeviceClass cod)
    {
        try
        {
            String friendlyName=device.getFriendlyName(true);
            deviceList.append(friendlyName, null);
            gefundeneGeraete.addElement(device);
        }catch(java.io.IOException e){
            //Exception behandeln
        }
    }

    public void inquiryCompleted(int type)
    {
        if(type==DiscoveryListener.INQUIRY_COMPLETED)
        {
            Alert info=new Alert("Inquiry","Suche beendet",null,AlertType.INFO);
            info.setTimeout(3000);
            Display.getDisplay(this).setCurrent(info);
        }
        else
        {
            Alert info=new Alert("Inquiry","Suche mit Fehler beendet.",null,AlertType.INFO);
            info.setTimeout(3000);
            Display.getDisplay(this).setCurrent(info);
        }
    }
    public void servicesDiscovered(int transID,ServiceRecord[] record)
    {
        for(int i=0;i<record.length;i++)
        {
            DataElement nameElement=(DataElement)record[i].getAttributeValue(0x100);
            if((nameElement==null)&&(nameElement.getDataType()==DataElement.STRING))
            {
                String name=(String)nameElement.getValue();
                SRListe.insert(0, name, null);

            }
        }
    }
    public void serviceSearchCompleted(int transID, int type)
    {
        if(type==DiscoveryListener.SERVICE_SEARCH_COMPLETED)
        {
            Alert info=new Alert("Dienstsuche","Suche beendet.",null,AlertType.INFO);
            info.setTimeout(3000);
            Display.getDisplay(this).setCurrent(info);
        }
        else
        {
            Alert info=new Alert("Dienstsuche","Suche mit Fehler beendet.",null,AlertType.INFO);
            info.setTimeout(3000);
            Display.getDisplay(this).setCurrent(info);
        }
    }

    private void EinfacheGeraeteSuche()
    {
        RemoteDevice[] list=agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
        if(list!=null)
        {
            for(int i=0;i<list.length;i++)
            {
                try{
                    String friendlyName=list[i].getFriendlyName(true);
                    deviceList.append(friendlyName,null);
                    gefundeneGeraete.addElement(list[i]);
                }catch(java.io.IOException e){
                    //Exception behandeln
                }
            }
        }
        list=agent.retrieveDevices(DiscoveryAgent.CACHED);
        if(list!=null)
        {
            for(int i=0;i<list.length;i++)
            {
                try{
                    String friendlyName=list[i].getFriendlyName(true);
                    deviceList.append(friendlyName,null);
                    gefundeneGeraete.addElement(list[i]);
                }catch(java.io.IOException e){
                    //Exception behandeln
                }
            }
        }
    }
    private void ZeigeInfo(RemoteDevice device)
    {
        String daten="leer";
        try
        {
            daten=device.getBluetoothAddress()
                    +"\n"
                    +device.getFriendlyName(false);
        }catch(java.io.IOException e){
            //Exception behandeln
        }
        Alert info=new Alert("Geräteinformationen",daten,null,AlertType.INFO);
        info.setTimeout(Alert.FOREVER);
        Display.getDisplay(this).setCurrent(info);
    }
    private void dienstSuche(RemoteDevice device)
    {
        try
        {
            UUID[] uuidList=new UUID[1];
            uuidList[0]= new UUID("25A3F389A8574a76AFB346FE036F95CE",false);
            int[] attrList=new int[1];
            attrList[0]=0x100;
            agent.searchServices(attrList, uuidList,device,this);
        }catch(java.io.IOException e){
        }     
    }
    private void sendeNachricht(String connectionString)
    {
        try
        {
            if(connectionString!=null)
            {
                try
                {
                    StreamConnection connection=(StreamConnection)Connector.open(connectionString);
                    OutputStream out=connection.openOutputStream();
                    out.write("Hallo".getBytes());
                    out.flush();
                }
                catch(IOException e)
                {
                }
            }
        }
        catch(Exception e)
        {
        }
    }
}

Ich möchte nun noch die restlichen Methoden wie zB sendeNachricht() oder dienstSuche() oben bei einem Comment verwenden, weis aber nicht was ich bei den restlichen Methoden als Parameter mitgeben soll oder muss


MfG Knexi
 

The_S

Top Contributor
Sprich Copy&Paste und du hast keine Ahnung was es tut ;) ? Das ist schon einmal immer ungut. dienstSuche rufst du ja in deinem Programm schon auf. sendeNachricht erwartet eine Connection URL um den richtigen Output-Stream zu ergattern. Wie sich der zusammensetzt, solltest du in der API-Doc nachlesen können.
 

Knexi

Mitglied
Hast du zufällig einen Guide wo das suchen und verbinden mit einem Bluetooth Gerät beschrieben ist? Habe bisher nichts Brauchbares gefunden.

MfG Knexi
 

The_S

Top Contributor
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
H WIFI, Bluetooth und NFC Verbindung überwachen Android & Cross-Platform Mobile Apps 1
G Bluetooth Verbindung zwischen Handy und PC Android & Cross-Platform Mobile Apps 5
G Bluetooth Verbindung Android & Cross-Platform Mobile Apps 2
O Bluetooth Verbindung zwischen 2 Handys Android & Cross-Platform Mobile Apps 5
K Androidstudio Bluetooth connect Android & Cross-Platform Mobile Apps 6
xXSkyWalkerXx1 Android Bluetooth LE - Frage zur Funktionalität Android & Cross-Platform Mobile Apps 26
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
D Android Bluetooth Chat Example modifizieren Android & Cross-Platform Mobile Apps 6
T Musik über Bluetooth streamen Android & Cross-Platform Mobile Apps 2
A Zweite Kamera an HTC und per Bluetooth kommunizieren? Android & Cross-Platform Mobile Apps 2
W Bluetooth und obex Package nutzen Android & Cross-Platform Mobile Apps 4
P Bluetooth vom remoteDevice auf den Stream Android & Cross-Platform Mobile Apps 9
K Java ME Bluetooth geräte suchen Android & Cross-Platform Mobile Apps 2
K Java ME J2ME Bluetooth - bluesoleil,bluecove, JSR82... ? Android & Cross-Platform Mobile Apps 6
S Wie teste ich meine Bluetooth Anwendung Android & Cross-Platform Mobile Apps 4
G Bluetooth LocalDevice.isPowerOn() meldet false Android & Cross-Platform Mobile Apps 2
R Bluetooth und Sun Wireless Toolkit 2.5.2 Android & Cross-Platform Mobile Apps 3
W Datenbankzugriff, WLAN, Bluetooth Android & Cross-Platform Mobile Apps 2
T Java Bluetooth Kalender Android & Cross-Platform Mobile Apps 2
C How to send AT-Commands via Bluetooth using JSR-82 Android & Cross-Platform Mobile Apps 3
J Bluetooth Stick und Lego Mindstorms Android & Cross-Platform Mobile Apps 2
D Bluetooth Pairing dem Programm ueberlassen? Android & Cross-Platform Mobile Apps 3
L HTTP via Bluetooth Android & Cross-Platform Mobile Apps 2
D Start/Stop Bit per Bluetooth von PC zu PC, oder PCtoHandy Android & Cross-Platform Mobile Apps 2
M bluetooth? Android & Cross-Platform Mobile Apps 3
missy72 Kotlin SSH Verbindung mit JSch Android & Cross-Platform Mobile Apps 5
J Android VPN Verbindung herstellen? Android & Cross-Platform Mobile Apps 4
B Android TCP-Verbindung zum Server über welche Prozess auslagerung nutzen? Android & Cross-Platform Mobile Apps 1
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
H Android 3G TCP Socket Verbindung zum PC durch NAT Android & Cross-Platform Mobile Apps 8
T Android MSSQL Verbindung herstellen - Android Studio Android & Cross-Platform Mobile Apps 2
M Android Server-Client-Verbindung in Android-App mit Sockets aufbauen Android & Cross-Platform Mobile Apps 5
W Android HTTPS-Verbindung mit Client-Authentifizierung Android & Cross-Platform Mobile Apps 0
R Socket Verbindung AsycTask Android & Cross-Platform Mobile Apps 5
F Android USB Verbindung zu Windows Programm Android & Cross-Platform Mobile Apps 3
U Android Https-Verbindung Android & Cross-Platform Mobile Apps 2
N Java ME Server-Client Verbindung über Wifi Android & Cross-Platform Mobile Apps 6
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
N Handy -PC Verbindung Android & Cross-Platform Mobile Apps 2
N Blutooz-Verbindung ... ich schaffs nicht Android & Cross-Platform Mobile Apps 5
F Server - Client Verbindung mit Java ME Android & Cross-Platform Mobile Apps 3
K HTTP-Verbindung mit J2ME.... Android & Cross-Platform Mobile Apps 2
OSchriever Auf onClick-Listener reagieren und Parameter übergeben (Android Studio) Android & Cross-Platform Mobile Apps 4
L R.raw als Parameter Android & Cross-Platform Mobile Apps 2
A Android Android Parameter für VM Android & Cross-Platform Mobile Apps 7

Ähnliche Java Themen

Neue Themen


Oben