Example #1
0
    @Override
    public void onClick(View v) {
      switch (v.getId()) {
        case R.id.item_card:
          Log.e("click", mDataset.get(getAdapterPosition()).getName());
          BluetoothDevice btDev = mDataset.get(getAdapterPosition());

          if (btDev.getBondState() == BluetoothDevice.BOND_NONE) {
            Log.e("BlueToothTest", "start pair");
            Boolean isBonded = false;
            try {
              isBonded = createBond(btDev);
              if (isBonded) {
                Log.e("Log", "Paired");
                tvDeviceName.setTextColor(ContextCompat.getColor(activity, R.color.forest_green));
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          } else if (btDev.getBondState() == BluetoothDevice.BOND_BONDED) {

            Jump();
          }
          break;
      }
    }
  private void initializeUIElements() {
    TextView textViewDeviceName = (TextView) findViewById(R.id.textViewDeviceName);
    textViewDeviceName.setText(device.getName());

    ImageView imageViewPairStatus = (ImageView) findViewById(R.id.imageViewPairStatus);
    switch (device.getBondState()) {
      case BluetoothDevice.BOND_NONE:
        imageViewPairStatus.setImageResource(R.drawable.bluetooth_notpaired);
        break;
      case BluetoothDevice.BOND_BONDED:
        imageViewPairStatus.setImageResource(R.drawable.bluetooth_paired);
        break;
      case BluetoothDevice.BOND_BONDING:
        imageViewPairStatus.setImageResource(R.drawable.bluetooth_bonding);
        break;
    }

    TextView textViewStatus = (TextView) findViewById(R.id.textViewStatus);
    switch (device.getBondState()) {
      case BluetoothDevice.BOND_BONDED:
        textViewStatus.setText(R.string.label_status_bonded);
        break;
      case BluetoothDevice.BOND_BONDING:
        textViewStatus.setText(R.string.label_status_bonding);
        break;
      case BluetoothDevice.BOND_NONE:
        textViewStatus.setText(R.string.label_status_not_bonded);
        break;
    }

    TextView textViewAddress = (TextView) findViewById(R.id.textViewAddress);
    textViewAddress.setText(
        getResources().getString(R.string.label_device_address) + " " + device.getAddress());
  }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed
            // already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());

              // if (lastAdress.compareTo(device.getAddress())==0)
              // {
              setAndReturn(device.getAddress());
              // }
            }
            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getText(R.string.none_found).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          }
        }
        @Override
        public void onReceive(Context context, Intent intent) {
          Log.e("STEGANOS", "Inside the onreceive method");
          String action = intent.getAction();
          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    Toast.makeText(getBaseContext(), "Device found", Toast.LENGTH_SHORT).show();
                  }
                });
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device =
                (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed alreadsy
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }

            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    Toast.makeText(getBaseContext(), "Discovery Finished!", Toast.LENGTH_SHORT)
                        .show();
                  }
                });
          }
        }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // 查找到设备action
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // 得到蓝牙设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // 如果是已配对的则略过,已得到显示,其余的在添加到列表中进行显示
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            } else { // 添加到已配对设备列表
              mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
              // mPairedDevicesArrayAdapter.add(mBtAdapter.getRemoteDevice(device.getAddress()).getName() + "\n" + device.getAddress());
            }
            // 搜索完成action
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(getResources().getString(R.string.select));
            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getString(R.string.nonewdevice);
              mNewDevicesArrayAdapter.add(noDevices);
            }
          }
        }
        @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);
            }
          }
        }
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              String strNoFound = getIntent().getStringExtra("no_devices_found");
              if (strNoFound == null) strNoFound = "No devices found";

              if (mPairedDevicesArrayAdapter.getItem(0).equals(strNoFound)) {
                mPairedDevicesArrayAdapter.remove(strNoFound);
              }
              mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }

            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            String strSelectDevice = getIntent().getStringExtra("select_device");
            if (strSelectDevice == null) strSelectDevice = "Select a device to connect";
            setTitle(strSelectDevice);
          }
        }
 @Override
 public void onReceive(Context context, Intent intent) {
   Bundle b = intent.getExtras();
   BluetoothDevice device =
       mBluetoothAdapter.getRemoteDevice(
           b.get("android.bluetooth.device.extra.DEVICE").toString());
   Log.d("Bond state", "BOND_STATED = " + device.getBondState());
 }
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          mBluetoothAdapter.cancelDiscovery();

          // Get MAC-Address
          String info = ((TextView) view).getText().toString();
          String address = info.substring(info.length() - 17);

          BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

          if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
            removeBond(device);
          } else if (device.getBondState() == BluetoothDevice.BOND_NONE) {
            createBond(device);
          }

          return true;
        }
    @Override
    public void onReceive(Context context, Intent intent) {

      if (intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
        BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
        byte[] pinBytes;

        Log.d(TAG, "------------" + intent);
        switch (type) {
          case BluetoothDevice.PAIRING_VARIANT_PIN:
            pinBytes = BluetoothDevice.convertPinToBytes(mDefaultCode);
            bluetoothDevice.setPin(pinBytes);
            break;
          case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
            int passkey = Integer.parseInt(mDefaultCode);
            bluetoothDevice.setPasskey(passkey);
            break;
          case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
          case BluetoothDevice.PAIRING_VARIANT_CONSENT:
            {
              bluetoothDevice.setPairingConfirmation(true);
              pinBytes = BluetoothDevice.convertPinToBytes(mDefaultCode);
              bluetoothDevice.setPin(pinBytes);
            }
            break;
          case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
          case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
            int pairingKey =
                intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
            if (type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
              bluetoothDevice.setPairingConfirmation(true);
            } else {
              String pinCode = String.format("%04d", pairingKey);
              pinBytes = BluetoothDevice.convertPinToBytes(pinCode);
              bluetoothDevice.setPin(pinBytes);
            }
            break;
          case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
            bluetoothDevice.setRemoteOutOfBandData();
            break;
        }

      } else if (intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
        int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

        if (state == BluetoothAdapter.STATE_ON) {
          mAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, 0);
          mAdapter.setDiscoverableTimeout(0);
        }
      } else if (intent.getAction().equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
          mServerThread.cancel();
          mServerThread.start();
        }
      }
    }
  protected void showDeviceDialog(BluetoothDevice device, final DialogListener listener) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String message = "Device " + device.getName() + " has address: " + device.getAddress() + ".\n";
    boolean isBonded = false;
    if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
      isBonded = true;
      message += "You are Bonded to this device.\n";
    } else if (device.getBondState() == BluetoothDevice.BOND_BONDING) {
      message += "You are currently bonding to this device.\n";
    } else {
      message += "You are not bonded to this device.\n";
    }

    builder.setMessage(message).setCancelable(true);

    if (!isBonded) {
      builder.setPositiveButton(
          "Pair to this device",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              listener.pairDevice();
            }
          });
    } else {
      builder.setNegativeButton(
          "Un-Pair from this device",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
              listener.unpairDevices();
              dialog.cancel();
            }
          });
    }

    AlertDialog deviceDialog = builder.create();
    deviceDialog.setOnKeyListener(
        new OnKeyListener() {
          public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            return DroidToothActivity.this.onKeyDown(keyCode, event);
          }
        });
    deviceDialog.show();
  }
  // Look for a connected device with glass in the name, or the one and only bound device
  public BluetoothDevice getBTDevice() {
    String addressOfDevice = RouterService.getDeviceAddress(mRouterService);

    BluetoothDevice suggestedDevice = null;
    BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
    if (defaultAdapter != null && defaultAdapter.getBondedDevices().size() > 0) {
      Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
      if (bondedDevices != null) {
        if (bondedDevices.size() == 1) {
          BluetoothDevice device = bondedDevices.iterator().next();
          if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
            Log.d(getTag(), "One BT Device: " + device.getName());
            suggestedDevice = device;
          }
          if (device.getAddress().equals(addressOfDevice)) {
            Log.d(getTag(), "Selected BT Device: " + device.getName());
            return device;
          }
        } else if (bondedDevices.size() > 0) {
          for (BluetoothDevice device : bondedDevices) {
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
              if (suggestedDevice == null) {
                if (device.getName().toLowerCase().contains("glass")) {
                  Log.d(getTag(), "BT Device: " + device.getName());
                  suggestedDevice = device;
                }
              }
            }
            if (device.getAddress().equals(addressOfDevice)) {
              Log.d(getTag(), "Selected BT Device: " + device.getName());
              return device;
            }
          }
        }
      }
    }

    if (suggestedDevice != null && addressOfDevice == null) {
      Log.d(getTag(), "Using best guess device: " + suggestedDevice.getName());
      return suggestedDevice;
    }
    Log.d(getTag(), "Selected BT Device not found.");
    return null;
  }
Example #13
0
  public DeviceData(BluetoothDevice device, String emptyName) {
    name = device.getName();
    address = device.getAddress();
    bondState = device.getBondState();

    if (name == null || name.isEmpty()) name = emptyName;
    deviceClass = device.getBluetoothClass().getDeviceClass();
    majorDeviceClass = device.getBluetoothClass().getMajorDeviceClass();
    uuids = BluetoothUtils.getDeviceUuids(device);
  }
Example #14
0
 @Override
 public void onReceive(Context context, Intent intent) {
   BluetoothDevice device = null;
   if (intent
       .getAction()
       .equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { // 配对状态改变时,的广播处理
     device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     if (device.getBondState() == BluetoothDevice.BOND_BONDED) mbBonded = true; // 蓝牙配对设置成功
     else mbBonded = false; // 蓝牙配对进行中或者配对失败
   }
 }
Example #15
0
  private boolean okToConnect(BluetoothDevice device) {
    AdapterService adapterService = AdapterService.getAdapterService();
    // check if it is inbound connection in Quiet mode, priority and Bond status
    // to decide if its ok to allow this connection
    if ((adapterService == null)
        || ((adapterService.isQuietModeEnabled()) && (mTargetDevice == null))
        || (BluetoothProfile.PRIORITY_OFF == getPriority(device))
        || (device.getBondState() == BluetoothDevice.BOND_NONE)) return false;

    return true;
  }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice bluetoothDevice =
                intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
              _bluetoothDevicesAdapter.addItem(bluetoothDevice);
              _bluetoothDevicesAdapter.notifyDataSetChanged();
            }
          }
        }
  private void updateBondedBluetoothDevices() {
    mBondedDevices.clear();

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
      Set<BluetoothDevice> devices = adapter.getBondedDevices();
      if (devices != null) {
        for (BluetoothDevice device : devices) {
          if (device != null && device.getBondState() != BluetoothDevice.BOND_NONE) {
            mBondedDevices.add(device);
          }
        }
      }
    }
  }
        public void onReceive(final Context pContext, final Intent pIntent) {
          final String action = pIntent.getAction();

          if (action.equals(BluetoothDevice.ACTION_FOUND)) {
            final BluetoothDevice device = pIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.add(
                  device.getName() + "\n" + device.getAddress());
            }
          } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
            BluetoothListDevicesActivity.this.setProgressBarIndeterminateVisibility(false);
            BluetoothListDevicesActivity.this.setTitle("Select a device to connect...");
            if (BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.getCount() == 0) {
              BluetoothListDevicesActivity.this.mNewDevicesArrayAdapter.add("No devices found!");
            }
          }
        }
  /**
   * When the device is bonded and has the Generic Attribute service and the Service Changed
   * characteristic this method enables indications on this characteristic. In case one of the
   * requirements is not fulfilled this method returns <code>false</code>.
   *
   * @param gatt the gatt device with services discovered
   * @return <code>true</code> when the request has been sent, <code>false</code> when the device is
   *     not bonded, does not have the Generic Attribute service, the GA service does not have the
   *     Service Changed characteristic or this characteristic does not have the CCCD.
   */
  private boolean ensureServiceChangedEnabled(final BluetoothGatt gatt) {
    if (gatt == null) return false;

    // The Service Changed indications have sense only on bonded devices
    final BluetoothDevice device = gatt.getDevice();
    if (device.getBondState() != BluetoothDevice.BOND_BONDED) return false;

    final BluetoothGattService gaService = gatt.getService(GENERIC_ATTRIBUTE_SERVICE);
    if (gaService == null) return false;

    final BluetoothGattCharacteristic scCharacteristic =
        gaService.getCharacteristic(SERVICE_CHANGED_CHARACTERISTIC);
    if (scCharacteristic == null) return false;

    Logger.i(mLogSession, "Service Changed characteristic found on a bonded device");
    return enableIndications(scCharacteristic);
  }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getText(R.string.none_found).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          }
        }
 boolean okToConnect(BluetoothDevice device) {
   AdapterService adapterService = AdapterService.getAdapterService();
   int priority = mService.getPriority(device);
   boolean ret = false;
   // check if this is an incoming connection in Quiet mode.
   if ((adapterService == null)
       || ((adapterService.isQuietModeEnabled() == true) && (mTargetDevice == null))) {
     ret = false;
   }
   // check priority and accept or reject the connection. if priority is undefined
   // it is likely that our SDP has not completed and peer is initiating the
   // connection. Allow this connection, provided the device is bonded
   else if ((BluetoothProfile.PRIORITY_OFF < priority)
       || ((BluetoothProfile.PRIORITY_UNDEFINED == priority)
           && (device.getBondState() != BluetoothDevice.BOND_NONE))) {
     ret = true;
   }
   return ret;
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if ((device.getBondState() != BluetoothDevice.BOND_BONDED)
                && (device.getBluetoothClass().getDeviceClass()
                    == BluetoothClass.Device.TOY_ROBOT)) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
              findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
              findViewById(R.id.no_devices).setVisibility(View.GONE);
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle("Select device");
            findViewById(R.id.button_scan).setVisibility(View.VISIBLE);
          }
        }
  public static boolean pair(String strAddr, String strPsw) {
    boolean result = false;

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    bluetoothAdapter.cancelDiscovery();
    if (!bluetoothAdapter.isEnabled()) {
      bluetoothAdapter.enable();
    }
    if (!BluetoothAdapter.checkBluetoothAddress(strAddr)) { // 检查蓝牙地址是否有效
      Log.d("mylog", "devAdd un effient!");
    }

    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(strAddr);
    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
      try {
        Log.d("mylog", "NOT BOND_BONDED");

        setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对

        createBond(device.getClass(), device);

        //				remoteDevice = device; // 配对完毕就把这个设备对象传给全局的remoteDevice
        result = true;
      } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("mylog", "setPiN failed!");
        e.printStackTrace();
      } //
    } else {
      Log.d("mylog", "HAS BOND_BONDED");
      try {
        createBond(device.getClass(), device);
        setPin(device.getClass(), device, strPsw); // 手机和蓝牙采集器配对
        createBond(device.getClass(), device);
        result = true;
      } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("mylog", "setPiN failed!");
        e.printStackTrace();
      }
    }
    return result;
  }
Example #24
0
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              String dev_name = device.getName();
              if (dev_name.substring(0, c_id_length) == cluster_id) {
                discovered_devices.add(device.getName());
              }
            }
            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            outData();
          }
        }
Example #25
0
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Log.d("bbtt", "device:"+device.getName()+":"+device.getAddress());

            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              if (devices != null) {
                // devices.add(device);
                addDevice(device);
              }
              if (DEBUG) Log.d("bbtt", "device2:" + device.getName() + ":" + device.getAddress());
            }

            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // if(devices!=null){
            // Log.d("testthread", "scanning devices finish ...."+devices.size());
            // }
            stopTimeoutTimer(); // 停掉计时
            new Thread() {
              public void run() {
                try {
                  if (!BaseConnectOBDService.addingCar) {
                    // Log.d("testthread", "cricleConnect...");
                    cricleConnetOBD(devices);
                  }
                  connetState = 1; // 查找结束
                } catch (InterruptedException e) {
                  connetState = 1; // 查找结束
                  e.printStackTrace();
                }
              }
            }.start();
          }
        }
Example #26
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    DeviceInfoHolder holder = null;
    BluetoothDevice deviceInfo = deviceList.get(position);
    if (convertView == null) {
      holder = new DeviceInfoHolder();
      convertView = mInflator.inflate(R.layout.listitem_device, null);
      holder.deviceName = ((TextView) convertView.findViewById(R.id.device_name));
      holder.deviceAddress = ((TextView) convertView.findViewById(R.id.device_address));
      holder.deviceBondStatus = ((TextView) convertView.findViewById(R.id.tv_status));

      convertView.setTag(holder);
    } else {
      holder = (DeviceInfoHolder) convertView.getTag();
    }

    holder.deviceName.setTextColor(Color.BLUE);
    final String deviceName = deviceInfo.getName();
    if (deviceName != null && deviceName.length() > 0) {
      holder.deviceName.setText(deviceName);
    } else {
      holder.deviceName.setText(R.string.unknown_device);
    }

    //        holder.deviceAddress.setText(
    //                deviceInfo.getAddress() + "  RSSI:" + String.valueOf(rssis.get(position)));
    holder.deviceAddress.setText(deviceInfo.getAddress());
    int bondStatus = deviceInfo.getBondState();
    switch (bondStatus) {
      case BluetoothDevice.BOND_BONDED:
        holder.deviceBondStatus.setText(R.string.bond_bonded);
        break;
      case BluetoothDevice.BOND_BONDING:
        holder.deviceBondStatus.setText(R.string.bond_bonding);
        break;
      case BluetoothDevice.BOND_NONE:
        holder.deviceBondStatus.setText(R.string.bond_none);
        break;
    }
    return convertView;
  }
Example #27
0
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // When discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            if (mNewDevicesArrayAdapter.size() == 0) {
              String noDevices = getResources().getText(R.string.none_found).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          }
        }
Example #28
0
 @Override
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   // carga la vista de dispositivos encontrados
   findViewById(R.id.subtitulo).setVisibility(View.VISIBLE);
   // Si se encuentra un device
   if (BluetoothDevice.ACTION_FOUND.equals(action)) {
     // recupera el objeto device del intent
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     // saltar si ya esta vinculado
     if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
       arrayDevicesEncontrados.add(device.getName() + "\n" + device.getAddress());
     }
     // cuando termina el discovery, cambia la vista
   } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
     bolita.setVisibility(View.GONE);
     if (arrayDevicesEncontrados.getCount() == 0) {
       String noDevices = getResources().getText(R.string.noencontrados).toString();
       arrayDevicesEncontrados.add(noDevices);
     }
   }
 }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // when discovery finds a device
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // get the device object from intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // if its already paired, skip it because its on the list
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // when discovery finsihed, change the title
            else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              setProgressBarIndeterminateVisibility(false);
              setTitle(R.string.select_device);
              if (mNewDevicesArrayAdapter.getCount() == 0) {
                String noDevices = getResources().getText(R.string.none_found).toString();
                mNewDevicesArrayAdapter.add(noDevices);
              }
            }
          }
        }
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
              String entry =
                  device.getName()
                      + " ("
                      + getDeviceMajorClassName(device.getBluetoothClass().getMajorDeviceClass())
                      + ")"
                      + "\n"
                      + device.getAddress();

              if (mNewDevicesArrayAdapter.getPosition(entry) == -1)
                mNewDevicesArrayAdapter.add(entry);
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.text_select_device);
            findViewById(R.id.button_scan).setVisibility(View.VISIBLE);

            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getText(R.string.text_no_new_devices).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
            int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1);

            if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) {
              String pairingPasskey = intent.getStringExtra(BluetoothDevice.EXTRA_PAIRING_KEY);

              if (DBG) Log.d(TAG, "Passkey :" + pairingPasskey);

            } else if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_PIN) {

              if (DBG) Log.d(TAG, "Pairing via PIN");
            }
          } else if (BluetoothDevice.ACTION_UUID.equals(action)) {

          } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
            int currBondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Unpaired
            if (prevBondState == BluetoothDevice.BOND_BONDED
                && currBondState == BluetoothDevice.BOND_NONE) {

              Toast.makeText(
                      getApplicationContext(), "Unpaired " + device.getName(), Toast.LENGTH_SHORT)
                  .show();

            } else if (prevBondState == BluetoothDevice.BOND_BONDING
                && currBondState == BluetoothDevice.BOND_BONDED) {

              Toast.makeText(
                      getApplicationContext(),
                      "Paired with " + device.getName(),
                      Toast.LENGTH_SHORT)
                  .show();

              String entry =
                  device.getName()
                      + " ("
                      + getDeviceMajorClassName(device.getBluetoothClass().getMajorDeviceClass())
                      + ")"
                      + "\n"
                      + device.getAddress();

              mNewDevicesArrayAdapter.remove(entry);
            }

            getPairedDevices();
          }
        }