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());
}
}
}
}
No comments:
Post a Comment