Search Results

Search found 3 results on 1 pages for 'hugi'.

Page 1/1 | 1 

  • Bluetooth connection. Problem with sony ericsson.

    - by Hugi
    I have bt client and server. Then i use method Connector.open, client connects to the port, but passed so that my server does not see them. Nokia for all normal, but with sony ericsson i have this problem. On bt adapter open one port (com 5). Listings Client /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Vector; import javax.bluetooth.*; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; /** * @author ????????????? */ public class Client extends MIDlet implements DiscoveryListener, CommandListener { private static Object lock=new Object(); private static Vector vecDevices=new Vector(); private ServiceRecord[] servRec = new ServiceRecord[INQUIRY_COMPLETED]; private Form form = new Form( "Search" ); private List voteList = new List( "Vote list", List.IMPLICIT ); private List vote = new List( "", List.EXCLUSIVE ); private RemoteDevice remoteDevice; private String connectionURL = null; protected int stopToken = 255; private Command select = null; public void startApp() { //view form Display.getDisplay(this).setCurrent(form); try { //device search print("Starting device inquiry..."); getAgent().startInquiry(DiscoveryAgent.GIAC, this); try { synchronized(lock){ lock.wait(); } }catch (InterruptedException e) { e.printStackTrace(); } //device count int deviceCount=vecDevices.size(); if(deviceCount <= 0) { print("No Devices Found ."); } else{ remoteDevice=(RemoteDevice)vecDevices.elementAt(0); print( "Server found" ); //create uuid UUID uuid = new UUID(0x1101); UUID uuids[] = new UUID[] { uuid }; //search service print( "Searching for service..." ); getAgent().searchServices(null,uuids,remoteDevice,this); } } catch( Exception e) { e.printStackTrace(); } } //if deivce discovered add to vecDevices public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { //add the device to the vector try { if(!vecDevices.contains(btDevice) && btDevice.getFriendlyName(true).equals("serverHugi")){ vecDevices.addElement(btDevice); } } catch( IOException e ) { } } public synchronized void servicesDiscovered(int transID, ServiceRecord[] servRecord) { //for each service create connection if( servRecord!=null && servRecord.length>0 ){ print( "Service found" ); connectionURL = servRecord[0].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false); //connectionURL = servRecord[0].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT,false); } if ( connectionURL != null ) { showVoteList(); } } public void serviceSearchCompleted(int transID, int respCode) { //print( "serviceSearchCompleted" ); synchronized(lock){ lock.notify(); } } //This callback method will be called when the device discovery is completed. public void inquiryCompleted(int discType) { synchronized(lock){ lock.notify(); } switch (discType) { case DiscoveryListener.INQUIRY_COMPLETED : print("INQUIRY_COMPLETED"); break; case DiscoveryListener.INQUIRY_TERMINATED : print("INQUIRY_TERMINATED"); break; case DiscoveryListener.INQUIRY_ERROR : print("INQUIRY_ERROR"); break; default : print("Unknown Response Code"); break; } } //add message at form public void print( String msg ) { form.append( msg ); form.append( "\n\n" ); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } //get agent :))) private DiscoveryAgent getAgent() { try { return LocalDevice.getLocalDevice().getDiscoveryAgent(); } catch (BluetoothStateException e) { throw new Error(e.getMessage()); } } private synchronized String getMessage( final String send ) { StreamConnection stream = null; DataInputStream in = null; DataOutputStream out = null; String r = null; try { //open connection stream = (StreamConnection) Connector.open(connectionURL); in = stream.openDataInputStream(); out = stream.openDataOutputStream(); out.writeUTF( send ); out.flush(); r = in.readUTF(); print( r ); in.close(); out.close(); stream.close(); return r; } catch (IOException e) { } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { } } return r; } } private synchronized void showVoteList() { String votes = getMessage( "c_getVotes" ); voteList.append( votes, null ); select = new Command( "Select", Command.OK, 4 ); voteList.addCommand( select ); voteList.setCommandListener( this ); Display.getDisplay(this).setCurrent(voteList); } private synchronized void showVote( int index ) { String title = getMessage( "c_getVote_"+index ); vote.setTitle( title ); vote.append( "Yes", null ); vote.append( "No", null ); vote.setCommandListener( this ); Display.getDisplay(this).setCurrent(vote); } public void commandAction( Command c, Displayable d ) { if ( c == select && d == voteList ) { int index = voteList.getSelectedIndex(); print( ""+index ); showVote( index ); } } } Use BlueCove in this program. Server /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication4; import java.io.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import javax.bluetooth.*; import javax.microedition.io.*; import javaapplication4.Connect; /** * * @author ????????????? */ public class SampleSPPServer { protected static int endToken = 255; private static Lock lock=new ReentrantLock(); private static StreamConnection conn = null; private static StreamConnectionNotifier streamConnNotifier = null; private void startServer() throws IOException{ //Create a UUID for SPP UUID uuid = new UUID("1101", true); //Create the service url String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server"; //open server url StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString ); while ( true ) { Connect ct = new Connect( streamConnNotifier.acceptAndOpen() ); ct.getMessage(); } } /** * @param args the command line arguments */ public static void main(String[] args) { //display local device address and name try { LocalDevice localDevice = LocalDevice.getLocalDevice(); localDevice.setDiscoverable(DiscoveryAgent.GIAC); System.out.println("Name: "+localDevice.getFriendlyName()); } catch( Throwable e ) { e.printStackTrace(); } SampleSPPServer sampleSPPServer=new SampleSPPServer(); try { //start server sampleSPPServer.startServer(); } catch( IOException e ) { e.printStackTrace(); } } } Connect /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication4; import java.io.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import javax.bluetooth.*; import javax.microedition.io.*; /** * * @author ????????????? */ public class Connect { private static DataInputStream in = null; private static DataOutputStream out = null; private static StreamConnection connection = null; private static Lock lock=new ReentrantLock(); public Connect( StreamConnection conn ) { connection = conn; } public synchronized void getMessage( ) { Thread t = new Thread() { public void run() { try { in = connection.openDataInputStream(); out = connection.openDataOutputStream(); String r = in.readUTF(); System.out.println("read:" + r); if ( r.equals( "c_getVotes" ) ) { out.writeUTF( "vote1" ); out.flush(); } if ( r.equals( "c_getVote_0" ) ) { out.writeUTF( "Vote1" ); out.flush(); } out.close(); in.close(); } catch (Throwable e) { } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } try { connection.close(); } catch( IOException e ) { } } } }; t.start(); } }

    Read the article

  • j2me bluetooth client. Function startInquiry nothing found.

    - by Hugi
    I develop simple j2me bluetooth client and have problem with bluetooth device search. Function startInquiry nothing found. Client : nokia 5220 Server : my pc with bluetooth adapter All bluetooth devices is on. /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.microedition.midlet.*; import javax.bluetooth.*; import java.util.Vector; import javax.microedition.lcdui.*; /** * @author ????????????? */ public class Midlet extends MIDlet implements DiscoveryListener { private static Vector vecDevices=new Vector(); private static String connectionURL=null; private LocalDevice localDevice; private DiscoveryAgent agent; private RemoteDevice remoteDevice; private RemoteDevice[] devList; private Display display; private Form form; public void startApp() { display = Display.getDisplay(this); form = new Form( "Client" ); try { localDevice = LocalDevice.getLocalDevice(); } catch( BluetoothStateException e ) { e.printStackTrace(); } form.append("Address: "+localDevice.getBluetoothAddress()+"\n\n"); form.append("Name: "+localDevice.getFriendlyName()+"\n\n"); try { agent = localDevice.getLocalDevice().getDiscoveryAgent(); form.append("Starting device inquiry... \n\n"); boolean si = agent.startInquiry(DiscoveryAgent.GIAC, this); if ( si ) { form.append("true"); } else { form.append("false"); } } catch( BluetoothStateException e ) { } int deviceCount = vecDevices.size(); if(deviceCount <= 0){ form.append("No Devices Found ."); } else{ //print bluetooth device addresses and names in the format [ No. address (name) ] form.append("Bluetooth Devices: "); for (int i = 0; i < deviceCount; i++) { remoteDevice=(RemoteDevice)vecDevices.elementAt(i); form.append( remoteDevice.getBluetoothAddress() ); } } display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { //add the device to the vector if(!vecDevices.contains(btDevice)){ vecDevices.addElement(btDevice); } } public void inquiryCompleted(int discType) { } //implement this method since services are not being discovered public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { if(servRecord!=null && servRecord.length>0){ connectionURL=servRecord[0].getConnectionURL(0,false); } } //implement this method since services are not being discovered public void serviceSearchCompleted(int transID, int respCode) { } }

    Read the article

1