Ejemplo n.º 1
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();
            }
          }
        }
Ejemplo n.º 2
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();
          }
        }
  @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");
  }
        // 検出されたデバイスからのブロードキャストを受ける
        @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) {
   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;
       }
     }
   }
 }
  @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");
            }
          }
        }
      }
    }
  }