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:
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
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