private void getCharacteristics(List<BluetoothGattService> gattServices) {
    Boolean tx = false;
    Boolean rx = false;

    if (gattServices == null) return;

    List<BluetoothGattCharacteristic> gattCharacteristics =
        new ArrayList<BluetoothGattCharacteristic>();

    for (BluetoothGattService gattService : gattServices) {
      if (gattService.getUuid().equals(UART_UUID)) {
        gattCharacteristics = gattService.getCharacteristics();
        for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
          if (gattCharacteristic.getUuid().equals(TX_UUID)) {
            target_character = gattCharacteristic;
            // Sleep a bit
            try {
              Thread.sleep(200);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            sendCurrentTime();
            sendClearToSend();
          }
        }
      }
    }
  }
        @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());

          }
        }
Esempio n. 3
0
  public static boolean qppEnable(
      BluetoothGatt bluetoothGatt, String qppServiceUUID, String writeCharUUID) {
    resetQppField();
    if (qppServiceUUID != null) uuidQppService = qppServiceUUID;
    if (writeCharUUID != null) uuidQppCharWrite = writeCharUUID;
    if (bluetoothGatt == null || qppServiceUUID.isEmpty() || writeCharUUID.isEmpty()) {
      Log.e(TAG, "invalid arguments");
      return false;
    }

    BluetoothGattService qppService = bluetoothGatt.getService(UUID.fromString(qppServiceUUID));
    if (qppService == null) {
      Log.e(TAG, "Qpp service not found");
      return false;
    }

    List<BluetoothGattCharacteristic> gattCharacteristics = qppService.getCharacteristics();
    for (int j = 0; j < gattCharacteristics.size(); j++) {
      BluetoothGattCharacteristic chara = gattCharacteristics.get(j);
      if (chara.getUuid().toString().equals(writeCharUUID)) {
        // Log.i(TAG,"Wr char is "+chara.getUuid().toString());
        writeCharacteristic = chara;
      } else if (chara.getProperties() == BluetoothGattCharacteristic.PROPERTY_NOTIFY) {
        // Log.i(TAG,"NotiChar UUID is : "+chara.getUuid().toString());
        notifyCharacteristic = chara;
        arrayNtfCharList.add(chara);
      }
    }

    if (!setCharacteristicNotification(bluetoothGatt, arrayNtfCharList.get(0), true)) return false;

    notifyCharaIndex++;

    return true;
  }
  // Demonstrates how to iterate through the supported GATT Services/Characteristics.
  // In this sample, we populate the data structure that is bound to the ExpandableListView
  // on the UI.
  private void displayGattServices(List<BluetoothGattService> gattServices) {

    if (DEBUG) Log.d(TAG, "displayGattServices");

    if (gattServices == null) return;
    String uuid = null;
    String unknownServiceString = "Unknown service";
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
      HashMap<String, String> currentServiceData = new HashMap<String, String>();
      uuid = gattService.getUuid().toString();
      currentServiceData.put(LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));

      // If the service exists for HM 10 Serial, say so.
      if (SampleGattAttributes.lookup(uuid, unknownServiceString) == "HM 10 Serial") {
        if (DEBUG) Log.d(TAG, "Found serial service");
      } else {
        if (ERROR) Log.e(TAG, "Unable to find the serial service for BT device");
      }
      currentServiceData.put(LIST_UUID, uuid);
      gattServiceData.add(currentServiceData);

      // get characteristic when UUID matches RX/TX UUID
      characteristicTX = gattService.getCharacteristic(UUID_HM_RX_TX);
      characteristicRX = gattService.getCharacteristic(UUID_HM_RX_TX);
      if (characteristicRX != null) setCharacteristicNotification(characteristicRX, true);
    }
  }
  public void writeCharacter(String address) {

    BluetoothGattService alarmService = null;

    BluetoothGattCharacteristic alarmCharacter = null;

    if (mBluetoothGatt != null) {
      alarmService = mBluetoothGatt.getService(SERVIE_UUID);
    }
    if (alarmService == null) {
      showMessage("link loss Alert service not found!");
      return;
    }
    alarmCharacter =
        alarmService.getCharacteristic(UUID.fromString("00002a06-0000-1000-8000-00805f9b34fb"));
    if (alarmCharacter == null) {
      connect(address);
      showMessage("link loss Alert Level charateristic not found!");
      return;
    }
    alarmCharacter.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
    byte[] value = {0x01};
    alarmCharacter.setValue(value);
    mBluetoothGatt.writeCharacteristic(alarmCharacter);
  }
        @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();
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            BluetoothGattService gattService = mBluetoothLeService.getSoftSerialService();
            characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
            // characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
            characteristicRX = characteristicTX;

            if ((gattService != null) && (characteristicTX != null)) {
              mBluetoothLeService.setCharacteristicNotification(characteristicTX, true);

              isSerial.setText("Serial ready");
              updateReadyState(R.string.ready);
            } else {
              isSerial.setText("Serial can't be found");
            }

          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(mBluetoothLeService.EXTRA_DATA));
          }
        }
Esempio n. 7
0
  public ProgramInfo(byte[] mImage, SensorTagDevice device) {
    // this.mFileImgHdr = mFileImgHdr;
    mFileImgHdr = new ImageHdr(mImage);
    mFileBuffer = mImage;
    this.mBtLeService = BluetoothLeService.getInstance();
    this.deviceAddress = device.getAddress();

    BluetoothGattService mOadService = device.getOadService();
    BluetoothGattService mConnControlService = device.getConnControlService();
    // Characteristics list
    List<BluetoothGattCharacteristic> mCharListOad = mOadService.getCharacteristics();
    List<BluetoothGattCharacteristic> mCharListCc = mConnControlService.getCharacteristics();

    mServiceOk = mCharListOad.size() == 2 && mCharListCc.size() >= 3;
    if (mServiceOk) {
      mCharIdentify = mCharListOad.get(0);
      mCharBlock = mCharListOad.get(1);
      mCharBlock.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
      mCharConnReq = mCharListCc.get(1);

      mBtLeService.setCharacteristicNotification(deviceAddress, mCharBlock, true);

      if (Build.VERSION.SDK_INT >= 21) {
        mBtLeService.requestConnectionPriority(
            deviceAddress, BluetoothGatt.CONNECTION_PRIORITY_HIGH);
      }

      setConnectionParameters();
    }
  }
Esempio n. 8
0
 /** Function for writing value to characteristic */
 public void writeCharacteristic(int index, UUID service, UUID characteristic, byte[] value) {
   BluetoothGattService Service;
   BluetoothGattCharacteristic Characteristic;
   if (index < 0 || index > mBluetoothGatt.length) {
     Log.e(TAG, "Read char index out of bound");
     return;
   }
   if (mBluetoothGatt[index] == null) {
     Log.e(TAG, "GATT isn't connected");
     return;
   }
   if (service != null && characteristic != null) {
     Service = mBluetoothGatt[index].getService(service);
     if (Service == null) {
       Log.e(TAG, "Service not found");
       return;
     }
     Characteristic = Service.getCharacteristic(characteristic);
     if (Characteristic == null) {
       Log.e(TAG, "Characteristic not found");
       return;
     }
     Characteristic.setValue(value);
     // Characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
     mBluetoothGatt[index].writeCharacteristic(Characteristic);
   }
 }
Esempio n. 9
0
 public void readCharateristic(int index, UUID service, UUID characteristic) {
   BluetoothGattService Service;
   BluetoothGattCharacteristic Characteristic;
   if (index < 0 || index > mBluetoothGatt.length) {
     Log.e(TAG, "Read char index out of bound");
     return;
   }
   if (mBluetoothGatt[index] == null) {
     Log.e(TAG, "GATT isn't connected");
     return;
   }
   if (service != null && characteristic != null) {
     Service = mBluetoothGatt[index].getService(service);
     if (Service == null) {
       Log.e(TAG, "Service not found");
       return;
     }
     Characteristic = Service.getCharacteristic(characteristic);
     if (Characteristic == null) {
       Log.e(TAG, "Characteristic not found");
       return;
     }
     if (!mBluetoothGatt[index].readCharacteristic(Characteristic)) Log.e(TAG, "Read Failed");
   }
 }
Esempio n. 10
0
 public void enableNotification(int index, UUID service, UUID characteristic) {
   BluetoothGattService Service;
   BluetoothGattCharacteristic Characteristic;
   BluetoothGattDescriptor Descriptor;
   if (index < 0 || index > mBluetoothGatt.length) {
     Log.e(TAG, "Read char index out of bound");
     return;
   }
   if (mBluetoothGatt[index] == null) {
     Log.e(TAG, "GATT isn't connected");
     return;
   }
   if (service != null && characteristic != null) {
     Service = mBluetoothGatt[index].getService(service);
     if (Service == null) {
       Log.e(TAG, "Service not found");
       return;
     }
     Characteristic = Service.getCharacteristic(characteristic);
     if (Characteristic == null) {
       Log.e(TAG, "Characteristic not found");
       return;
     }
     mBluetoothGatt[index].setCharacteristicNotification(Characteristic, true);
     Descriptor = Characteristic.getDescriptor(CCCD);
     Descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     mBluetoothGatt[index].writeDescriptor(Descriptor);
     Log.i(TAG, "CCCD set");
   }
 }
  // Some peripherals re-use UUIDs for multiple characteristics so we need to check the properties
  // and UUID of all characteristics instead of using service.getCharacteristic(characteristicUUID)
  private BluetoothGattCharacteristic findWritableCharacteristic(
      BluetoothGattService service, UUID characteristicUUID, int writeType) {
    try {
      BluetoothGattCharacteristic characteristic = null;

      // get write property
      int writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE;
      if (writeType == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
        writeProperty = BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
      }

      List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
      for (BluetoothGattCharacteristic c : characteristics) {
        if ((c.getProperties() & writeProperty) != 0 && characteristicUUID.equals(c.getUuid())) {
          characteristic = c;
          break;
        }
      }

      // As a last resort, try and find ANY characteristic with this UUID, even if it doesn't have
      // the correct properties
      if (characteristic == null) {
        characteristic = service.getCharacteristic(characteristicUUID);
      }

      return characteristic;
    } catch (Exception e) {
      Log.e(LOG_TAG, "Errore su findWritableCharacteristic", e);
      return null;
    }
  }
 public void getBatteryLevel() {
   BluetoothGattService service = mBluetoothLeService.GetBluetoothGattService(BATTERY_SERVICE);
   if (service != null) {
     BluetoothGattCharacteristic batteryLevel = service.getCharacteristic(KEY_BATTERY_LEVEL);
     mBluetoothLeService.readCharacteristic(batteryLevel);
   }
 }
Esempio n. 13
0
 @Override
 public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
   final BluetoothGattService service = gatt.getService(UART_SERVICE_UUID);
   if (service != null) {
     mTXCharacteristic = service.getCharacteristic(UART_TX_CHARACTERISTIC_UUID);
     mRXCharacteristic = service.getCharacteristic(UART_RX_CHARACTERISTIC_UUID);
   }
   return mTXCharacteristic != null && mRXCharacteristic != null;
 }
  public JSONObject asJSONObject(BluetoothGatt gatt) {

    JSONObject json = asJSONObject();

    try {
      JSONArray servicesArray = new JSONArray();
      JSONArray characteristicsArray = new JSONArray();
      json.put("services", servicesArray);
      json.put("characteristics", characteristicsArray);

      if (connected && gatt != null) {
        for (BluetoothGattService service : gatt.getServices()) {
          servicesArray.put(UUIDHelper.uuidToString(service.getUuid()));

          for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
            JSONObject characteristicsJSON = new JSONObject();
            characteristicsArray.put(characteristicsJSON);

            characteristicsJSON.put("service", UUIDHelper.uuidToString(service.getUuid()));
            characteristicsJSON.put(
                "characteristic", UUIDHelper.uuidToString(characteristic.getUuid()));
            // characteristicsJSON.put("instanceId", characteristic.getInstanceId());

            characteristicsJSON.put("properties", Helper.decodeProperties(characteristic));
            // characteristicsJSON.put("propertiesValue", characteristic.getProperties());

            if (characteristic.getPermissions() > 0) {
              characteristicsJSON.put("permissions", Helper.decodePermissions(characteristic));
              // characteristicsJSON.put("permissionsValue", characteristic.getPermissions());
            }

            JSONArray descriptorsArray = new JSONArray();

            for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
              JSONObject descriptorJSON = new JSONObject();
              descriptorJSON.put("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
              descriptorJSON.put("value", descriptor.getValue()); // always blank

              if (descriptor.getPermissions() > 0) {
                descriptorJSON.put("permissions", Helper.decodePermissions(descriptor));
                // descriptorJSON.put("permissionsValue", descriptor.getPermissions());
              }
              descriptorsArray.put(descriptorJSON);
            }
            if (descriptorsArray.length() > 0) {
              characteristicsJSON.put("descriptors", descriptorsArray);
            }
          }
        }
      }
    } catch (JSONException e) { // TODO better error handling
      e.printStackTrace();
    }

    return json;
  }
 @Override
 public void writeToParcel(Parcel dest, int flags) {
   UUIDUtils.writeToParcel(mService.getUuid(), dest);
   dest.writeInt(mService.getType());
   dest.writeInt(mService.getCharacteristics().size());
   for (BluetoothGattCharacteristic characteristic : mService.getCharacteristics()) {
     BTCharacteristicProfile profile = new BTCharacteristicProfile(characteristic);
     profile.writeToParcel(dest, flags);
   }
 }
 @Override
 public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
   final BluetoothGattService service =
       gatt.getService(CYCLING_SPEED_AND_CADENCE_SERVICE_UUID);
   if (service != null) {
     mCSCMeasurementCharacteristic =
         service.getCharacteristic(CSC_MEASUREMENT_CHARACTERISTIC_UUID);
   }
   return mCSCMeasurementCharacteristic != null;
 }
Esempio n. 17
0
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   BluetoothGattService mOximeterMeasurement = gatt.getService(Service.OXIMETER);
   if (mOximeterMeasurement != null) {
     BluetoothGattCharacteristic characteristic =
         mOximeterMeasurement.getCharacteristic(Characteristic.OXIMETER);
     if (characteristic != null) {
       setCharacteristic(gatt, characteristic, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     }
   }
 }
  private void enableNotifications(BluetoothGatt gatt, UUID serviceUuid, UUID dataUuid) {
    UUID CCC = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

    BluetoothGattService service = gatt.getService(serviceUuid);
    BluetoothGattCharacteristic dataCharacteristic = service.getCharacteristic(dataUuid);
    gatt.setCharacteristicNotification(dataCharacteristic, true); // Enabled locally

    BluetoothGattDescriptor config = dataCharacteristic.getDescriptor(CCC);
    config.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(config); // Enabled remotely
  }
  private void turnOnService(BluetoothGatt gatt, UUID serviceUUID, UUID configUUID) {

    BluetoothGattService humService = gatt.getService(serviceUUID);
    BluetoothGattCharacteristic config = humService.getCharacteristic(configUUID);
    config.setValue(
        new byte[] {
          1
        }); // 01 for Humidity; 01 for +-2g deviation//NB: the config value is different for the
            // Gyroscope
    gatt.writeCharacteristic(config);
  }
 @Override
 public boolean isRequiredServiceSupported(final BluetoothGatt gatt) {
   final BluetoothGattService service = gatt.getService(GLS_SERVICE_UUID);
   if (service != null) {
     mGlucoseMeasurementCharacteristic = service.getCharacteristic(GM_CHARACTERISTIC);
     mGlucoseMeasurementContextCharacteristic =
         service.getCharacteristic(GM_CONTEXT_CHARACTERISTIC);
     mRecordAccessControlPointCharacteristic =
         service.getCharacteristic(RACP_CHARACTERISTIC);
   }
   return mGlucoseMeasurementCharacteristic != null
       && mRecordAccessControlPointCharacteristic != null;
 }
Esempio n. 21
0
  public void showServicesAndCharacteristics() {
    for (BluetoothGattService service : this.io.gatt.getServices()) {
      Log.d(TAG, "onServicesDiscovered:" + service.getUuid());

      for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
        Log.d(TAG, "  char:" + characteristic.getUuid());

        for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
          Log.d(TAG, "    descriptor:" + descriptor.getUuid());
        }
      }
    }
  }
  private void getGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    String uuid = null;
    mModelNumberCharacteristic = null;
    mSerialPortCharacteristic = null;
    mCommandCharacteristic = null;
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
      uuid = gattService.getUuid().toString();
      System.out.println("displayGattServices + uuid=" + uuid);

      List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
      ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

      // Loops through available Characteristics.
      for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
        charas.add(gattCharacteristic);
        uuid = gattCharacteristic.getUuid().toString();
        if (uuid.equals(ModelNumberStringUUID)) {
          mModelNumberCharacteristic = gattCharacteristic;
          System.out.println(
              "mModelNumberCharacteristic  " + mModelNumberCharacteristic.getUuid().toString());
        } else if (uuid.equals(SerialPortUUID)) {
          mSerialPortCharacteristic = gattCharacteristic;
          System.out.println(
              "mSerialPortCharacteristic  " + mSerialPortCharacteristic.getUuid().toString());
          //                    updateConnectionState(R.string.comm_establish);
        } else if (uuid.equals(CommandUUID)) {
          mCommandCharacteristic = gattCharacteristic;
          System.out.println(
              "mSerialPortCharacteristic  " + mSerialPortCharacteristic.getUuid().toString());
          //                    updateConnectionState(R.string.comm_establish);
        }
      }
      mGattCharacteristics.add(charas);
    }

    if (mModelNumberCharacteristic == null
        || mSerialPortCharacteristic == null
        || mCommandCharacteristic == null) {
      Toast.makeText(mainContext, "Please select DFRobot devices", Toast.LENGTH_SHORT).show();
      mConnectionState = connectionStateEnum.isToScan;
      onConectionStateChange(mConnectionState);
    } else {
      mSCharacteristic = mModelNumberCharacteristic;
      mBluetoothLeService.setCharacteristicNotification(mSCharacteristic, true);
      mBluetoothLeService.readCharacteristic(mSCharacteristic);
    }
  }
  public void getCharacteristic(List<BluetoothGattService> gattServices) {
    this.gattServices = gattServices;
    String uuid = null;
    UUID uuuid = null;
    String uuidDescr = "";
    BleNameResolver resolver = new BleNameResolver();
    BluetoothGattCharacteristic characteristic = null;
    BluetoothGattService targetGattService = null;
    List<BluetoothGattCharacteristic> gattCharacteristics = null;
    // get target gattservice
    for (BluetoothGattService gattService : gattServices) {
      uuuid = gattService.getUuid();
      uuid = gattService.getUuid().toString();
      uuidDescr = resolver.resolveServiceName(uuid);

      gattCharacteristics = gattService.getCharacteristics();

      if (uuid.equals(BATTERY_SERVICE.toString())) {
        targetGatBatteryService = gattService;
        BluetoothGattCharacteristic readGattCharacteristic =
            targetGatBatteryService.getCharacteristic(KEY_BATTERY_LEVEL);
        if (readGattCharacteristic != null) {
          mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true);
        }
      }

      if (uuid.equals(targetServiceUuid.toString())) {
        targetGattService = gattService;
        BluetoothGattCharacteristic readGattCharacteristic =
            targetGattService.getCharacteristic(readUUID);
        if (readGattCharacteristic != null) {
          mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true);
        }
        //                break;
      }
    }
    if (targetGattService != null) {
      Toast.makeText(context, "get service ok", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(context, "not support this BLE module", Toast.LENGTH_SHORT).show();
      //            return ;
    }
    gattCharacteristics = targetGattService.getCharacteristics();

    targetGattCharacteristic = targetGattService.getCharacteristic(targetCharacterUuid);
    BluetoothGattCharacteristic readGattCharacteristic =
        targetGattService.getCharacteristic(readUUID);
    if (readGattCharacteristic != null)
      mBluetoothLeService.setCharacteristicNotification(readGattCharacteristic, true);
  }
Esempio n. 24
0
  // Demonstrates how to iterate through the supported GATT Services/Characteristics.
  // In this sample, we populate the data structure that is bound to the ExpandableListView
  // on the UI.
  private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    String unknownCharaString = getResources().getString(R.string.unknown_characteristic);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();
    ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData =
        new ArrayList<ArrayList<HashMap<String, String>>>();
    mGattCharacteristics = new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
      HashMap<String, String> currentServiceData = new HashMap<String, String>();
      currentServiceData.put(
          LIST_NAME, Service.lookup(gattService.getUuid(), unknownServiceString));
      currentServiceData.put(LIST_UUID, uuid);
      gattServiceData.add(currentServiceData);

      ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
          new ArrayList<HashMap<String, String>>();
      List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics();
      ArrayList<BluetoothGattCharacteristic> charas = new ArrayList<BluetoothGattCharacteristic>();

      // Loops through available Characteristics.
      for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
        charas.add(gattCharacteristic);
        HashMap<String, String> currentCharaData = new HashMap<String, String>();
        uuid = gattCharacteristic.getUuid().toString();
        currentCharaData.put(
            LIST_NAME, Characteristic.lookup(gattCharacteristic.getUuid(), unknownCharaString));
        currentCharaData.put(LIST_UUID, uuid);
        gattCharacteristicGroupData.add(currentCharaData);
      }
      mGattCharacteristics.add(charas);
      gattCharacteristicData.add(gattCharacteristicGroupData);
    }

    SimpleExpandableListAdapter gattServiceAdapter =
        new SimpleExpandableListAdapter(
            this,
            gattServiceData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] {android.R.id.text1, android.R.id.text2},
            gattCharacteristicData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] {LIST_NAME, LIST_UUID},
            new int[] {android.R.id.text1, android.R.id.text2});
    mGattServicesList.setAdapter(gattServiceAdapter);
  }
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
          System.out.println("onServicesDiscoverd(): " + gatt + ", status: " + status);

          if (gatt != btGatt) return;

          List<BluetoothGattService> list = btGatt.getServices();
          for (BluetoothGattService s : list) {
            System.err.println(
                "Found service: " + s.getType() + ", " + s.getInstanceId() + ", " + s.getUuid());
            for (BluetoothGattCharacteristic a : s.getCharacteristics()) {
              System.err.println("  char: " + a.getUuid());
            }
            for (BluetoothGattService a : s.getIncludedServices()) {
              System.err.println("  serv: " + a.getUuid());
            }
          }

          System.out.println(" => DummyRead");
          if (status == BluetoothGatt.GATT_SUCCESS) {
            DummyReadForSecLevelCheck(gatt);
            // continue in onCharacteristicRead
          } else {
            DummyReadForSecLevelCheck(gatt);
            // reportConnectFailed("onServicesDiscovered(" + gatt + ", " +
            // status + ")");
          }
        }
 @Before
 public void setUp() throws Exception {
   MockitoAnnotations.initMocks(this);
   mValueCaptor = ArgumentCaptor.forClass(byte[].class);
   when(mService.getCharacteristic(any(UUID.class))).thenReturn(mCharacteristic);
   when(mStore.isEnabled()).thenReturn(true);
 }
 public static boolean isCorrectService(BluetoothGattService service) {
   if ((service.getUuid().toString().compareTo(SensorTagGatt.UUID_HUM_SERV.toString()))
       == 0) { // service.getUuid().toString().compareTo(SensorTagGatt.UUID_HUM_DATA.toString())) {
     Log.d("Test", "Match !");
     return true;
   } else return false;
 }
Esempio n. 28
0
 public synchronized void send(UUID uuid, byte[] value) {
   if (bluetoothGattService != null) {
     BluetoothGattCharacteristic characteristic = bluetoothGattService.getCharacteristic(uuid);
     characteristic.setValue(value);
     bluetoothGatt.writeCharacteristic(characteristic);
   }
 }
Esempio n. 29
0
  /**
   * 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);
  }
 public static boolean isCorrectService(BluetoothGattService service) {
   if ((service.getUuid().toString().compareTo(SensorTagGatt.UUID_IO_SERV.toString())) == 0) {
     Log.d("DeviceActivity", "Found IO !");
     return true;
   } else {
     return false;
   }
 }