Esempio n. 1
0
        @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();
                  }
                });
          }
        }
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            Toast.makeText(
                    getApplicationContext(), "Device found" + device.getName(), Toast.LENGTH_SHORT)
                .show(); // Device found
            if (device.getName().equals("SM-T325")) {
              visits++;
              ((TextView) findViewById(R.id.textView))
                  .setText("Number of visits this month: " + MainActivity.visits + " times");
            }
          } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Action connected", Toast.LENGTH_SHORT)
                .show(); // Device is now connected
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Discovery finished", Toast.LENGTH_SHORT)
                .show(); // Done searching
          } else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Disconnect Requested", Toast.LENGTH_SHORT)
                .show(); // Device is about to disconnect
          } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            Toast.makeText(getApplicationContext(), "Disconnected", Toast.LENGTH_SHORT)
                .show(); // Device has disconnected
          }
        }
        @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);
            }
          }
        }
        @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);
            }
          }
        }
        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);
          }
        }
Esempio n. 6
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);
            Short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);

            mNewDevicesArrayAdapter.add(
                device.getName() + "\n" + "RSSI = " + rssi + "\n" + device.getAddress());

            // log for test
            if (device.getName().equals("Zhenan")) {
              DataLogger dl =
                  new DataLogger(device.getName(), device.getAddress(), rssi.intValue());
            }
            // When discovery is finished, change the Activity title
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.scan_finish);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
              String noDevices = getResources().getText(R.string.none_found).toString();
              mNewDevicesArrayAdapter.add(noDevices);
            }
          }
        }
Esempio n. 7
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());

              // 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) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    String action = intent.getAction();
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
      int state = BluetoothAdapter.getDefaultAdapter().getState();
      if (state == BluetoothAdapter.STATE_ON) {
        ShadowTalkLog.i("STATE_ON");
      } else if (state == BluetoothAdapter.STATE_OFF) {
        ShadowTalkLog.i("STATE_OFF");

      } else if (state == BluetoothAdapter.STATE_TURNING_ON) {
        ShadowTalkLog.i("STATE_TURNING_ON");

      } else if (state == BluetoothAdapter.STATE_TURNING_OFF) {
        ShadowTalkLog.i("STATE_TURNING_OFF");
      }
    } else if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {

    } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {

    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

    }
    // throw new UnsupportedOperationException("Not yet implemented");
  }
Esempio n. 9
0
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          // ada device baru
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // ambil objek BluetoothDevice dari Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            arrayPairedDevices.add(device);
            // tulis nama dan MAC address ke ListView
            adapter.add("Baru:" + device.getName() + "\n" + device.getAddress());
            // refresh listview, JANGAN LUPA!!
            adapter.notifyDataSetChanged();
          } else
          // mulai proses discovery, untuk debug saja, memastikan proses dimulai
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Toast toast =
                Toast.makeText(
                    getApplicationContext(), "Mulai proses discovery", Toast.LENGTH_LONG);
            toast.show();
          } else
          // mulai proses discovery, untuk debug saja, memastikan proses dimulai
          if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Toast toast =
                Toast.makeText(
                    getApplicationContext(), "Proses discovery selesai", Toast.LENGTH_LONG);
            toast.show();
          }
        }
Esempio n. 10
0
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          bluetoothImage.setBackgroundResource(R.drawable.discovering_animation);
          anim = (AnimationDrawable) bluetoothImage.getBackground();
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            discoverBT.setText(R.string.cancel_discovery);
            enableBT.setEnabled(false);
            checkBT.setEnabled(false);
            anim.start();
            Toast.makeText(MainActivity.this, R.string.discovering, Toast.LENGTH_SHORT).show();
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            discoverBT.setText(R.string.discover_devices);
            enableBT.setEnabled(true);
            checkBT.setEnabled(true);
            anim.stop();
            bluetoothImage.setBackground(getResources().getDrawable((R.drawable.btooth_on)));
            Toast.makeText(MainActivity.this, R.string.discovery_finished, Toast.LENGTH_SHORT)
                .show();
          } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device =
                (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            if (!tmpBtChecker.contains(device)) {
              tmpBtChecker.add(device);
              discoveredDevicesArList.add(device.getName() + "\n" + device.getAddress());
              adapterForDiscoveredDevices.notifyDataSetChanged();
            }
          }
        }
Esempio n. 11
0
  @Override
  public void onReceive(Context context, Intent intent) {

    if (bluetoothAdapter == null) {
      return;
    }

    String action = intent.getAction();
    // When discovery finds a device

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

      if (device == null) {
        return;
      }

      String name = device.getName();
      if (name == null) {
        return;
      }

      if (DEVICE_NAME_LIST.contains(name)) {
        deviceNames.put(device.getName(), device.getAddress());
        devices.put(device.getName(), device);

        cancelDiscovery();

        try {
          byte[] pin = ByteBuffer.allocate(4).putInt(1234).array();
          Method m = device.getClass().getMethod("setPin", byte[].class);
          m.invoke(device, pin);

          device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
          device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device);
        } catch (Exception e) {
          e.printStackTrace();
        }

        sendCurrentStateMessage(
            BluetoothManagerState.DEVICE_FOUND,
            new DeviceDTO(device.getName(), device.getAddress()));
      }
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
      bluetoothAdapter.cancelDiscovery();
    }
  }
        // 検出されたデバイスからのブロードキャストを受ける
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          String dName = null;
          BluetoothDevice foundDevice;
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.d("BP", "スキャン開始");
          }
          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // デバイスが検出された
            Log.d("BP", "デバイス検出");
            foundDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            foundDeviceList.add(foundDevice);
            if ((dName = foundDevice.getName()) != null) {
              Log.d("BP", "デバイス: " + dName);
              BtClientRead thread = new BtClientRead(mContext, foundDevice, mBtAdapter);
              thread.start();
              Log.d("BP", "BtClientThreadスタート");
              // ArrayList<String> dataList = null;
              data = "";
              try {
                data = thread.getValue();
                thread.cancel();
                Log.d("BP", "BtClientThreadストップ");
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              linear_layout.removeView(waitText);
              linear_layout.removeView(progressBar);
              String[] dataArray = data.split("&");
              String name = dataArray[3];
              nonPairedDeviceAdapter.add("" + name);
              Log.d("BP", "デバイス検出終了");

              // if(foundDevice.getBondState() != BluetoothDevice.BOND_BONDED){
              // 接続したことのないデバイスのみアダプタに詰める
              // nonPairedDeviceAdapter.add(dName + "\n" + foundDevice.getAddress());
              // Log.d("ACTION_FOUND", dName);
              // }
            }
            // nonpairedList.setAdapter(nonPairedDeviceAdapter);
          }

          if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Log.d("BP", "スキャン終了");
          }
        }
        @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);
            }
          }
        }
Esempio n. 14
0
        @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);
          }
        }
Esempio n. 15
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();
          }
        }
 @Override
 public void onReceive(Context context, Intent intent) {
   synchronized (this) {
     if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction())) {
       mFiredFlags |= DISCOVERY_STARTED_FLAG;
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())) {
       mFiredFlags |= DISCOVERY_FINISHED_FLAG;
     } else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(intent.getAction())) {
       int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR);
       assertNotSame(mode, BluetoothAdapter.ERROR);
       switch (mode) {
         case BluetoothAdapter.SCAN_MODE_NONE:
           mFiredFlags |= SCAN_MODE_NONE_FLAG;
           break;
         case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
           mFiredFlags |= SCAN_MODE_CONNECTABLE_FLAG;
           break;
         case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
           mFiredFlags |= SCAN_MODE_CONNECTABLE_DISCOVERABLE_FLAG;
           break;
       }
     } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
       int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
       assertNotSame(state, BluetoothAdapter.ERROR);
       switch (state) {
         case BluetoothAdapter.STATE_OFF:
           mFiredFlags |= STATE_OFF_FLAG;
           break;
         case BluetoothAdapter.STATE_TURNING_ON:
           mFiredFlags |= STATE_TURNING_ON_FLAG;
           break;
         case BluetoothAdapter.STATE_ON:
           mFiredFlags |= STATE_ON_FLAG;
           break;
         case BluetoothAdapter.STATE_TURNING_OFF:
           mFiredFlags |= STATE_TURNING_OFF_FLAG;
           break;
       }
     }
   }
 }
Esempio n. 17
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();
          }
        }
Esempio n. 18
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);
            }
          }
        }
Esempio n. 19
0
        @Override
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // ... //Device found
          } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Device is now connected
            PrintHelper.SetIsConnected(true);
            AppGlobals.printerTestDialogFragment.SetPrinterStatus();
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // Done searching
          } else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
            // Device is about to disconnect
          } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // Device has disconnected
            PrintHelper.SetIsConnected(false);
            AppGlobals.printerTestDialogFragment.SetPrinterStatus();
          }
        }
 public void onReceive(Context context, Intent intent) {
   String action = intent.getAction();
   Log.d("Блютуз", "Чет пришло на ресивер: " + intent.getAction());
   if (BluetoothDevice.ACTION_FOUND.equals(action)) {
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     Log.d("Блютуз", "Я тебя нашел, " + device.getName() + "!!!");
     if (device.getName().contains(BluetoothInterface.PREFIX))
       onNewServerFound(device.getName().substring(BluetoothInterface.PREFIX.length()));
   }
   if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
     onDiscoveryFinish();
   }
   if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
     if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0) == BluetoothAdapter.STATE_ON) {
       onAdapterOn();
     } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)
         == BluetoothAdapter.STATE_OFF) {
       onAdapterOff();
     }
   }
 }
Esempio n. 21
0
        public void onReceive(Context context, Intent intent) {

          String action = intent.getAction();

          if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            if (dialog != null) {
              dialog.dismiss();
            }
          } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {

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

            String discoveryName = device.getName();
            /* if (discoveryNameService != null)
            {
                discoveryName = discoveryNameService.foundDevice(device.getAddress(), device.getName());
            }*/
            BTArrayAdapter.add(discoveryName + "\n" + device.getAddress());
            BTArrayAdapter.notifyDataSetChanged();
          }
        }
Esempio n. 22
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);
     }
   }
 }
Esempio n. 23
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 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();
          }
        }
Esempio n. 25
0
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();

          if (BluetoothDevice.ACTION_FOUND.equals(action)) {

            BluetoothDevice newDev =
                (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.i(TAG, "A new device has been found " + newDev.getName());

            Intent ip = new Intent(DashboardTab.MOBITRADE_NEW_PEER_DISCOVERED);
            Bundle bp = new Bundle();
            bp.putString("NAME", newDev.getName());
            ip.putExtras(bp);

            sendBroadcast(ip);

            if (newDev != null) {
              MobiTradeProtocol.GetMobiTradeProtocol().AddNewDiscoveredDevice(newDev);
            }
          } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // End of discovery
            Log.i(TAG, "Discovery Finished @ " + DatabaseHelper.getCurrentTime());

            // Verify if the bluetooth server is still running or not
            MobiTradeProtocol.GetMobiTradeProtocol().StartMobiTradeBluetoothListener();

            // Mix the discovered devices with the already paired ones and start new sessions
            {
              Set<BluetoothDevice> pairedSet =
                  BluetoothAdapter.getDefaultAdapter().getBondedDevices();
              for (BluetoothDevice pd : pairedSet) {
                MobiTradeProtocol.GetMobiTradeProtocol().AddNewDiscoveredDevice(pd);
              }
            }

            int nbrDiscoveredNodes =
                MobiTradeProtocol.GetMobiTradeProtocol().GetNumberOfDiscoveredDevices();
            if (nbrDiscoveredNodes > 0) {

              Intent i = new Intent(DashboardTab.MOBITRADE_DISCOVERY_1);
              Bundle b = new Bundle();
              b.putInt("NUMBER", nbrDiscoveredNodes);
              i.putExtras(b);
              sendBroadcast(i);

              MobiTradeProtocol.GetMobiTradeProtocol()
                  .StartMobiTradeNegociationWith(
                      MobiTradeProtocol.GetMobiTradeProtocol().getNextDevice());
            } else {
              Log.i(TAG, "No new devices discovered, restarting the discovery");
              sendBroadcast(new Intent(DashboardTab.MOBITRADE_DISCOVERY_0));

              // We start a new discovery only if there is no running sessions
              if (MobiTradeProtocol.GetMobiTradeProtocol().GetNumberOfSessions() == 0
                  && alwaysInDiscovery == 1)
                MobiTradeProtocol.GetMobiTradeProtocol().StartDiscovery();
            }

          } else if (action.compareTo(DashboardTab.MOBITRADE_NEW_CONTENT_RECEIVED) == 0) {
            Bundle b = intent.getExtras();
            String content = b.getString(Content.CONTENT_NAME);

            // Load a possibly updated configuration entries
            int useVibratorNotification =
                DatabaseHelper.getDBManager().getConfigVibratorNotification();

            // Start the vibration
            if (useVibratorNotification == 1) {
              Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
              vibrator.vibrate(DashboardTab.VIBRATION_CONTENT_RECEIVED);
            }

            // Send a status bar notification

            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

            // Initiate the notification
            int icon = R.drawable.mobitradeiconstatusbar2424;
            CharSequence tickerText = "New content received: " + content;
            long when = System.currentTimeMillis();

            Notification notification = new Notification(icon, tickerText, when);

            CharSequence contentTitle = "MobiTrade, new content received";
            CharSequence contentText = "New content received: " + content;

            PendingIntent contentIntent = null;

            Intent notificationIntent = new Intent(MobiTradeService.this, MobiTradeMain.class);
            contentIntent =
                PendingIntent.getActivity(MobiTradeService.this, 0, notificationIntent, 0);

            notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

            mNotificationManager.notify(1, notification);

          } else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
            // End of discovery
            Log.i(
                TAG,
                "Bluetooth Scan Mode changed, current adapter status: " + bluetoothAdapterStatus);

            if (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_OFF
                && bluetoothAdapterStatus == 1) {
              Log.i(TAG, "Bluetooth adapter is turned off, pausing the service");
              // The bluetooth adapter is switched off

              // Stopping MobiTrade Bluetooth server
              MobiTradeProtocol.GetMobiTradeProtocol().StopMobiTradeBluetoothListener();

              // Cancel the session timeOut timer
              MobiTradeProtocol.GetMobiTradeProtocol().CancelSessionsTimer();

              // Stopping all ongoing sessions
              MobiTradeProtocol.GetMobiTradeProtocol().StopOngoingSessions();

              // Notifies the dashboar that we are closing
              sendBroadcast(new Intent(DashboardTab.MOBITRADE_OFF));

              bluetoothAdapterStatus = 0;

            } else if (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_ON
                && bluetoothAdapterStatus == 0) {
              // The bluetooth adapter is switched on
              Log.i(TAG, "Bluetooth adapter is turned on, re starting");

              // Start bluetooth server
              MobiTradeProtocol.GetMobiTradeProtocol().StartMobiTradeBluetoothListener();

              MobiTradeProtocol.GetMobiTradeProtocol().StartSessionsTimer();

              // Verify if we should stay always in discovery mode or not
              alwaysInDiscovery = DatabaseHelper.getDBManager().getConfigAlwaysDiscovering();

              // Registers the broadcast receiver
              RegisterBroadcastReceiver();

              // Init the temporary status used via the Dashboard
              DatabaseHelper.getDBManager().updateConfigLiveStatus("none");
              DatabaseHelper.getDBManager().updateConfigLiveStatus2("none");

              if (MobiTradeProtocol.GetMobiTradeProtocol().GetNumberOfSessions() == 0
                  && !BluetoothAdapter.getDefaultAdapter().isDiscovering()) {
                MobiTradeProtocol.GetMobiTradeProtocol().StartDiscovery();
              }

              // Notifies the dashboar that we are closing
              sendBroadcast(new Intent(DashboardTab.MOBITRADE_ON));

              bluetoothAdapterStatus = 1;
            }

            int mode = intent.getExtras().getInt(BluetoothAdapter.EXTRA_SCAN_MODE);
            if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
              // Still in the discoverable mode
            } else if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
              // Not in the discoverable mode but still can receive connections
              // MakeTheDeviceDiscoverable();
              if (BluetoothAdapter.getDefaultAdapter().isEnabled())
                sendBroadcast(new Intent(DashboardTab.MOBITRADE_DISCOVERABLE_MODE_OFF));
            } else if (mode == BluetoothAdapter.SCAN_MODE_NONE) {
              // Not in the discoverble mode and cannot receive anymore connections
              if (BluetoothAdapter.getDefaultAdapter().isEnabled())
                sendBroadcast(new Intent(DashboardTab.MOBITRADE_DISCOVERABLE_MODE_OFF));
            }

          } else if (action.equals(DashboardTab.START_NEW_DISCOVERY_SESSION)) {
            // We start a new discovery session if and only if there is no running sessions
            if (MobiTradeProtocol.GetMobiTradeProtocol().GetNumberOfSessions() == 0) {
              if (!BluetoothAdapter.getDefaultAdapter().isDiscovering())
                MobiTradeProtocol.GetMobiTradeProtocol().StartDiscovery();
            }

          } else if (action.equals(ConfigTab.DISCOVERY_MODE_CHANGED)) {
            int tmp = alwaysInDiscovery;
            alwaysInDiscovery = DatabaseHelper.getDBManager().getConfigAlwaysDiscovering();
            if (tmp == 0 && alwaysInDiscovery == 1) {
              MobiTradeProtocol.GetMobiTradeProtocol().StartDiscovery();
            }
          }
        }
  @Override
  public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    final String action = intent.getAction();

    DatabaseHandler dataEvent = DatabaseHandler.getInstance();
    dataEvent.setContext(context);

    btAdapter = BluetoothAdapter.getDefaultAdapter();
    String myDeviceMac = btAdapter.getAddress();

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
      final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
      switch (state) {
        case BluetoothAdapter.STATE_OFF:
          Log.i(TAG, "Bluetooth OFF");

          break;
        case BluetoothAdapter.STATE_TURNING_OFF:
          Log.i(TAG, "Turning OFF Bluetooth...");

          break;
        case BluetoothAdapter.STATE_ON:
          Log.i(TAG, "Bluetooth ON");

          break;
        case BluetoothAdapter.STATE_TURNING_ON:
          Log.i(TAG, "Turning ON Bluetooth...");
          break;
      }
    } else {
      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.d(TAG, "\n  Device: " + device.getName() + ", " + device);

        dataEvent
            .getInstance()
            .getDatabaseManager()
            .saveData(
                System.currentTimeMillis(),
                myDeviceMac,
                device.getName(),
                device.getAddress(),
                0,
                1,
                0,
                0,
                0);

      } else {
        if (BluetoothDevice.ACTION_UUID.equals(action)) {
          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
          Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
          Log.d(TAG, "\n  UUID Device: " + device.getName() + ", " + device);
          if (uuidExtra != null) {}

        } else {
          if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.d(TAG, "\nDiscovery Started");

          } else {
            if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
              Log.d(TAG, "\nDiscovery Finished");
            }
          }
        }
      }
    }
  }