コード例 #1
1
ファイル: Main.java プロジェクト: aelane/TeddyB
        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();
          }
        }
コード例 #2
0
  @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");
    }
  }
コード例 #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();
      }
    }
コード例 #5
0
  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;
  }
コード例 #6
0
  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;
  }
コード例 #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);
   }
 }
コード例 #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();
  }
コード例 #10
0
  private boolean sendCommand(String command) {

    try {
      bluetoothSocket.getOutputStream().write((command + '!').getBytes());
      return true;
    } catch (IOException e) {
      return false;
    }
  }
コード例 #11
0
 public void send(byte[] bytes) {
   OutputStream outputStream;
   try {
     outputStream = transferSocket.getOutputStream();
     outputStream.write(bytes);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
コード例 #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();
     }
   }
 }
コード例 #13
0
  public void manageConnectedSocket(BluetoothSocket socket) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

    LivrePack lpk = new LivrePack(this.livreID);

    out.writeObject(lpk);

    out.close();
  }
コード例 #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());
  }
コード例 #15
0
  /**
   * 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);
  }
コード例 #16
0
  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;
  }
コード例 #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;
 }
コード例 #18
0
ファイル: MyService.java プロジェクト: Imanity/HelloArduino
  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!");
  }
コード例 #19
0
ファイル: BtAsyncTask.java プロジェクト: kjs4395/BlueTutorial
  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();
    }
  }
コード例 #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;
 }
コード例 #21
0
 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());
 }
コード例 #22
0
ファイル: MainActivity.java プロジェクト: iancwwong/solapp
    // 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;
    }
コード例 #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) {
    }
  }
コード例 #24
0
ファイル: BT.java プロジェクト: baddox/csc450-androidapp
  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;
  }
コード例 #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();
      }
    }
コード例 #26
0
 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;
 }
コード例 #27
0
ファイル: ComPort.java プロジェクト: playerdrafter/oobd
  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);
    }
  }
コード例 #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;
    }
コード例 #29
0
    _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;
    }
コード例 #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;
 }