Exemplo n.º 1
0
    @Override
    public void doConnectionHandover(NdefMessage handoverRequest, int handover, int record)
        throws IOException {

      byte[] remoteCollision = handoverRequest.getRecords()[handover + 1].getPayload();
      if (remoteCollision[0] == mCollisionResolution[0]
          && remoteCollision[1] == mCollisionResolution[1]) {
        return; // They'll have to try again.
      }
      boolean amServer =
          (remoteCollision[0] < mCollisionResolution[0]
              || (remoteCollision[0] == mCollisionResolution[0]
                  && remoteCollision[1] < mCollisionResolution[1]));

      if (mAlwaysClient) {
        amServer = false;
      }

      if (!mConnectionStarted) {
        synchronized (BluetoothConnecting.this) {
          if (!mConnectionStarted) {
            mConnectionStarted = true;
            mmBtConnected.beforeConnect(amServer);
          }
        }
      }
      if (!amServer) {
        // Not waiting for a connection:
        mAcceptThread.cancel();
        Uri uri = Uri.parse(new String(handoverRequest.getRecords()[record].getPayload()));
        UUID serviceUuid = UUID.fromString(uri.getPath().substring(1));
        int channel = -1;
        String channelStr = uri.getQueryParameter("channel");
        if (null != channelStr) {
          channel = Integer.parseInt(channelStr);
        }

        BluetoothDevice remoteDevice = mBluetoothAdapter.getRemoteDevice(uri.getAuthority());
        new ConnectThread(remoteDevice, serviceUuid, channel).start();
      }
    }
        @Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            mCurrentConnectedBLEAddr = mDeviceAddress;
            if (onConnectedListener != null) {
              onConnectedListener.onConnected(true);
            }
          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            mCurrentConnectedBLEAddr = null;
            if (onConnectedListener != null) {
              onConnectedListener.onConnected(false);
            }
          } else if (BluetoothLeService.ACTION_BATTERY_LEVEL.equals(action)) {
            int batteryLevel = intent.getIntExtra(BluetoothLeService.EXTRA_DATA, -1);
            Toast.makeText(
                    context, "battery level=" + Integer.toString(batteryLevel), Toast.LENGTH_SHORT)
                .show();
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            // displayGattServices(mBluetoothLEService.getSupportedGattServices());
            if (mBluetoothLeService != null)
              getCharacteristic(mBluetoothLeService.getSupportedGattServices());
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            //
            if (intent.hasExtra(BluetoothLeService.ACTION_TYPE)) {
              String service_type = intent.getStringExtra(BluetoothLeService.ACTION_TYPE);
              if (service_type.equals(BluetoothLeService.UUID_KEY_DEVICE_TRIGGER.toString())) {
                int vibrateCount;
                int severity = intent.getIntExtra(BluetoothLeService.EXTRA_DATA, 0);
                if (severity == BluetoothLeService.CHAR_VALUE_LOW) {
                  Toast.makeText(context, "Low severity key triggered", Toast.LENGTH_SHORT).show();
                  Log.e("BroadcastReceiver", "Low severity key triggered");
                  vibrateCount = 4;
                  while ((!mBluetoothLeService.EmergencyCancelled) && (vibrateCount > 0)) {
                    mBluetoothLeService.LowEmergencyVibrate(800);
                    mBluetoothLeService.StopLowVibrate(1000);
                    vibrateCount--;
                    Log.e("BroadcastReceiver", String.format("vibration %s", vibrateCount));
                  }
                  if (!mBluetoothLeService.EmergencyCancelled) {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("0828213175", null, "Test message", null, null);
                    mBluetoothLeService.LowEmergencyVibrate(1500);
                    mBluetoothLeService.StopLowVibrate(200);
                    // reset
                    mBluetoothLeService.EmergencyCancelled = false;
                    mBluetoothLeService.EmergencyTriggered = false;

                  } else {
                    mBluetoothLeService.StopLowVibrate(1);
                  }
                }
                if (severity == BluetoothLeService.CHAR_VALUE_HIGH) {
                  Toast.makeText(context, "High severity key triggered", Toast.LENGTH_SHORT).show();
                  Log.e("BroadcastReceiver", "High severity key triggered");
                  vibrateCount = 3;
                  while ((!mBluetoothLeService.EmergencyCancelled) && (vibrateCount > 0)) {
                    mBluetoothLeService.LowEmergencyVibrate(600);
                    mBluetoothLeService.StopLowVibrate(400);
                    vibrateCount--;
                    Log.e("BroadcastReceiver", String.format("vibration %s", vibrateCount));
                  }
                  if (!mBluetoothLeService.EmergencyCancelled) {
                    // signal emergency
                    mBluetoothLeService.LowEmergencyVibrate(1500);
                    mBluetoothLeService.StopLowVibrate(200);
                    // reset
                    mBluetoothLeService.EmergencyCancelled = false;
                    mBluetoothLeService.EmergencyTriggered = false;

                  } else {
                    mBluetoothLeService.StopLowVibrate(1);
                  }
                }
              }
            }
          }
        }
Exemplo n.º 3
0
 public void onConnected(ConnectionThread connectionThread) {
   if (!managedConnections.contains(connectionThread)) return;
   for (OnConnectedListener listener :
       Application.getInstance().getManagers(OnConnectedListener.class))
     listener.onConnected(connectionThread.getConnectionItem());
 }