Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package soluciones;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import javax.bluetooth.DataElement;
- 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;
- import javax.microedition.io.Connector;
- import javax.microedition.io.StreamConnection;
- import javax.microedition.io.StreamConnectionNotifier;
- public class Cliente {
- public String chatURL;
- public void startClient() throws IOException, InterruptedException {
- List<RemoteDevice> devicesDiscovered = new ArrayList<RemoteDevice>();
- final Object inquiryCompletedEvent = new Object();
- devicesDiscovered.clear();
- DiscoveryListener listener = new DiscoveryListener() {
- public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
- System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
- devicesDiscovered.add(btDevice);
- try {
- System.out.println(" Name: " + btDevice.getFriendlyName(false));
- System.out.println(" BT Address: " + btDevice.getFriendlyName(false));
- } catch (IOException cantGetDeviceName) {
- }
- }
- public void inquiryCompleted(int discType) {
- System.out.println("Device Inquiry completed!");
- synchronized(inquiryCompletedEvent){
- inquiryCompletedEvent.notifyAll();
- }
- }
- public void serviceSearchCompleted(int transID, int respCode) {
- System.out.println("Service search completed!");
- synchronized(inquiryCompletedEvent){
- inquiryCompletedEvent.notifyAll();
- }
- }
- public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
- for (int i = 0; i < servRecord.length; i++) {
- String url = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
- if (url == null) {
- continue;
- }
- DataElement serviceName = servRecord[i].getAttributeValue(0x0100);
- if (serviceName != null) {
- System.out.println("Service name: " + serviceName.getValue() + ". Service url: " + url);
- if(serviceName.getValue().toString().equals("newchat")) {
- chatURL=url;
- }
- } else {
- System.out.println("Service name: " + url);
- }
- }
- }
- };
- synchronized(inquiryCompletedEvent) {
- boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
- if (started) {
- System.out.println("Client Searching for devices...");
- inquiryCompletedEvent.wait();
- System.out.println(devicesDiscovered.size() + " device(s) found");
- }
- }
- if (!devicesDiscovered.isEmpty()){
- UUID uuids[] = new UUID[1];
- uuids[0] = new UUID(0x1101); // SerialPort
- int attridset[] = new int[1];
- attridset[0] = 0x0100;
- for (RemoteDevice d : devicesDiscovered) {
- System.out.println(
- "Services available on the device " + d.getBluetoothAddress() + " - " + d.getFriendlyName(false));
- LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(attridset, uuids, d, listener);
- synchronized (inquiryCompletedEvent) {
- inquiryCompletedEvent.wait();
- }
- }
- if(chatURL!=null) {
- StreamConnection con = (StreamConnection) Connector.open(chatURL);
- Scanner sc = new Scanner(System.in);
- String response= " ";
- String mensaje = " ";
- RemoteDevice dev = RemoteDevice.getRemoteDevice(con);
- System.out.println("BT Address of the remote device: " + dev.getBluetoothAddress());
- System.out.println("Name of the remote device: " + dev.getFriendlyName(false));
- InputStream is = con.openInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- OutputStream os = con.openOutputStream();
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
- do {
- System.out.println("Client ->");
- mensaje=sc.nextLine();
- bw.write(mensaje);
- bw.newLine();
- bw.flush();
- System.out.println("Waiting for service response...");
- response=br.readLine();
- System.out.println(dev.getBluetoothAddress() + " - " + dev.getFriendlyName(false) + ": " + response);
- }while(!mensaje.equals("FIN"));
- System.out.println("Device " + dev.getBluetoothAddress() + " - " + dev.getFriendlyName(false) + " disconnected successfully");
- br.close();
- bw.close();
- sc.close();
- mensaje = "";
- con.close();
- }
- }
- }
- public static void main(String args[]) throws IOException, InterruptedException {
- Cliente cliente = new Cliente();
- cliente.startClient();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement