Exemplo n.º 1
1
        public void run() {
          BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
          UUID uuid = UUID.fromString("4e5d48e0-75df-11e3-981f-0800200c9a66");
          try {
            BluetoothServerSocket serverSocket =
                adapter.listenUsingRfcommWithServiceRecord("BLTServer", uuid);
            android.util.Log.e("TrackingFlow", "Listening...");

            // Connection accepted and opening
            socket = serverSocket.accept();
            android.util.Log.e("TrackingFlow", "Socket accepted...");

            // Setup input and output streams
            is = socket.getInputStream();
            os = new OutputStreamWriter(socket.getOutputStream());

            // Enable the setup and learning buttons now
            runOnUiThread(enableButtons);

            // Store the socket for future use
            ((TEDTablet) getApplication()).setSocket(socket);

          } catch (IOException e) {
            e.printStackTrace();
          }
        }
  @Override
  public void disconnect(BtDevice btDevice) {
    BtContainer btContainer = AppProvider.getBtContainer();

    BluetoothSocket socket = btContainer.getSocketForDevice(btDevice);
    if (socket != null) {
      try {
        InputStream is = socket.getInputStream();

        OutputStream os = socket.getOutputStream();

        if (is != null) {
          is.close();
        }

        if (os != null) {
          os.close();
        }

        socket.close();
      } catch (IOException e) {
        btControllerInterface.disconnectFailed(btDevice);
      }
    }

    btContainer.removeConnected(btDevice);
    btControllerInterface.updateConnectedList();
  }
  public void makeconnection() {

    try {
      in = mysocket.getInputStream();
      out = mysocket.getOutputStream();

      oos = new ObjectOutputStream(out);
      // oos = new ObjectOutputStream(new BufferedOutputStream(out));
      oos.flush();

      ois = new ObjectInputStream(in);
      // ois =new ObjectInputStream(new BufferedInputStream(in));

      System.out.println("connection established");

      waitforreceivingdata();

    } catch (SocketException ex) {
      Log.d(TAG, "Hubo error makeconnection 1");

    } catch (IOException ex) {
      Log.d(TAG, "Hubo error makeconnection 2");
    } catch (Exception ex) {
      Log.d(TAG, "Hubo error makeconnection 3");
    }
  }
Exemplo n.º 4
0
    public void run() {
      InputStream inStream = null;
      OutputStream outStream = null;
      mBluetoothAdapter.cancelDiscovery();

      try {
        mySocket.connect();
      } catch (IOException e) {
        Log.e("Bluetooth", mDevice.getName() + ": Could not establish connection with device");
        try {
          mySocket.close();
        } catch (IOException e1) {
          Log.e("Bluetooth", mDevice.getName() + ": could not close socket", e1);
        }
      }

      String line = "";
      try {
        inStream = mySocket.getInputStream();
        outStream = mySocket.getOutputStream();

        outStream.write(message.getBytes());
        outStream.flush();
        Log.e("Bluetooth", "Message sent: " + message);
        inStream.close();
        outStream.close();
        cancel();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  public void sendMessage(String destination, String message) {
    try {
      BluetoothSocket myBsock = btSockets.get(destination);
      if (myBsock != null) {
        OutputStream outStream = myBsock.getOutputStream();
        byte[] stringAsBytes = (message + " ").getBytes();
        stringAsBytes[stringAsBytes.length - 1] = 0; // Add a stop marker
        outStream.write(stringAsBytes);
        DebugLog.i(TAG, "successful sendMessage - Dest:" + destination + ", Msg:" + message);
        Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE);
        Bundle bundle = new Bundle();
        bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, true);
        msg.setData(bundle);
        mHandler.sendMessage(msg);
      } else {
        Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE);
        Bundle bundle = new Bundle();
        bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, false);
        msg.setData(bundle);
        mHandler.sendMessage(msg);
      }
    } catch (IOException e) {
      DebugLog.e(TAG, "IOException in sendMessage - Dest:" + destination + ", Msg:" + message);
      Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_WRITE);
      Bundle bundle = new Bundle();
      bundle.putBoolean(DynamicAppBluetooth.SEND_RESULT, false);
      msg.setData(bundle);
      mHandler.sendMessage(msg);
    }

    return;
  }
  public ConnectedThread(BluetoothSocket socket, BFVService service) {

    this.service = service;

    Log.d(BFVService.TAG, "create ConnectedThread");
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;
    BufferedReader tmpReader = null;

    // Get the BluetoothSocket input and output streams
    try {
      tmpIn = socket.getInputStream();
      tmpOut = socket.getOutputStream();
      tmpReader = new BufferedReader(new InputStreamReader(tmpIn), 256);
    } catch (IOException e) {
      Log.e(BFVService.TAG, "temp sockets not created", e);
    }

    mmOutStream = tmpOut;
    mmReader = tmpReader;

    chargeFromVolts = new PiecewiseLinearFunction(new Point2d(3.6, 0.0));
    chargeFromVolts.addNewPoint(new Point2d(3.682, 0.032));
    chargeFromVolts.addNewPoint(new Point2d(3.696, 0.124));
    chargeFromVolts.addNewPoint(new Point2d(3.75, 0.212));
    chargeFromVolts.addNewPoint(new Point2d(3.875, 0.624));
    chargeFromVolts.addNewPoint(new Point2d(3.96, 0.73));
    chargeFromVolts.addNewPoint(new Point2d(4.16, 1.0));

    firstPressure = false;
  }
Exemplo n.º 7
0
 @Override
 public OutputStream getOutputStream() throws ConnectionLostException {
   try {
     return socket_.getOutputStream();
   } catch (IOException e) {
     throw new ConnectionLostException(e);
   }
 }
 @Override
 public OutputStream getOutputStream() throws HoTTException {
   try {
     return socket.getOutputStream();
   } catch (final IOException e) {
     throw new HoTTException(e);
   }
 }
Exemplo n.º 9
0
  public void connect(String address) throws IOException {
    BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
    // Throws an error on reconnect...
    bluetoothSocket = device.createRfcommSocketToServiceRecord(BLUETOOTH_UUID);
    bluetoothSocket.connect();

    out = bluetoothSocket.getOutputStream();
    in = bluetoothSocket.getInputStream();
  }
  private boolean sendCommand(String command) {

    try {
      bluetoothSocket.getOutputStream().write((command + '!').getBytes());
      return true;
    } catch (IOException e) {
      return false;
    }
  }
 public void send(byte[] bytes) {
   OutputStream outputStream;
   try {
     outputStream = transferSocket.getOutputStream();
     outputStream.write(bytes);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 12
0
 private void turnOffMotor() {
   if (btSocket != null) {
     try {
       btSocket.getOutputStream().write("m".toString().getBytes());
     } catch (IOException e) {
       Snackbar.make(prima, R.string.error, Snackbar.LENGTH_SHORT).show();
     }
   }
 }
  public void manageConnectedSocket(BluetoothSocket socket) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

    LivrePack lpk = new LivrePack(this.livreID);

    out.writeObject(lpk);

    out.close();
  }
Exemplo n.º 14
0
  public ConnectedThread(BluetoothSocket socket, ConnectedThreadListener listener)
      throws IOException {
    this.socket = socket;
    this.listener = listener;
    this.remoteDevice = socket.getRemoteDevice();

    objOutput = new ObjectOutputStream(socket.getOutputStream());
    objOutput.flush();
    objInput = new ObjectInputStream(socket.getInputStream());
  }
  /**
   * Create a bluetooth connection with SerialPortServiceClass_UUID
   *
   * @see <a href= "http://lejos.sourceforge.net/forum/viewtopic.php?t=1991&highlight=android" /> On
   *     error the method either sends a message to it's owner or creates an exception in the case
   *     of no message handler.
   */
  public void createNXTconnection() throws IOException {
    try {
      BluetoothSocket nxtBTSocketTemporary;
      BluetoothDevice nxtDevice = null;
      nxtDevice = btAdapter.getRemoteDevice(mMACaddress);
      if (nxtDevice == null) {
        if (uiHandler == null) throw new IOException();
        else {
          sendToast(mResources.getString(R.string.no_paired_nxt));
          sendState(STATE_CONNECTERROR);
          return;
        }
      }
      nxtBTSocketTemporary =
          nxtDevice.createRfcommSocketToServiceRecord(SERIAL_PORT_SERVICE_CLASS_UUID);
      try {
        nxtBTSocketTemporary.connect();
      } catch (IOException e) {
        if (myOwner.isPairing()) {
          if (uiHandler != null) {
            sendToast(mResources.getString(R.string.pairing_message));
            sendState(STATE_CONNECTERROR_PAIRING);
          } else throw e;
          return;
        }

        // try another method for connection, this should work on the HTC desire, credits to Michael
        // Biermann
        try {
          Method mMethod =
              nxtDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
          nxtBTSocketTemporary = (BluetoothSocket) mMethod.invoke(nxtDevice, Integer.valueOf(1));
          nxtBTSocketTemporary.connect();
        } catch (Exception e1) {
          if (uiHandler == null) throw new IOException();
          else sendState(STATE_CONNECTERROR);
          return;
        }
      }
      nxtBTsocket = nxtBTSocketTemporary;
      nxtInputStream = nxtBTsocket.getInputStream();
      nxtOutputStream = nxtBTsocket.getOutputStream();
      connected = true;
    } catch (IOException e) {
      if (uiHandler == null) throw e;
      else {
        if (myOwner.isPairing()) sendToast(mResources.getString(R.string.pairing_message));
        sendState(STATE_CONNECTERROR);
        return;
      }
    }
    // everything was OK
    if (uiHandler != null) sendState(STATE_CONNECTED);
  }
  public WriteToSparkiTask(BluetoothSocket socket) {
    OutputStream tmpOut = null;

    // Get the output stream, using temp object because
    // member streams are final
    try {
      tmpOut = socket.getOutputStream();
    } catch (IOException e) {
    }

    outputStream = tmpOut;
  }
Exemplo n.º 17
0
 public ConnectedThread(BluetoothSocket socket) {
   mmSocket = socket;
   InputStream tmpIn = null;
   OutputStream tmpOut = null;
   try {
     tmpIn = socket.getInputStream();
     tmpOut = socket.getOutputStream();
   } catch (IOException e) {
   }
   mmInStream = tmpIn;
   mmOutStream = tmpOut;
 }
Exemplo n.º 18
0
  public void myBtConnect() {
    sendToast("Connecting...");

    //  BluetoothDevice mBtDevice = mBtAdapter.getRemoteDevice(HC_MAC);
    BluetoothDevice mBtDevice = null;
    Set<BluetoothDevice> mBtDevices = mBtAdapter.getBondedDevices();
    if (mBtDevices.size() > 0) {
      for (Iterator<BluetoothDevice> iterator = mBtDevices.iterator(); iterator.hasNext(); ) {
        mBtDevice = (BluetoothDevice) iterator.next();
        sendToast(mBtDevice.getName() + "|" + mBtDevice.getAddress());
      }
    }

    try {
      mBtSocket = mBtDevice.createRfcommSocketToServiceRecord(HC_UUID);
    } catch (IOException e) {
      e.printStackTrace();
      mBtFlag = false;
      sendToast("Create bluetooth socket error");
      return;
    }

    mBtAdapter.cancelDiscovery();

    /* Setup connection */
    try {
      mBtSocket.connect();
      sendToast("Connect bluetooth success");
      // Log.i(TAG, "Connect " + HC_MAC + " Success!");
    } catch (IOException e) {
      e.printStackTrace();
      try {
        mBtSocket.close();
        mBtFlag = false;
      } catch (IOException e1) {
        e1.printStackTrace();
      }
      return;
    }

    /* I/O initialize */
    if (mBtFlag) {
      try {
        inStream = mBtSocket.getInputStream();
        outStream = mBtSocket.getOutputStream();
      } catch (IOException e) {
        e.printStackTrace();
        return;
      }
    }
    sendToast("Bluetooth is ready!");
  }
Exemplo n.º 19
0
  public BtAsyncTask(Activity activity, BluetoothSocket socket) {

    // save the calling activity
    mainActivity = activity;

    // Prepare stream reader and writer
    try {
      reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 20
0
 private boolean sendCommand(int command) {
   byte byteCommand = (byte) command;
   try {
     if (btSocket != null) {
       if (D) Log.d(TAG, "Sending command: " + String.format("%02X", byteCommand));
       btSocket.getOutputStream().write(byteCommand);
       return true;
     }
   } catch (IOException e) {
     Log.e(TAG, e.getLocalizedMessage());
   }
   return false;
 }
 ConnectedThread(BluetoothSocket socket) {
   mmSocket = socket;
   InputStream tmpIn = null;
   OutputStream tmpOut = null;
   try {
     tmpIn = socket.getInputStream();
     tmpOut = socket.getOutputStream();
   } catch (IOException e) {
   }
   mmInStream = tmpIn;
   mmOutStream = tmpOut;
   setName("OneSheeldConnectedReadThread: " + OneSheeldDevice.this.getName());
 }
Exemplo n.º 22
0
    // creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
      InputStream tmpIn = null;
      OutputStream tmpOut = null;

      try {
        // Create I/O streams for connection
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
      } catch (IOException e) {
      }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }
Exemplo n.º 23
0
  public void connectDevice(String address) {
    device = adapter.getRemoteDevice(address);
    // Connection Attempt

    try {
      bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
      bluetoothSocket.connect();
      outStream = bluetoothSocket.getOutputStream();
      inStream = bluetoothSocket.getInputStream();
      rover = new Rover(BluetoothService.getConnection());
      startDataListener();
    } catch (IOException e) {
    }
  }
Exemplo n.º 24
0
  public void connectStreams() {
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    try {
      tmpIn = socket.getInputStream();
      tmpOut = socket.getOutputStream();
    } catch (IOException e) {
      Log.d("BTCON", "Connecting Streams crashed");
    }

    inStream = tmpIn;
    outStream = tmpOut;
  }
Exemplo n.º 25
0
    public void run() {
      Log.e("Bluetooth", "Begin waiting for connection");
      BluetoothSocket connectSocket = null;
      InputStream inStream = null;
      OutputStream outStream = null;
      String line = "";

      while (true) {
        try {
          connectSocket = myServSocket.accept();
          mBluetoothAdapter.cancelDiscovery();
        } catch (IOException e) {
          Log.e("Bluetooth", "Connection failed");
          break;
        }

        try {
          inStream = connectSocket.getInputStream();
          outStream = connectSocket.getOutputStream();
          int i = 0;

          idBuilder = new StringBuilder();
          while (i != -1) {
            i = inStream.read();
            idBuilder.append((char) i);
          }

        } catch (IOException e) {
          Log.e("Bluetooth", idBuilder.toString());
          mIdReceivedCallback.onIdReceived(idBuilder.toString());
          try {
            inStream.close();
            outStream.close();
            connectSocket.close();
          } catch (IOException e1) {
            e1.printStackTrace();
          }
          //                    break;
        }
      }

      try {
        if (inStream != null && outStream != null) {
          inStream.close();
          outStream.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
 public ConnectedThread(BluetoothSocket socket, BluetoothDevice mmDev) {
   toaster(1, mmDev);
   Log.i("ConnectedThread", "Inside ConnectedThread");
   CONNECTED = 1;
   InputStream tmpIn = null;
   OutputStream tmpOut = null;
   try {
     tmpIn = socket.getInputStream();
     tmpOut = socket.getOutputStream();
   } catch (IOException e) {
   }
   mmInStream = tmpIn;
   mmOutStream = tmpOut;
 }
Exemplo n.º 27
0
  public synchronized void write(String s) {
    Log.d("OOBD:Comport", "Write something ");
    if (serialPort != null) {
      try {
        serialPort.getOutputStream().write(s.getBytes());
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    } else {
      System.err.println("NO comhandle:" + s);
    }
  }
Exemplo n.º 28
0
    public CommunicationThread(BluetoothSocket socket) {
      Log.w(TAG, "CommunicationThread()");
      communicationSocket = socket;
      InputStream tmpIn = null;
      OutputStream tmpOut = null;
      // To final Input and Output streams we use temp objects
      try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
      } catch (IOException e) {
      }

      communicationInStream = tmpIn;
      communicationOutStream = tmpOut;
    }
    _ConnectionManager(BluetoothSocket _BluetoothSocket) {

      mmBluetoothSocket = _BluetoothSocket;
      InputStream _InputStream = null;
      OutputStream _OutputStream = null;

      try {
        _InputStream = mmBluetoothSocket.getInputStream();
        _OutputStream = mmBluetoothSocket.getOutputStream();
      } catch (Exception _e) {
      }

      mmInputStream = _InputStream;
      mmOutputStream = _OutputStream;
    }
Exemplo n.º 30
0
 public ConnectedThread(BluetoothSocket socket) {
   Log.d(TAG, "create ConnectedThread");
   mmSocket = socket;
   InputStream tmpIn = null;
   OutputStream tmpOut = null;
   // Get the BluetoothSocket input and output streams
   try {
     tmpIn = socket.getInputStream();
     tmpOut = socket.getOutputStream();
   } catch (IOException e) {
     Log.e(TAG, "temp sockets not created", e);
   }
   mmInStream = tmpIn;
   mmOutStream = tmpOut;
 }