コード例 #1
0
  public synchronized void start() {
    DebugLog.i(TAG, "start");

    setState(STATE_LISTEN);

    if (mSecureAcceptThread == null) {
      DebugLog.i(TAG, "starting accept thread...");
      mSecureAcceptThread = new AcceptThread();
      mSecureAcceptThread.start();
    }
  }
コード例 #2
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;
  }
コード例 #3
0
  /**
   * Write to the ConnectedThread in an unsynchronized manner
   *
   * @param out The bytes to write
   * @see ConnectedThread#write(byte[])
   */
  public void write(BluetoothDevice device, byte[] out) {
    // Create temporary object
    ConnectedThread r = null;
    // Synchronize a copy of the ConnectedThread
    synchronized (this) {
      String addressToStop = device.getAddress();
      int size = connectedThreads.size();

      for (int i = 0; i < size; i++) {
        ConnectedThread conThread = connectedThreads.get(i);

        if (conThread != null) {
          String address = conThread.getMmDevice().getAddress();
          if (addressToStop.equals(address)) {
            r = conThread;
            break;
          }
        }
      }
    }
    // Perform the write unsynchronized
    if (r != null) {
      r.write(out);
    } else {
      DebugLog.i(TAG, "write thread not connected");
    }
  }
コード例 #4
0
  public synchronized void disconnect(BluetoothDevice device) {
    String addressToStop = device.getAddress();
    BluetoothSocket socket = btSockets.get(addressToStop);
    if (socket != null) {
      try {
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    btSockets.remove(addressToStop);
    btDeviceAddresses.remove(addressToStop);

    DebugLog.i(TAG, "stop");

    int size = connectedThreads.size();

    for (int i = 0; i < size; i++) {
      ConnectedThread conThread = connectedThreads.get(i);

      if (conThread != null) {
        String address = conThread.getMmDevice().getAddress();
        if (addressToStop.equals(address)) {
          conThread = null;
          connectedThreads.remove(i);
          break;
        }
      }
    }

    this.start();
  }
コード例 #5
0
  public synchronized void connect(BluetoothDevice device) {
    DebugLog.i(TAG, "connect to: " + device);

    mConnectThread = new ConnectThread(device);
    mConnectThread.start();
    setState(STATE_CONNECTING);
  }
コード例 #6
0
    public void run() {
      mAdapter.cancelDiscovery();
      try {
        DebugLog.i(TAG, "start of AcceptThread:");
        for (int i = 0; (i < MAX_CONNECTIONS) && (connections < MAX_CONNECTIONS); ) {

          String uid = uuidList.get(i);
          if (availableUuids.get(uid)) {
            BluetoothServerSocket myServerSocket =
                mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, UUID.fromString(uid));

            BluetoothSocket socket = myServerSocket.accept();

            if (socket != null) {
              synchronized (BluetoothConnectionService.this) {
                DebugLog.i(TAG, "AcceptThread:" + i);
                String address = socket.getRemoteDevice().getAddress();
                btSockets.put(address, socket);
                connections++;
                availableUuids.put(uid, false);
                deviceUuidList.put(socket.getRemoteDevice(), uid);
                connected(socket, socket.getRemoteDevice());
                try {
                  myServerSocket.close();
                } catch (IOException e) {
                  DebugLog.e(TAG, "Could not close unwanted socket");
                }
              }
            }
          } else {
            DebugLog.e(TAG, "try AcceptThread:" + i);
          }

          i++;

          if ((i == MAX_CONNECTIONS) && (connections < MAX_CONNECTIONS)) {
            i = 0;
          }
        }
        DebugLog.i(TAG, "end of AcceptThread:");
        connectionFullFailure();

      } catch (IOException e) {

      }
    }
コード例 #7
0
  @Override
  public void execute() {
    DebugLog.i(TAG, "parameters are: " + params);

    if (methodName.equalsIgnoreCase("notify")) {
      dynamicApp.callJsEvent(PROCESSING_FALSE);
      this.customNotify();
    } else {
      dynamicApp.callJsEvent(PROCESSING_FALSE);
      this.cancelNotification();
    }
  }
コード例 #8
0
  /** Stop all threads */
  public synchronized void stop() {
    DebugLog.i(TAG, "stop");

    if (mConnectThread != null) {
      mConnectThread.cancel();
      mConnectThread = null;
    }

    if (mSecureAcceptThread != null) {
      mSecureAcceptThread = null;
    }

    this.disconnectAll();

    setState(STATE_NONE);
  }
コード例 #9
0
    public ConnectedThread(BluetoothDevice device, BluetoothSocket socket) {
      // BluetoothSocket bSock = btSockets.get(socke);
      mmDevice = device;
      DebugLog.i(TAG, "create ConnectedThread: ");
      InputStream tmpIn = null;
      OutputStream tmpOut = null;
      // Get the BluetoothSocket input and output streams
      try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
      } catch (IOException e) {
        DebugLog.e(TAG, "temp sockets not created");
      }

      mmInStream = tmpIn;
      mmOutStream = tmpOut;
    }
コード例 #10
0
    public void run() {
      DebugLog.i(TAG, "BEGIN mConnectedThread");
      final int MAX_SIZE = 5242880;
      byte[] buffer = new byte[MAX_SIZE];
      int bytesRead = -1;
      String message = "";
      // Keep listening to the InputStream while connected
      while (true) {
        try {
          message = "";
          bytesRead = mmInStream.read(buffer);
          if (bytesRead != -1) {
            while ((bytesRead >= 0) && (buffer[bytesRead - 1] != 0)) {
              message = message + new String(buffer, 0, bytesRead);
              bytesRead = mmInStream.read(buffer);
            }

            if (buffer[bytesRead - 1] == 0) {
              message = message + new String(buffer, 0, bytesRead - 1); // Remove stop marker

              Message msg = mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_READ);
              Bundle bundle = new Bundle();
              bundle.putBoolean(DynamicAppBluetooth.READ_RESULT, true);
              bundle.putString(DynamicAppBluetooth.RECEIVED_MESSAGE, message);
              bundle.putString(DynamicAppBluetooth.DEVICE_NAME, mmDevice.getName());
              bundle.putString(DynamicAppBluetooth.DEVICE_ADD, mmDevice.getAddress());
              msg.setData(bundle);
              mHandler.sendMessage(msg);
            }
          } else {
            DebugLog.e(TAG, "end of read");
          }
        } catch (IOException e) {
          DebugLog.e(TAG, "disconnected");
          connectionLost(mmDevice);
          // Start the service over to restart listening mode
          BluetoothConnectionService.this.start();
          break;
        }
      }
    }
コード例 #11
0
  private synchronized void setState(int state) {
    DebugLog.i(TAG, "setState() " + mState + " -> " + state);
    mState = state;

    mHandler.obtainMessage(DynamicAppBluetooth.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
  }
コード例 #12
0
 public void connectionFullFailure() {
   // this.setState(STATE_FULL);
   DebugLog.i(TAG, "Sockets are full.");
 }