private static void writeRegister(APIRegister register, params byte[] data)
 {
     queueCommand(Registers.buildWriteCommand(register, data));
 }
Example #2
0
 /**
  * Builds an array of bytes corresponding to writing a register value.  The output of
  * this function needs to be written to the Command characteristic to set the value
  * of a register.
  * @param register Register to write to
  * @param parameters Additional parameters for the write command e.g. LED color or
  * a disable/enable bit
  * @return Array of bytes to write a register value
  * @see com.mbientlab.metawear.api.characteristic.MetaWear#COMMAND
  */
 public static byte[] buildWriteCommand(APIRegister register, params byte[] parameters)
 {
     return(buildCommand(register.module(), register.opcode(), parameters));
 }
        /** MetaWear specific Bluetooth GATT callback
         * private final BluetoothGattCallback metaWearGattCallback= new BluetoothGattCallback()
         * {
         *  private ArrayDeque<BluetoothGattCharacteristic> shouldNotify= new ArrayDeque<>();
         *
         *  @Override
         *  public void onConnectionStateChange(BluetoothGatt gatt, int status,
         *          int newState) {
         *      Intent intent= new Intent();
         *      boolean broadcast= true;
         *
         *      switch (newState) {
         *      case BluetoothProfile.STATE_CONNECTED:
         *          gatt.discoverServices();
         *          intent.setAction(Action.DEVICE_CONNECTED);
         *          connected= true;
         *          break;
         *      case BluetoothProfile.STATE_DISCONNECTED:
         *          intent.setAction(Action.DEVICE_DISCONNECTED);
         *          connected= false;
         *          break;
         *      default:
         *          broadcast= false;
         *          break;
         *      }
         *      if (broadcast) sendBroadcast(intent);
         *  }
         *
         *  @Override
         *  public void onServicesDiscovered(BluetoothGatt gatt, int status) {
         *      for(BluetoothGattService service: gatt.getServices()) {
         *          for(BluetoothGattCharacteristic characteristic: service.getCharacteristics()) {
         *              int charProps = characteristic.getProperties();
         *              if ((charProps & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) {
         *                  shouldNotify.add(characteristic);
         *              }
         *          }
         *      }
         *      deviceState= DeviceState.ENABLING_NOTIFICATIONS;
         *      setupNotification();
         *  }
         *
         *  private void setupNotification() {
         *      metaWearGatt.setCharacteristicNotification(shouldNotify.peek(), true);
         *      BluetoothGattDescriptor descriptor= shouldNotify.poll().getDescriptor(CHARACTERISTIC_CONFIG);
         *      descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
         *      metaWearGatt.writeDescriptor(descriptor);
         *  }
         *
         *  @Override
         *  public void onDescriptorWrite(BluetoothGatt gatt,
         *          BluetoothGattDescriptor descriptor, int status) {
         *      if (!shouldNotify.isEmpty()) setupNotification();
         *      else deviceState= DeviceState.READY;
         *
         *      if (deviceState == DeviceState.READY) {
         *          if (!commandBytes.isEmpty()) {
         *              deviceState= DeviceState.WRITING_CHARACTERISTICS;
         *              writeCommand();
         *          } else if (!readCharUuids.isEmpty()) {
         *              deviceState= DeviceState.READING_CHARACTERISTICS;
         *              readCharacteristic();
         *          }
         *      }
         *  }
         *
         *  @Override
         *  public void onCharacteristicRead(BluetoothGatt gatt,
         *          BluetoothGattCharacteristic characteristic, int status) {
         *      Intent intent= new Intent(Action.CHARACTERISTIC_READ);
         *      intent.putExtra(Extra.SERVICE_UUID, characteristic.getService().getUuid());
         *      intent.putExtra(Extra.CHARACTERISTIC_UUID, characteristic.getUuid());
         *      intent.putExtra(Extra.CHARACTERISTIC_VALUE, characteristic.getValue());
         *      sendBroadcast(intent);
         *
         *      if (!readCharUuids.isEmpty()) readCharacteristic();
         *      else deviceState= DeviceState.READY;
         *
         *      if (deviceState == DeviceState.READY) {
         *          if (!commandBytes.isEmpty()) {
         *              deviceState= DeviceState.WRITING_CHARACTERISTICS;
         *              writeCommand();
         *          } else if (!readCharUuids.isEmpty()) {
         *              deviceState= DeviceState.READING_CHARACTERISTICS;
         *              readCharacteristic();
         *          }
         *      }
         *  }
         *
         *  public void onCharacteristicWrite(BluetoothGatt gatt,
         *          BluetoothGattCharacteristic characteristic, int status)
         *  {
         *      if (!commandBytes.isEmpty()) writeCommand();
         *      else deviceState= DeviceState.READY;
         *      if (deviceState == DeviceState.READY && !readCharUuids.isEmpty()) {
         *          deviceState= DeviceState.READING_CHARACTERISTICS;
         *          readCharacteristic();
         *      }
         *  }
         *
         *  public void onCharacteristicChanged(BluetoothGatt gatt,
         *          BluetoothGattCharacteristic characteristic)
         *  {
         *      Intent intent= new Intent(Action.NOTIFICATION_RECEIVED);
         *      intent.putExtra(Extra.CHARACTERISTIC_VALUE, characteristic.getValue());
         *      sendBroadcast(intent);
         *  }
         * }
         */

        /**
         * Connect to the GATT service on the MetaWear device
         * @param metaWearBoard MetaWear device to connect to
         */
        /*
         * public void connect(BluetoothDevice metaWearBoard)
         * {
         *  if (!metaWearBoard.equals(this.metaWearBoard))
         *  {
         *      commandBytes.clear();
         *      readCharUuids.clear();
         *  }
         *
         *  deviceState= DeviceState.UNKNOWN;
         *  close();
         *
         *  this.metaWearBoard= metaWearBoard;
         *  metaWearGatt= metaWearBoard.connectGatt(this, false, metaWearGattCallback);
         * }
         *
         * public void reconnect()
         * {
         *  if (metaWearBoard != null)
         *  {
         *      deviceState= DeviceState.UNKNOWN;
         *      close();
         *      metaWearGatt= metaWearBoard.connectGatt(this, false, metaWearGattCallback);
         *  }
         * }
         *
         * public void disconnect()
         * {
         *  if (metaWearGatt != null)
         *  {
         *      metaWearGatt.disconnect();
         *  }
         * }
         */

        /** Close the GATT service and free up resources */

        /*
         * public void close(Boolean notify)
         * {
         *  if (metaWearGatt != null)
         *  {
         *      metaWearGatt.close();
         *      metaWearGatt= null;
         *      connected= false;
         *      if (notify)
         *      {
         *          for(DeviceCallbacks it: deviceCallbacks)
         *          {
         *              it.disconnected();
         *          }
         *      }
         *  }
         * }
         */

        /** Close the GATT service and free up resources */

        /*
         * public void close()
         * {
         *  close(false);
         * }
         */

        /*
         * Binding between the Intent and this service
         * private Binder serviceBinder= new LocalBinder();
         *
         * Dummy class for getting the MetaWear BLE service from its binder
         * public class LocalBinder extends Binder {
         *  /**
         * Get the MetaWearBLEService object
         * @return MetaWearBLEService object
         *
         *   public MetaWearBleService getService() {
         *      return MetaWearBleService.this;
         *  }
         * }
         *
         * @Override
         * public IBinder onBind(Intent intent) {
         *  // TODO Auto-generated method stub
         *  return serviceBinder;
         * }
         *
         * @Override
         * public boolean onUnbind(Intent intent)
         * {
         *  close();
         *  return super.onUnbind(intent);
         * }
         */

        // Reading a register really means writing a command to the command buffer and then receiving a response
        // as a characteristic output of the device.
        private static void readRegister(APIRegister register, params byte[] parameters)
        {
            queueCommand(Registers.buildReadCommand(register, parameters));
        }
Example #4
0
 /**
  * Builds an array of bytes corresponding to reading a register value.  The output
  * of this function needs to be written to the Command characteristic to retrieve
  * the value of a register.
  * @param register Register to read from
  * @param parameters Additional parameters for the read command e.g. a pixel index
  * or a io pin number
  * @return Array of bytes to read a register value
  * @see com.mbientlab.metawear.api.characteristic.MetaWear#COMMAND
  */
 public static byte[] buildReadCommand(APIRegister register, params byte[] parameters)
 {
     return(buildCommand(register.module(), (byte)(0x80 | register.opcode()), parameters));
 }