@Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            Intent scan_intent = new Intent(DisplaySoundActivity.this, DeviceScanActivity.class);
            startActivity(scan_intent);
            invalidateOptionsMenu();
            // unregisterReceiver(mGattUpdateReceiver);
            finish();

          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            // ** changed //
            services = mBluetoothLeService.getSupportedGattServices();
            getCharacteristics(services);
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            onDataRecived(intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA));
          }
        }
Example #2
0
        @Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) // Gatt连接成功
          {
            mConnected = true;
            status = "connected";
            // 更新连接状态
            updateConnectionState(status);
            //				log("ble device connected");

          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED // Gatt连接失败
              .equals(action)) {
            mConnected = false;
            status = "disconnected";
            // 更新连接状态
            updateConnectionState(status);
            //				log("ble device disconnected");

          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED // 发现GATT服务器
              .equals(action)) {
            // Show all the supported services and characteristics on the
            // user interface.
            // 获取设备的所有蓝牙服务
            displayGattServices(mBluetoothLeService.getSupportedGattServices());
            //				System.out.println("BroadcastReceiver :"+ "device SERVICES_DISCOVERED");
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) // 有效数据
          {
            // 处理发送过来的数据
            //				String revMsg = intent.getExtras().getString(BluetoothLeService.EXTRA_DATA);
          }
        }
        @SuppressLint("DefaultLocale")
        @Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          System.out.println("mGattUpdateReceiver->onReceive->action=" + action);
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            mHandler.removeCallbacks(mConnectingOverTimeRunnable);

          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            mConnectionState = connectionStateEnum.isToScan;
            onConectionStateChange(mConnectionState);
            mHandler.removeCallbacks(mDisonnectingOverTimeRunnable);
            mBluetoothLeService.close();
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            // Show all the supported services and characteristics on the user interface.
            for (BluetoothGattService gattService :
                mBluetoothLeService.getSupportedGattServices()) {
              System.out.println(
                  "ACTION_GATT_SERVICES_DISCOVERED  " + gattService.getUuid().toString());
            }
            getGattServices(mBluetoothLeService.getSupportedGattServices());
          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            if (mSCharacteristic == mModelNumberCharacteristic) {
              if (intent
                  .getStringExtra(BluetoothLeService.EXTRA_DATA)
                  .toUpperCase()
                  .startsWith("DF BLUNO")) {
                mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, false);
                mSCharacteristic = mCommandCharacteristic;
                mSCharacteristic.setValue(mPassword);
                mBluetoothLeService.writeCharacteristic(mSCharacteristic);
                mSCharacteristic.setValue(mBaudrateBuffer);
                mBluetoothLeService.writeCharacteristic(mSCharacteristic);
                mSCharacteristic = mSerialPortCharacteristic;
                mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, true);
                mConnectionState = connectionStateEnum.isConnected;
                onConectionStateChange(mConnectionState);

              } else {
                Toast.makeText(mainContext, "Please select DFRobot devices", Toast.LENGTH_SHORT)
                    .show();
                mConnectionState = connectionStateEnum.isToScan;
                onConectionStateChange(mConnectionState);
              }
            } else if (mSCharacteristic == mSerialPortCharacteristic) {
              onSerialReceived(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
            }

            System.out.println(
                "displayData " + intent.getStringExtra(BluetoothLeService.EXTRA_DATA));

            //
            //	mPlainProtocol.mReceivedframe.append(intent.getStringExtra(BluetoothLeService.EXTRA_DATA)) ;
            //            	System.out.print("mPlainProtocol.mReceivedframe:");
            //            	System.out.println(mPlainProtocol.mReceivedframe.toString());

          }
        }
 @Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
     mConnected = true;
     updateConnectionState(R.string.connected);
     listenToCalipersDataButton();
   } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
     mConnected = false;
     updateConnectionState(R.string.disconnected);
     clearUI();
   } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
     displayGattServices(mBluetoothLeService.getSupportedGattServices());
   } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
     displayData(intent.getStringExtra(EXTRA_VALUE));
   } else {
     Log.e(TAG, "Received something else");
   }
 }
Example #5
0
 @Override
 public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();
   if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
     mConnected = true;
     updateConnectionState(R.string.connected);
     invalidateOptionsMenu();
   } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
     mConnected = false;
     updateConnectionState(R.string.disconnected);
     invalidateOptionsMenu();
     clearUI();
   } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
     // Show all the supported services and characteristics on the user interface.
     displayGattServices(mBluetoothLeService.getSupportedGattServices());
   } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
     displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
   }
 }
Example #6
0
        @Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            Log.i("BLE", "connect is on");
          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            try {

              // move to the device scan
              Intent deviceScanIntent = new Intent(getBaseContext(), DeviceScanActivity.class);
              deviceScanIntent.putExtra("disconnect", true);
              startActivity(deviceScanIntent);

            } catch (Exception e) {
              e.printStackTrace();
            }

          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            setVisibility(TREATMENT_SERVICE_FOUND);
            Toast.makeText(
                    MainActivity.this,
                    getBaseContext()
                        .getResources()
                        .getString(R.string.toast_successfull_connection),
                    Toast.LENGTH_SHORT)
                .show();

          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {

            if (intent.getStringExtra("voltage") != null) {
              // convert hex string to short
              short volt = (short) Integer.parseInt(intent.getStringExtra("voltage"), 16);
              // set mV to V
              volt = (short) (volt / 1000);

              String voltText = "";
              voltText = "" + volt + " V";
              // show volt
              voltageTextView.setText(voltText);

              if (!polarityChanged) { // POSITIVE
                // check if volt from device and volt from screen are same

                if ((int) volt < (textViewToInt(volts) - 1)
                    && ((int) volt > (textViewToInt(volts) + 1))) {

                  stopTreatment();
                  Toast.makeText(
                          MainActivity.this, "Treatment stopped: wrong voltage", Toast.LENGTH_SHORT)
                      .show();
                }
              } else { // NEGATIVE
                // check if volt from device and volt from screen are same

                if ((int) volt < (textViewToInt(volts) - 1)
                    && ((int) volt
                        > (textViewToInt(volts) + 1))) { // convert from screen volt to negative

                  stopTreatment();
                  Toast.makeText(
                          MainActivity.this, "Treatment stopped: wrong voltage", Toast.LENGTH_SHORT)
                      .show();
                }
              }

            } else if (intent.getStringExtra("status") != null) {
              Log.i(TAG, "polarity data available");
              // check polarity state
              if (intent.getStringExtra("status").equals("3")) { // 3 == NEGATIVE
                // set positive or negative to screen
                // polarityTextView.setText("P-");
                // stopTreatment();
                // Toast.makeText(MainActivity.this, "Treatment stopped, wrong polarity",
                // Toast.LENGTH_SHORT).show();
                Log.i(TAG, "Polarity: NEGATIVE");
              } else { // POSITIVE
                // polarityTextView.setText("P+");
                Log.i(TAG, "Polarity: POSITIVE");
              }
              if (!polarityChanged) { // POSITIVE
                polarityTextView.setText("P+");
              } else {
                polarityTextView.setText("P-");
              }
            }
          }
        }
        @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);
                  }
                }
              }
            }
          }
        }