private void broadcastUpdate(
     final String action, final BluetoothGattCharacteristic characteristic) {
   final Intent intent = new Intent(action);
   if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
     int flag = characteristic.getProperties();
     int format = -1;
     if ((flag & 0x01) != 0) {
       format = BluetoothGattCharacteristic.FORMAT_UINT16;
     } else {
       format = BluetoothGattCharacteristic.FORMAT_UINT8;
     }
     final int heartRate = characteristic.getIntValue(format, 1);
     LogUtil.info(TAG, "心率数据Received heart rate: %d" + heartRate);
     LogUtil.info(TAG, String.format("Received heart rate: %d", heartRate));
     intent.putExtra(EXTRA_DATA, receiveData);
   } else {
     // 脂肪秤和血糖仪设备数据返回
     final byte[] data = characteristic.getValue();
     if (data != null && data.length > 0) {
       String s = FatScaleDataUtil.byteToHexStringFormat(data);
       LogUtil.info(TAG, "原始数据:" + s);
       if (DEVICE_TYPE == 1) {
         intent.putExtra(EXTRA_DATA, s);
       } else {
         intent.putExtra(EXTRA_DATA, receiveData);
       }
     }
   }
   sendBroadcast(intent);
 }
  public BluetoothGattService getSupportedGattService() {
    if (mBluetoothGatt == null) {
      LogUtil.info(TAG, "mBluetoothGatt is  null ,try reconnect!");
      boolean isconnectok = connect();
      if (!isconnectok) {
        connect();
      }
      return null;
    }
    String UUID_SERVICE;
    switch (DEVICE_TYPE) {
      case 1: // 脂肪秤
        UUID_SERVICE = BLEConstants.UUID_FATSCALE_SERVICE;
        break;
      case 2: // 血压计
        UUID_SERVICE = BLEConstants.UUID_BLOOD_PRESSURE_SERVICE;
        break;
      case 3: // 血糖仪
        UUID_SERVICE = BLEConstants.UUID_BLOODSUGAR_SERVICE;
        break;
      default:
        UUID_SERVICE = BLEConstants.UUID_FATSCALE_SERVICE;
        break;
    }

    LogUtil.info(TAG, "device type=" + DEVICE_TYPE);
    BluetoothGattService service = mBluetoothGatt.getService(UUID.fromString(UUID_SERVICE));
    return service;
  }
 /**
  * After using a given BLE device, the app must call this method to ensure resources are released
  * properly.
  */
 public void close() {
   LogUtil.info(TAG, "mBluetoothGatt is close  before");
   if (mBluetoothGatt == null) {
     return;
   }
   mBluetoothGatt.close();
   mBluetoothGatt = null;
   broadcastUpdate(ACTION_GATT_CLOSE);
   LogUtil.info(TAG, "mBluetoothGatt is close");
 }
  public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {

    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
      LogUtil.info(TAG, "BluetoothAdapter not initialized");
      return;
    }
    boolean isWrite = mBluetoothGatt.writeCharacteristic(characteristic);
    if (isWrite) {
      LogUtil.info(TAG, "写入数据成功");
    } else {
      LogUtil.info(TAG, "写入数据失败");
    }
  }
 /**
  * Disconnects an existing connection or cancel a pending connection. The disconnection result is
  * reported asynchronously through the {@code
  * BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
  * callback.
  */
 public void disconnect() {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     LogUtil.info(TAG, "BluetoothAdapter not initialized");
     return;
   }
   mBluetoothGatt.disconnect();
 }
 @Override
 public boolean onUnbind(Intent intent) {
   LogUtil.info("smarhit", "service中的onUnbind()");
   close();
   isBind = false;
   return super.onUnbind(intent);
 }
 /**
  * Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
  * asynchronously through the {@code
  * BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt,
  * android.bluetooth.BluetoothGattCharacteristic, int)} callback.
  *
  * @param characteristic The characteristic to read from.
  */
 public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     LogUtil.info(TAG, "BluetoothAdapter not initialized");
     return;
   }
   mBluetoothGatt.readCharacteristic(characteristic);
 }
 /**
  * Enables or disables notification on a give characteristic.
  *
  * @param characteristic Characteristic to act on.
  * @param enabled If true, enable notification. False otherwise.
  */
 public void setCharacteristicNotification(
     BluetoothGattCharacteristic characteristic, boolean enabled) {
   if (mBluetoothAdapter == null || mBluetoothGatt == null) {
     LogUtil.info(TAG, "BluetoothAdapter not initialized");
     return;
   }
   LogUtil.info(TAG, "write set notification " + enabled);
   mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
   BluetoothGattDescriptor descriptor =
       characteristic.getDescriptor(
           UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
   if (descriptor != null) {
     LogUtil.info(TAG, "write descriptor");
     descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
     mBluetoothGatt.writeDescriptor(descriptor);
   }
 }
  /**
   * Initializes a reference to the local Bluetooth adapter.
   *
   * @return Return true if the initialization is successful.
   */
  public boolean initialize() {
    if (mBluetoothManager == null) {
      mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
      if (mBluetoothManager == null) {
        LogUtil.info(TAG, "Unable to initialize BluetoothManager.");
        return false;
      }
    }

    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
      LogUtil.info(TAG, "Unable to obtain a BluetoothAdapter.");
      return false;
    }

    return true;
  }
Esempio n. 10
0
 @Override
 public void onServicesDiscovered(BluetoothGatt gatt, int status) {
   LogUtil.info(TAG, "发现蓝牙服务onServicesDiscovered()");
   if (status == BluetoothGatt.GATT_SUCCESS) {
     broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
     // enable notification
     enableNotify();
   }
 }
Esempio n. 11
0
  public boolean reConnect() {
    if (mBluetoothAdapter == null || mDevice == null) {
      LogUtil.info(TAG, "BluetoothAdapter not initialized or unspecified address.");
      return false;
    }
    // if (mBluetoothDeviceAddress != null &&
    // address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
    // LogUtil.info(TAG,
    // "Trying to use an existing mBluetoothGatt for connection.");
    // if (mBluetoothGatt.connect()) {
    // mConnectionState = STATE_CONNECTING;
    // return true;
    // } else {
    // return false;
    // }
    // }

    // We want to directly connect to the device, so we are setting the
    // autoConnect
    // parameter to false.

    mBluetoothGatt = mDevice.connectGatt(BluetoothLeService.this, true, mGattCallback);
    LogUtil.info(TAG, "Trying to reConnect.");
    if (mDevice.getName().equals(BLEConstants.TYPE_FATSCALE_DEVICE_NAME)) {
      DEVICE_TYPE = 1;
    } else if (mDevice.getName().equals(BLEConstants.TYPE_BLOODPRESSURE_DEVICE_NAME)) {
      DEVICE_TYPE = 2;
    } else if (mDevice.getName().equals(BLEConstants.TYPE_BLOODSUGAR_DEVICE_NAME)) {
      DEVICE_TYPE = 3;
    }

    if (mBluetoothGatt != null) {
      if (mBluetoothGatt.connect()) {
        mConnectionState = STATE_CONNECTING;
        LogUtil.info(TAG, "Trying to reConnect. ok.");
        return true;
      } else {
        return false;
      }
    }
    return false;
  }
Esempio n. 12
0
 @Override
 public void onCharacteristicChanged(
     BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
   LogUtil.info(TAG, "接收数据onCharacteristicChanged()");
   String str = "";
   switch (DEVICE_TYPE) {
     case 1: // 脂肪秤
       broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
       if (characteristic.getValue() != null) {
         LogUtil.info(TAG, characteristic.getStringValue(0));
       }
       break;
     case 2: // 血压计
       LogUtil.info(TAG, "数据长度:" + characteristic.getValue().length);
       for (int i = 0; ; ++i) {
         if (i >= characteristic.getValue().length) {
           receiveData = str;
           broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
           LogUtil.info(TAG, "血压计结果数据:" + receiveData);
           return;
         }
         str = str + (0xFF & characteristic.getValue()[i]) + " ";
       }
     case 3: // 血糖仪
       for (int i = 0; ; ++i) {
         if (i >= characteristic.getValue().length) {
           receiveData = str;
           broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
           LogUtil.info(TAG, "血糖仪结果数据:" + receiveData);
           return;
         }
         str = str + (0xFF & characteristic.getValue()[i]) + " ";
       }
     default:
       break;
   }
 }
Esempio n. 13
0
 @Override
 public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
   String intentAction;
   //			isStateChange = true;
   LogUtil.info(TAG, "蓝牙连接状态发生变化!onConnectionStateChange()");
   if (newState == BluetoothProfile.STATE_CONNECTED) {
     intentAction = ACTION_GATT_CONNECTED;
     mConnectionState = STATE_CONNECTED;
     mBluetoothGatt.discoverServices();
     broadcastUpdate(intentAction);
   } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
     intentAction = ACTION_GATT_DISCONNECTED;
     mConnectionState = STATE_DISCONNECTED;
     mBluetoothGatt.close();
     broadcastUpdate(intentAction);
   }
 }
Esempio n. 14
0
 @Override
 public void onCharacteristicRead(
     BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   LogUtil.info(TAG, "读取数据onCharacteristicRead()");
 }
Esempio n. 15
0
  private void enableNotify() {
    BluetoothGattService service = getSupportedGattService();
    if (null == service) {
      LogUtil.info(TAG, "BluetoothGattService service is null");
      boolean isconnectok = connect();
      if (!isconnectok) {
        connect();
      }
      return;
    }
    BluetoothGattCharacteristic gattCharacteristic = null;
    switch (DEVICE_TYPE) {
      case 1:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_CHARACTERISTIC));
        break;
      case 2:
        gattCharacteristic =
            service.getCharacteristic(
                UUID.fromString(BLEConstants.UUID_BLOOD_PRESSURE_CHARACTERISTIC));
        break;
      case 3:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_BLOODSUGAR_CHARACTERISTIC));
        break;
      default:
        gattCharacteristic =
            service.getCharacteristic(UUID.fromString(BLEConstants.UUID_CHARACTERISTIC));
        break;
    }
    if (null == gattCharacteristic) {
      LogUtil.info(TAG, "BluetoothGattCharacteristic gattCharacteristic is null");
      return;
    }

    boolean bool = mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true);
    LogUtil.info(TAG, "setCharacteristicNotification:" + bool);
    BluetoothGattDescriptor descriptor = null;
    switch (DEVICE_TYPE) {
      case 1:
        descriptor =
            gattCharacteristic.getDescriptor(UUID.fromString(BLEConstants.UUID_DESCRIPTER));
      case 2:
        descriptor =
            gattCharacteristic.getDescriptor(
                UUID.fromString(BLEConstants.UUID_BLOOD_PRESSURE_DESCRIPTER));
        break;
      case 3:
        descriptor =
            gattCharacteristic.getDescriptor(
                UUID.fromString(BLEConstants.UUID_BLOODSUGAR_DESCRIPTER));
        break;

      default:
        descriptor =
            gattCharacteristic.getDescriptor(UUID.fromString(BLEConstants.UUID_DESCRIPTER));
        break;
    }
    if (null != descriptor) {
      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
      mBluetoothGatt.writeDescriptor(descriptor);
    }
  }
Esempio n. 16
0
 @Override
 public void onDescriptorWrite(
     BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
   LogUtil.info(TAG, "写入数据onDescriptorWrite()");
 }
Esempio n. 17
0
 @Override
 public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
   LogUtil.info(TAG, "执行了onReadRemoteRssi()! rssi = " + rssi);
 }
Esempio n. 18
0
 @Override
 public void onCharacteristicWrite(
     BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
   LogUtil.info(TAG, "-----------onCharacteristicWrite---------status:" + status);
 };
Esempio n. 19
0
 @Override
 public IBinder onBind(Intent intent) {
   LogUtil.info("smarhit", "service中的onBind()");
   isBind = true;
   return mBinder;
 }
Esempio n. 20
0
 public void scanLeDevice(String collectedDevice) {
   this.mCollectedDevice = collectedDevice;
   LogUtil.info("smarhit", collectedDevice + "数据已收集,开始扫描其他设备");
   mScanTime = System.currentTimeMillis();
   mBluetoothAdapter.startLeScan(mLeScanCallback);
 }