2017-03-10 14:53:19 +01:00
/ * Copyright ( C ) 2015 - 2017 Carsten Pfeiffer
This file is part of Gadgetbridge .
Gadgetbridge is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
Gadgetbridge is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>. */
2015-10-22 00:53:27 +02:00
package nodomain.freeyourgadget.gadgetbridge.service.devices.miband ;
import android.bluetooth.BluetoothGattCharacteristic ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandService ;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.VibrationProfile ;
2016-07-25 00:00:22 +02:00
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport ;
2015-10-22 00:53:27 +02:00
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEAction ;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder ;
2017-03-02 00:27:54 +01:00
import nodomain.freeyourgadget.gadgetbridge.service.devices.common.SimpleNotification ;
2015-10-22 00:53:27 +02:00
public class V1NotificationStrategy implements NotificationStrategy {
private static final Logger LOG = LoggerFactory . getLogger ( V1NotificationStrategy . class ) ;
static final byte [ ] startVibrate = new byte [ ] { MiBandService . COMMAND_SEND_NOTIFICATION , 1 } ;
static final byte [ ] stopVibrate = new byte [ ] { MiBandService . COMMAND_STOP_MOTOR_VIBRATE } ;
2016-07-25 00:00:22 +02:00
private final AbstractBTLEDeviceSupport support ;
2015-10-22 00:53:27 +02:00
2016-07-25 00:00:22 +02:00
public V1NotificationStrategy ( AbstractBTLEDeviceSupport support ) {
2015-10-22 00:53:27 +02:00
this . support = support ;
}
@Override
2017-03-02 00:27:54 +01:00
public void sendDefaultNotification ( TransactionBuilder builder , SimpleNotification simpleNotification , BtLEAction extraAction ) {
2015-10-22 00:53:27 +02:00
BluetoothGattCharacteristic characteristic = support . getCharacteristic ( MiBandService . UUID_CHARACTERISTIC_CONTROL_POINT ) ;
builder . write ( characteristic , getDefaultNotification ( ) ) ;
builder . add ( extraAction ) ;
}
private byte [ ] getDefaultNotification ( ) {
final int vibrateTimes = 1 ;
final long vibrateDuration = 250l ;
final int flashTimes = 1 ;
final int flashColour = 0xFFFFFFFF ;
final int originalColour = 0xFFFFFFFF ;
final long flashDuration = 250l ;
return getNotification ( vibrateDuration , vibrateTimes , flashTimes , flashColour , originalColour , flashDuration ) ;
}
private byte [ ] getNotification ( long vibrateDuration , int vibrateTimes , int flashTimes , int flashColour , int originalColour , long flashDuration ) {
byte [ ] vibrate = startVibrate ;
byte r = 6 ;
byte g = 0 ;
byte b = 6 ;
boolean display = true ;
// byte[] flashColor = new byte[]{ 14, r, g, b, display ? (byte) 1 : (byte) 0 };
return vibrate ;
}
/ * *
* Adds a custom notification to the given transaction builder
* @param vibrationProfile specifies how and how often the Band shall vibrate .
2017-03-02 00:27:54 +01:00
* @param simpleNotification
2015-10-22 00:53:27 +02:00
* @param flashTimes
* @param flashColour
* @param originalColour
* @param flashDuration
* @param extraAction an extra action to be executed after every vibration and flash sequence . Allows to abort the repetition , for example .
* @param builder
* /
2016-10-08 22:43:41 +02:00
@Override
2017-03-02 00:27:54 +01:00
public void sendCustomNotification ( VibrationProfile vibrationProfile , SimpleNotification simpleNotification , int flashTimes , int flashColour , int originalColour , long flashDuration , BtLEAction extraAction , TransactionBuilder builder ) {
2015-10-22 00:53:27 +02:00
BluetoothGattCharacteristic controlPoint = support . getCharacteristic ( MiBandService . UUID_CHARACTERISTIC_CONTROL_POINT ) ;
for ( short i = 0 ; i < vibrationProfile . getRepeat ( ) ; i + + ) {
int [ ] onOffSequence = vibrationProfile . getOnOffSequence ( ) ;
for ( int j = 0 ; j < onOffSequence . length ; j + + ) {
int on = onOffSequence [ j ] ;
on = Math . min ( 500 , on ) ; // longer than 500ms is not possible
builder . write ( controlPoint , startVibrate ) ;
builder . wait ( on ) ;
builder . write ( controlPoint , stopVibrate ) ;
if ( + + j < onOffSequence . length ) {
int off = Math . max ( onOffSequence [ j ] , 25 ) ; // wait at least 25ms
builder . wait ( off ) ;
}
if ( extraAction ! = null ) {
builder . add ( extraAction ) ;
}
}
}
}
2017-03-14 00:44:59 +01:00
@Override
public void stopCurrentNotification ( TransactionBuilder builder ) {
BluetoothGattCharacteristic controlPoint = support . getCharacteristic ( MiBandService . UUID_CHARACTERISTIC_CONTROL_POINT ) ;
builder . write ( controlPoint , stopVibrate ) ;
}
2015-10-22 00:53:27 +02:00
// private void sendCustomNotification(int vibrateDuration, int vibrateTimes, int pause, int flashTimes, int flashColour, int originalColour, long flashDuration, TransactionBuilder builder) {
// BluetoothGattCharacteristic controlPoint = getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT);
// int vDuration = Math.min(500, vibrateDuration); // longer than 500ms is not possible
// for (int i = 0; i < vibrateTimes; i++) {
// builder.write(controlPoint, startVibrate);
// builder.wait(vDuration);
// builder.write(controlPoint, stopVibrate);
// if (pause > 0) {
// builder.wait(pause);
// }
// }
//
// LOG.info("Sending notification to MiBand: " + controlPoint);
// builder.queue(getQueue());
// }
}