Bluetooth vom remoteDevice auf den Stream

Paddy.

Aktives Mitglied
Ich wollte über eine Bluetooth-Verbindungen Daten schicken.
Also lasse ich mittels deviceDiscovered alle Geräte auflisten.
und nun wollte ich eine der RemoteDevice auswählen und da eine Verbindung aufbauen, da ja gegebenfalls mehre Bluetooth-Geräte aufgefunden werden.

Also Serverseitig wäre das durch
Java:
StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open("btspp://localhost:123456789");
                StreamConnection connection = (StreamConnection)scn.acceptAndOpen();//blockierrender Aufruf
                is=connection.openDataInputStream();
                os=connection.openDataOutputStream();
kein Problem aber wie kann ich dann mit dem Verbinden???:L
 

Chéfkóch

Mitglied
Wenn ich mich richtig erinnere musst du mit dem DiscoveryAgent nach deinem Service suchen.
Mit ServiceRecord.html#getConnectionURL(int, boolean) bekommst du dann die ConnectionURL.

Ich hab mir selbst mal ne API geschrieben, die das ganze für mich erledigt. Leider ohne Threads und nur mit einmaligem Senden und Empfangen. Wenn ich mal Zeit habe bring ich die mal auf Vordermann und stell Sie dann hier zur Verfügung.

Bis dahin viel Glück :bae:
 

Paddy.

Aktives Mitglied
Also einmaliges Senden emfangen reich mir in dem Fall eh :)
Ich werde mir das an schauen und testen und hoffen das bald meine Java Mobile :rtfm: kommen:)

Aber wäre toll wenn die bekommen könnte;)
 

Paddy.

Aktives Mitglied
hab nun folgendes gefunden:

There's another way to connect to a service. If you don't care which device offers the desired service, you can use DiscoveryAgent.selectService(). This method searches all nearby Bluetooth devices for the service indicated by a UUID, and if successful returns the service's connection URL.

Und dann folgendes drauß gebastelt ???:L
Java:
            LocalDevice dev=LocalDevice.getLocalDevice();
            DiscoveryAgent agent = dev.getDiscoveryAgent();
            String con=agent.selectService(new UUID("123456789",false), ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

Nun ist in con eine ConnectionString
 
Zuletzt bearbeitet:

Chéfkóch

Mitglied
So kannst dus natürlich auch machen.
Da sparst du dir einen Haufen Arbeit mit dem Suchen der RemoteDevices und deren ServiceRecord.
Musst eben nur die UUID deines Services kennen :)
 

Paddy.

Aktives Mitglied
na die UUID ist ja kein Problem die denke ich mir ja selbst aus :)

Ist selectService ein blockierender Aufruf???:L und kann ich davon ausgehen das ich danach direkt
Java:
StreamConnection strCon = (StreamConnection)Connector.open(con);
is=strCon.openDataInputStream();
os=strCon.openDataOutputStream();
aufrufen ???:L und was ist wenn ich 2 Geräte also "Server laufen hab" :( irgendwie merkwürtig.
Wobei Broadcast mäsig wohl an alle gesendet!?
 

Chéfkóch

Mitglied
Ich gehe mal davon aus, dass selectService ein blockierender Aufruf ist.
Eine Verbindung kannst du so wie in deinem Code aufbauen.
Wenn ich mich recht erinnere hat
Java:
strCon.openDataInputStream()
bei mir nicht funktioniert. Ich musste explizit nochmal
Java:
new DataInputStream(strCon.openInputStream())
aufrufen. Gleiches galt für den OutputStream. Aber versuchs erst mal so wie von dir gepostet.
Wenn du den "Server" auf mehreren Geräten laufen hast wird auch der Erstbeste verwendet. Siehe dein Zitat
If you don't care which device offers the desired service...
 

Paddy.

Aktives Mitglied
Ja wer lesen kann...
:toll:

Ja das Problem mit der Auswahl löse ich sobald mein Programm sich 1000000000000mal verkauft hat so das ich befürchten muss das 2 Server in unmittelbarer Nähe laufen:D



Also wenn ich das auf dem PC laufen lasse funktioniert das Übertragen ohne Probleme.
Allerdings im Praxistest will der kein Gerät finden???:L
 
Zuletzt bearbeitet:

Chéfkóch

Mitglied
"Kennen" sich die beiden geräte denn?
Je nach Implementierung der JSR 82 API kann das nämlich dazu führen das dein Service nicht gefunden wird.

Auszug aus der BlueCove-API
Java:
public String selectService(UUID uuid, int security, boolean master) throws BluetoothStateException {
                ...
                RemoteDevice[] devs = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
                for (int i = 0; (devs != null) && (i < devs.length); i++) {
                        ServiceRecord sr = findServiceOnDevice(uuid, devs[i]);
                        if (sr != null) {
                                return sr.getConnectionURL(security, master);
                        }
                }
                devs = agent.retrieveDevices(DiscoveryAgent.CACHED);
                for (int i = 0; (devs != null) && (i < devs.length); i++) {
                        ServiceRecord sr = findServiceOnDevice(uuid, devs[i]);
                        if (sr != null) {
                                return sr.getConnectionURL(security, master);
                        }
                }
                ...
}
 

Paddy.

Aktives Mitglied
???:LWas verstehst du unter kennen?
also die beide Geräte haben sich gegenseitig unter meine geräte drin stehen.

Also ich hab nun die ganze select manuel gemacht:
Device suchen...
alle device auf dienste suchen und dann vom gefundenen Dienst die con auslesen:

Java:
public String selectService(){
        try {
            LocalDevice ld = LocalDevice.getLocalDevice();
            agent=ld.getDiscoveryAgent();
            agent.startInquiry(DiscoveryAgent.GIAC, this);
        } catch (BluetoothStateException ex) {
            showError("selectService\n"+ex.getMessage());
        }
        for(int i=0;i<100;i++){ //Timeout nach 100sekunden
            try {
                if(!searchKomplett)  Thread.sleep(1000);
            } catch (InterruptedException ex) {
                showError("Thread\n"+ex.getMessage());
            }
            if(conURL!=null) return conURL;
        }
        return null;
    }

mit folgende code im Interface (showError zeigt das ganze in nem alert an zum debuggen)
Java:
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
          rds.addElement(rd);   
    }

    public void servicesDiscovered(int i, ServiceRecord[] srs) {

        conURL=srs[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
    }

    public void serviceSearchCompleted(int i, int i1) {    }

    public void inquiryCompleted(int type) {
       if(type==DiscoveryListener.INQUIRY_COMPLETED){     
       try {
                UUID[] uuids={new UUID("123456789",false)};    //analog zu server btspp://localhost:123456789
                for(int i=0;i<rds.size();i++){
                    RemoteDevice rd=(RemoteDevice) rds.elementAt(i);
                    bts.btstatus = "Dienste "+rd.getFriendlyName(false);
                    bts.repaint();
                    agent.searchServices(null, uuids, rd, this);
                }
            }catch (BluetoothStateException ex) {
                showError("Dienstsuche\n"+ex.getMessage());              <<<FEHLER ex.getMEssage=null
            }catch (IOException ex) {
                showError("IO\n"+ex.getMessage());     
           }
        }
    }

der fehler ist dann wie im code an der stelle???:L
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
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
H WIFI, Bluetooth und NFC Verbindung überwachen Android & Cross-Platform Mobile Apps 1
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
K Java ME Bluetooth verbindung parameter Android & Cross-Platform Mobile Apps 3
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
G Bluetooth Verbindung zwischen Handy und PC Android & Cross-Platform Mobile Apps 5
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
G Bluetooth Verbindung Android & Cross-Platform Mobile Apps 2
J Bluetooth Stick und Lego Mindstorms Android & Cross-Platform Mobile Apps 2
O Bluetooth Verbindung zwischen 2 Handys Android & Cross-Platform Mobile Apps 5
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

Ähnliche Java Themen

Neue Themen


Oben