Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

J2ME - getIPdevice

// Retrevie IP device

package org.socketdemo;

import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SocketDEMO extends MIDlet implements CommandListener
{
	protected SocketDEMO midlet = this;
	
	private Alert info;
	
	protected void destroyApp(boolean value) throws MIDletStateChangeException
	{
		notifyDestroyed();
	}

	protected void pauseApp()
	{

	}

	protected void startApp() throws MIDletStateChangeException
	{
		new Thread()
		{
			public void run()
			{
				SocketConnection socket = null;
				
				try
				{
					socket = (SocketConnection) Connector.open("socket://193.204.114.233:13");
					
					socket.openInputStream();
					
					info = new Alert("Info", "Current IP: " + socket.getLocalAddress() + "\nPort: " + socket.getLocalPort(), null, AlertType.INFO);
					info.setTimeout(Alert.FOREVER);
					info.setCommandListener(midlet);
					
					getDisplay().setCurrent(info);
				}
				catch(Exception error)
				{
					info = new Alert("Info", "Current IP: N/A\nPort: N/A", null, AlertType.INFO);
					info.setTimeout(Alert.FOREVER);
					info.setCommandListener(midlet);
					
					getDisplay().setCurrent(info);
				}
				finally
				{
					if(socket != null)
					{
						try
						{
							socket.close();
						}
						catch(Exception error)
						{
							
						}
					}
				}
			}
		}.start();
	}
	
	protected Display getDisplay()
	{
		return Display.getDisplay(this);
	}

	public void commandAction(Command cmd, Displayable dsp)
	{
		if(cmd == Alert.DISMISS_COMMAND)
		{
			try
			{
				destroyApp(true);
			}
			catch(MIDletStateChangeException error)
			{
			
			}
		}
	}
}

J2ME - Alert Bloccante

// description of your code here

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

public class MessageBox extends Alert implements CommandListener
{
	protected Chattando midlet;
	
	private boolean isReady = false;
	
	private Displayable dspBACK;
	
	public MessageBox(String title, String text, AlertType type, Chattando midlet)
	{
		super(title, text, null, type);
		
		this.midlet = midlet;
		
		this.setCommandListener(this);
		this.setTimeout(Alert.FOREVER);
		
		// Display Precedente
		dspBACK = midlet.getDisplay().getCurrent();
		
		// Mostra l'alert
		midlet.getDisplay().setCurrent(this);
		
		// Attendi la conferma di chiusura
		waitForDone();
		
		// Visualizza il precedente Display
		midlet.getDisplay().setCurrent(dspBACK);
	}
	
	private void waitForDone()
	{
		try
		{
			while(!isReady)
			{
				synchronized(this)
				{
					this.wait();
					
				}
			}
		}
		catch(Exception error)
		{
			
		}
	}

	public void commandAction(Command cmd, Displayable dsp)
	{
		if(cmd == Alert.DISMISS_COMMAND)
		{
			isReady = true;
			
			synchronized(this)
			{
				this.notify();
			}			
		}
	}
}

J2ME - Create Service Bluetooth

// Create Service Bluetooth

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class ServerChat
{
	private static final String CHATTANDO_UUID = "A1A2A3A4A5A6A7A8A9A0B1B2B3B4B5B6";
	private static final String CHATTANDO_SERVICE = "Chattando";
	
	private boolean isReady = false;
	
	private StreamConnection stream_connection;
	private StreamConnectionNotifier stream_connection_notifier;
	
	public ServerChat()
	{
		startServerChatBluetooth();
	}
	
	// Apre il servizio per la Chat
	public void startServerChatBluetooth()
	{
		new Thread()
		{
			public void run()
			{
				try
				{
					LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
				
				try
				{
					stream_connection_notifier = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + CHATTANDO_UUID + ";name=" + CHATTANDO_SERVICE);
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
				
				stopServerChatBluetooth();
				
				// Mette in ascolto il Server della Chat
				isReady = true;
				
				try
				{
					while(isReady)
					{
						System.out.println("Sono in ascolto...");
						
						stream_connection = stream_connection_notifier.acceptAndOpen();
						
						System.out.println("Client Connected");
					}
				}
				catch(Exception error)
				{
					error.printStackTrace();
				}
			}
			
		}.start();
	}

	// Chiude il servizio per la Chat
	public void stopServerChatBluetooth()
	{
		if(isReady)
		{
			isReady = false;
			
			try
			{
				stream_connection_notifier.close();
			}
			catch(Exception error)
			{
				error.printStackTrace();
			}
		}
	}
}

J2ME - Search Service Bluetooth

// Example Search Service Bluetooth

import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

public class ClientChat implements DiscoveryListener
{
	private static final String CHATTANDO_UUID = "A1A2A3A4A5A6A7A8A9A0B1B2B3B4B5B6";
	private static final String CHATTANDO_SERVICE = "Chattando";

	protected Chattando midlet;
	
	private boolean searchDone = false;
	
	private DiscoveryAgent discovery_agent;
	
	private Vector remote_device;
	private Vector device_found;
	
	public ClientChat(Chattando midlet)
	{
		this.midlet = midlet;
		
		startScanBluetoothDevices();
	}
	
	// Avvia la ricerca dei dispositivi Bluetooth
	public void startScanBluetoothDevices()
	{
		try
		{
			remote_device = new Vector();
			device_found = new Vector();
			
			discovery_agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
			discovery_agent.startInquiry(DiscoveryAgent.GIAC, this);
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
	}
	
	// Stoppa la ricerca dei dispositivi Bluetooth
	public void stopScanBluetoothDevices()
	{
		discovery_agent.cancelInquiry(this);
	}

	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) 
	{
		// Aggiungo il dispositivo solo se e' un computer (0x0100) o un cellulare (0x0200)
		if(cod.getMajorDeviceClass() == 0x0100 || cod.getMajorDeviceClass() == 0x0200)
			remote_device.addElement(btDevice);
	}

	public void inquiryCompleted(int discType)
	{
		switch(discType)
		{
			case DiscoveryListener.INQUIRY_COMPLETED:
														System.out.println("Device Search Completed");
														
														break;
				
			case DiscoveryListener.INQUIRY_ERROR:
														System.out.println("Device Search Error");
														
														break;
				
			case DiscoveryListener.INQUIRY_TERMINATED:
														System.out.println("Device Search Terminated");
														
														break;
		}
		
		try
		{
			for(int i=0, cnt=remote_device.size(); i<cnt; i++)
			{
				discovery_agent.searchServices(new int[]{ 0x0100, 0x0200 }, new UUID[]{ new UUID(0x0003), new UUID(CHATTANDO_UUID, false) }, (RemoteDevice) remote_device.elementAt(i), this);
				waitForSearchDone();
			}
		}
		catch(Exception error)
		{
			error.printStackTrace();
		}
	}

	// Aspetta che la ricerca dei servizi per il dispositivo sia terminata
	private void waitForSearchDone()
	{
		searchDone = false;
		
		try
		{
			while(!searchDone)
			{
				synchronized(this)
				{
					this.wait();
				}
			}
		}
		catch(Exception error)
		{
			
		}
	}
	
	public void serviceSearchCompleted(int transID, int respCode)
	{
		switch(respCode)
		{
			case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
																		System.out.println("Service Search Completed");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
																		System.out.println("Service Search Device not Reachable");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_ERROR:
																		System.out.println("Service Search Error");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
																		System.out.println("Service Search No Records");
																		
																		break;
				
			case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
																		System.out.println("Service Search Terminated");
																		
																		break;
		}
		
		searchDone = true;
		
		// Risveglia il processo in attesa del completamento della ricerca dei servizi per un dispositivo
		synchronized(this)
		{
			this.notifyAll();
		}
	}

	public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
	{
		for(int i=0, cnt=servRecord.length; i<cnt; i++)
		{
			if(((String) servRecord[i].getAttributeValue(0x0100).getValue()).equalsIgnoreCase(CHATTANDO_SERVICE))
			{
				device_found.addElement(servRecord[i].getHostDevice());
			}
		}
	}
}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS