Exemplo n.º 1
0
 public void writeObject(Serializable object) {
   try {
     objOutput.writeObject(object);
     objOutput.flush();
     Log.v(TAG, "Wrote object to " + remoteDevice.toString());
   } catch (IOException e) {
     Log.e(TAG, "Failed to write object to " + remoteDevice.toString());
     close();
   }
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          Log.d(TAG, "广播接收者运行..." + action);

          // 查找到设备action
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // 得到蓝牙设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // 如果是已配对的则略过,已得到显示,其余的在添加到列表中进行显示
            Log.d(TAG, device.toString());
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesAdapter.add(device);
              tv_new_devices.setVisibility(View.VISIBLE);
              //                    newDevices.add(device);
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            swiperefresh.setRefreshing(false);
            //                btn_sann.setVisibility(View.VISIBLE);
            if (mNewDevicesAdapter.getCount() == 0) {
              setTitle("没有找到新设备");
              Toast.makeText(DeviceListActivity.this, "没有发现新设备!", Toast.LENGTH_SHORT).show();
            } else {
              setTitle(getString(R.string.chose_to_connect));
              tv_new_devices.setVisibility(View.VISIBLE);
            }
          }
        }
Exemplo n.º 3
0
  public void close() {
    Log.v(TAG, "Closing connection with " + remoteDevice.toString());
    try {
      Thread.sleep(WAIT_CLOSE);
      socket.close();
    } catch (Exception e) {
    }

    listener.onClose(remoteDevice);
  }
  // This method gets messages from both onStartCommand and mServiceHandler/mServiceLooper
  private synchronized void processMessage(Message msg) {
    int msgType = msg.what;
    final int state = msg.arg1;
    final int startId = msg.arg2;
    BluetoothDevice device = null;
    if (msg.obj != null) {
      device = (BluetoothDevice) msg.obj;
    }

    if (DEBUG)
      Log.d(
          TAG,
          "processMessage: "
              + msgType
              + " state: "
              + state
              + " device = "
              + (device == null ? "null" : device.toString()));

    boolean deferFinishCall = false;

    switch (msgType) {
      case MSG_TYPE_SHOW_UI:
        if (device != null) {
          createDialog(device, state, startId);
        }
        break;

      case MSG_TYPE_DOCKED:
        deferFinishCall = msgTypeDocked(device, state, startId);
        break;

      case MSG_TYPE_UNDOCKED_PERMANENT:
        deferFinishCall = msgTypeUndockedPermanent(device, startId);
        break;

      case MSG_TYPE_UNDOCKED_TEMPORARY:
        msgTypeUndockedTemporary(device, state, startId);
        break;

      case MSG_TYPE_DISABLE_BT:
        deferFinishCall = msgTypeDisableBluetooth(startId);
        break;
    }

    if (mDialog == null
        && mPendingDevice == null
        && msgType != MSG_TYPE_UNDOCKED_TEMPORARY
        && !deferFinishCall) {
      // NOTE: We MUST not call stopSelf() directly, since we need to
      // make sure the wake lock acquired by the Receiver is released.
      DockEventReceiver.finishStartingService(this, startId);
    }
  }
Exemplo n.º 5
0
 @Override
 public void run() {
   Log.v(TAG, "Established object stream with " + remoteDevice.toString());
   while (true) {
     try {
       listener.onReceive(remoteDevice, (Serializable) objInput.readObject());
       Log.v(TAG, "Received object from " + remoteDevice.toString());
     } catch (IOException e) {
       Log.v(TAG, "IO error in comm channel with " + remoteDevice.toString());
       listener.onReceiveError(remoteDevice);
       close();
       break;
     } catch (ClassNotFoundException e) {
       Log.e(TAG, "Object mismatch exception in comm channel with " + remoteDevice.toString(), e);
       listener.onReceiveError(remoteDevice);
       close();
       break;
     }
   }
 }