|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||
See:
Description
| Class Summary | |
| JSRTest | COPYRIGHT: (c) Copyright 2004 Avetana GmbH ALL RIGHTS RESERVED. |
This package provides an implementation of the JSR 82 specification from Sun Microsystems (c).
The aim of this specification is to easily develop Bluetooth-based applications in java. It
does not exist (as I am writing these comments) any universal implementation of the Sun Specification.
The Avetana Bluetooth package is available under three operating systems: Linux (GPL version), Windows
and Mac OS X (commercial versions) and is NOT a 100 % pure Java implementation. The use of JNI technologies
allows to communicate with the hardware and system-specific bluetooth stack.
Under Linux you must have a kernel version > 2.4.20 or at least you must have the BlueZ kernel modules
installed AND loaded.
LocalDevice m_local=LocalDevice.getLocalDevice();
//retrieves the BT address of the local device
LocalDevice.getBluetoothAdress()
//retrieves the name of the local device
LocalDevice.getFriendlyName()
// retrieves the discoverable mode. Beware, this method often requires root privileges
LocalDevice.getDiscoverableMode()
The method getRecord(ConnectionNotifier) and updateRecord(ServiceRecord) cannot be
directly used here. LocalDevice you can retrieve the DiscoveryAgent which will help you to perform an HCI inquiry
(Device search) or a service Search.
//retrieves the DiscoveryAgent
DiscoveryAgent m_agent=LocalDevice.getDiscoveryAgent()
Whatever the search you want to perforn you need beside your DiscoveryAgent a DiscoveryListener.
DiscoveryListener myListener=new DiscoveryListener() {
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
//does nothing
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
try {
System.out.println("New Device "+btDevice.getBluetoothAddress()+" found!");
System.out.println("Remote Name of the device is "+btDevice.getFriendlyName(true));
}catch(Exception ex) {}
}
public void inquiryCompleted(int discType) {
System.out.println("Device Search completed!");
}
public void serviceSearchCompleted(int transID, int respCode) {
// does nothing
}
};
try {
m_agent.startInquiry(DiscoveryAgent.GIAC, myListener);
}
catch(Exception ex) {ex.printStackTrace();}
This example does not implement the part of the DiscoveryListener class used to
manage the result of a service search. In this case this does not have any influence, since
this little code-fragment is only performing a device search.
String connectionURL="btspp://123456123456:1;encrypt=false;authenticate=false;master=true"
Connection con=Connector.open(connectionURL);
Runnable r=new Runnable() {
public void run() {
byte b[] = new byte[200];
try {
while (running) {
dataReceived.setText("Received " + received);
int a = is.read(b);
received += a;
}
} catch (Exception e) {e.printStackTrace();running=false; }
}
//Starts the thread used to read data
new Thread(r).run();
//Write some data
((StreamConnection)streamCon).openDataOutputStream().writeChars("Try to write");
|
||||||||||
| PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES | |||||||||