示例#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
  public void run() {
    while (ready == true) {

      int s;
      try {
        s = socket.getInputStream().read();

        while (s != -1) {
          if (s == 13) {
            Log.v("BluetoothThread", "received message: " + buffer);
            synchronized (parent) {
              parent.received(buffer);
            }
            buffer = "";
            s = (char) socket.getInputStream().read();
          } else {
            buffer += (char) s;
            Log.v("BluetoothThread", "received : " + s);
            s = (char) socket.getInputStream().read();
          }
        }
      } catch (IOException e) {
        Log.v("BluetoothThread", "IO Exception on receive");
        // alert the parent class
        // wait for reconnect
        ready = false;
        parent.received("DISCONNECT");
      }

      if (timeSinceLastDiscovery + 12000 < System.currentTimeMillis()) {
        timeSinceLastDiscovery = System.currentTimeMillis();
      }
    }
  }
示例#3
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();
      }
    }
  @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 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;
  }
  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");
    }
  }
    @Override
    public void run() {
      setName("ConnectThread");
      Looper.prepare();

      // Always cancel discovery because it will slow down a connection
      mAdapter.cancelDiscovery();

      // Make a connection to the BluetoothSocket
      try {
        // This is a blocking call and will only return on a
        // successful connection or an exception

        mmSocket.connect();
      } catch (IOException e) {
        // connection failure
        connectionFailed(0, 1);
        // Close the socket
        try {
          mmSocket.close();
        } catch (IOException e2) {
          Log.e(TAG, "unable to close() socket during connection failure", e2);
        }
        // Start the service over to restart listening mode
        Log.d(TAG, "originally resetting start()?");
        setState(STATE_LISTEN);
        BluetoothService.this.start();
        return;
      }

      int answer = 0; // 0: no , 1: yes for permission from a friend
      // Reset the ConnectThread because we're done
      // wait for the message from a friend
      synchronized (BluetoothService.this) {
        mConnectThread = null;
        try {
          InputStream mmInStream = mmSocket.getInputStream();
          answer = mmInStream.read();
        } catch (IOException e) {
          e.printStackTrace();
        }
        Log.d(TAG, "reading answer: " + answer);
        // Start the connected thread
        if (answer == 1) {
          boolean myturn = mHandler.toggleMyTurn();
          if (!myturn) {
            Log.e(TAG, "error handling game turn...");
          }
          connected(mmSocket, mmDevice);
        } else {
          try {
            mmSocket.close();
          } catch (IOException e) {
            Log.e(TAG, "close() of connect socket failed", e);
          }
          connectionFailed(1, 1);
        }
      }
      Looper.loop();
    }
 @Override
 public InputStream getInputStream() throws HoTTException {
   try {
     return socket.getInputStream();
   } catch (final IOException e) {
     throw new HoTTException(e);
   }
 }
示例#9
0
 @Override
 public InputStream getInputStream() throws ConnectionLostException {
   try {
     return socket_.getInputStream();
   } catch (IOException e) {
     throw new ConnectionLostException(e);
   }
 }
示例#10
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();
  }
示例#11
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 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;
 }
  public void connecked() {
    _device = _bluetooth.getRemoteDevice(address);
    try {
      if ((_socket != null)) {
        is.close();
        _socket.close();
        _socket = null;
      }
      _socket = _device.createRfcommSocketToServiceRecord(UUID.fromString(MY_UUID));
      Log.v("address1:" + address, "address1" + address);
    } catch (IOException e) {
      Log.v("addressA:" + address, "addressA" + address);
      Toast.makeText(this, getResources().getString(R.string.failcon), Toast.LENGTH_SHORT).show();
    }

    try {
      _socket.connect();
      Log.v("address2:" + address, "address2:" + address);
      Toast.makeText(
              this,
              getResources().getString(R.string.con)
                  + _device.getName()
                  + getResources().getString(R.string.suc),
              Toast.LENGTH_SHORT)
          .show();
      // btn.setText("断开");
    } catch (IOException e) {
      try {
        Log.v("addressB:" + address, "addressB" + address);
        Toast.makeText(this, getResources().getString(R.string.failcon), Toast.LENGTH_SHORT).show();
        _socket.close();
        _socket = null;
      } catch (IOException ee) {
        Log.v("addressC:" + address, "addressC" + address);
        Toast.makeText(this, getResources().getString(R.string.failcon), Toast.LENGTH_SHORT).show();
      }

      return;
    }
    try {
      Log.v("address3:" + address, "address3:" + address);
      is = _socket.getInputStream(); // 得到蓝牙数据输入流
      Log.v("address4:" + address, "address4:" + address);

      Intent intent = new Intent(this, ControlActivity.class);
      startActivity(intent);

    } catch (IOException e) {
      Toast.makeText(this, getResources().getString(R.string.rcdafail), Toast.LENGTH_SHORT).show();
      return;
    }
  }
示例#15
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!");
  }
 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());
 }
示例#17
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();
    }
  }
示例#18
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) {
    }
  }
示例#19
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;
    }
 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;
 }
示例#21
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;
  }
示例#22
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();
      }
    }
示例#23
0
    public ConnectedThread(BluetoothSocket s) {
      this.socket = s;

      InputStream tmpIs = null;
      OutputStream tmpOs = null;
      try {
        tmpIs = socket.getInputStream();
        tmpOs = socket.getOutputStream();
      } catch (IOException e) {

      }

      inputStream = tmpIs;
      outputStream = tmpOs;
    }
  @Override
  protected Void doInBackground(Void... params) {

    try {
      InputStream in = mmSocket.getInputStream();
      BufferedReader bReader = new BufferedReader(new InputStreamReader(in));

      lectura = bReader.readLine();

    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }
示例#25
0
    public ConnectedThread(BluetoothSocket socket) {
      mmSocket = socket;
      InputStream tmpIn = null;
      OutputStream tmpOut = null;

      try { // Get the BluetoothSocket input and output streams
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
      } catch (IOException e) {
        TopoDroidLog.Log(TopoDroidLog.LOG_ERR, "temp sockets not created " + e);
      }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }
    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;
    }
 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;
 }
示例#29
0
    public ConnectedThread(BluetoothSocket Socket) {

      InputStream tmpIn = null;
      OutputStream tmpOut = null;
      mmSocket = Socket;

      try {
        tmpIn = Socket.getInputStream();
        tmpOut = Socket.getOutputStream();
      } catch (IOException e) {
        e.printStackTrace();
      }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }
  private void setupStreams() {

    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
      tmpIn = socket.getInputStream();
      tmpOut = socket.getOutputStream();
    } catch (IOException e) {
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
  }