Over a million developers have joined DZone.

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

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}