import
java.io.IOException;
import
java.util.UUID;
import
javax.bluetooth.BluetoothDevice;
import
javax.bluetooth.BluetoothDiscoveryAgent;
import
javax.bluetooth.BluetoothStateException;
import
javax.bluetooth.DiscoveryListener;
import
javax.bluetooth.LocalDevice;
import
javax.bluetooth.RemoteDevice;
import
javax.bluetooth.ServiceRecord;
public
class
BluetoothCommunicator {
private
UUID uuid;
private
RemoteDevice device;
private
boolean
isConnected;
public
BluetoothCommunicator(UUID uuid) {
this
.uuid = uuid;
this
.device =
null
;
this
.isConnected =
false
;
}
public
boolean
connectToDevice(String deviceName) {
try
{
LocalDevice localDevice = LocalDevice.getLocalDevice();
BluetoothDiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
Object inquiryCompletedEvent =
new
Object();
DiscoveryListener listener =
new
DiscoveryListener() {
public
void
deviceDiscovered(RemoteDevice device, DeviceClass deviceClass) {
try
{
String name = device.getFriendlyName(
false
);
if
(name.equals(deviceName)) {
BluetoothCommunicator.
this
.device = device;
discoveryAgent.cancelInquiry(
this
);
synchronized
(inquiryCompletedEvent) {
inquiryCompletedEvent.notifyAll();
}
}
}
catch
(IOException e) {
e.printStackTrace();
}
}
public
void
inquiryCompleted(
int
discType) {
synchronized
(inquiryCompletedEvent) {
inquiryCompletedEvent.notifyAll();
}
}
public
void
serviceSearchCompleted(
int
transID,
int
respCode) {
}
public
void
servicesDiscovered(
int
transID, ServiceRecord[] serviceRecords) {
}
};
synchronized
(inquiryCompletedEvent) {
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, listener);
inquiryCompletedEvent.wait();
}
if
(device ==
null
) {
System.out.println(
"Device not found."
);
return
false
;
}
if
(!device.isAuthenticated()) {
boolean
isAuthenticated = device.authenticate();
if
(!isAuthenticated) {
System.out.println(
"Could not authenticate with device."
);
return
false
;
}
}
if
(!isConnected) {
BluetoothCommunicatorStreamHandler streamHandler =
new
BluetoothCommunicatorStreamHandler(device, uuid);
isConnected = streamHandler.connect();
}
return
isConnected;
}
catch
(BluetoothStateException e) {
e.printStackTrace();
return
false
;
}
catch
(InterruptedException e) {
e.printStackTrace();
return
false
;
}
}
public
boolean
send(String message) {
if
(!isConnected) {
System.out.println(
"Not connected to a device."
);
return
false
;
}
BluetoothCommunicatorStreamHandler streamHandler =
new
BluetoothCommunicatorStreamHandler(device, uuid);
return
streamHandler.send(message);
}
public
String receive() {
if
(!isConnected) {
System.out.println(
"Not connected to a device."
);
return
null
;
}
BluetoothCommunicatorStreamHandler streamHandler =
new
BluetoothCommunicatorStreamHandler(device, uuid);
return
streamHandler.receive();
}
public
boolean
disconnect() {
if
(!isConnected) {
System.out.println(
"Not connected to a device."
);
return
false
;
}
BluetoothCommunicatorStreamHandler streamHandler =
new
BluetoothCommunicatorStreamHandler(device, uuid);
boolean
isDisconnected = streamHandler.disconnect();
if
(isDisconnected) {
isConnected =
false
;
}
return
isDisconnected;
}
}