|
| 1 | +package com.patloew.rxfit; |
| 2 | + |
| 3 | +import android.support.annotation.NonNull; |
| 4 | +import android.support.annotation.RequiresPermission; |
| 5 | + |
| 6 | +import com.google.android.gms.common.api.Status; |
| 7 | +import com.google.android.gms.fitness.data.BleDevice; |
| 8 | +import com.google.android.gms.fitness.data.DataType; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import rx.Observable; |
| 14 | +import rx.Single; |
| 15 | +import rx.functions.Func1; |
| 16 | + |
| 17 | +/* Copyright 2016 Patrick Löwenstein |
| 18 | + * |
| 19 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | + * you may not use this file except in compliance with the License. |
| 21 | + * You may obtain a copy of the License at |
| 22 | + * |
| 23 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | + * |
| 25 | + * Unless required by applicable law or agreed to in writing, software |
| 26 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | + * See the License for the specific language governing permissions and |
| 29 | + * limitations under the License. */ |
| 30 | +public class Ble { |
| 31 | + |
| 32 | + private final RxFit rxFit; |
| 33 | + |
| 34 | + Ble(RxFit rxFit) { |
| 35 | + this.rxFit = rxFit; |
| 36 | + } |
| 37 | + |
| 38 | + // claimDevice |
| 39 | + |
| 40 | + public Single<Status> claimDevice(@NonNull BleDevice bleDevice) { |
| 41 | + return claimDeviceInternal(bleDevice, null, null, null); |
| 42 | + } |
| 43 | + |
| 44 | + public Single<Status> claimDevice(@NonNull BleDevice bleDevice, long timeout, @NonNull TimeUnit timeUnit) { |
| 45 | + return claimDeviceInternal(bleDevice, null, timeout, timeUnit); |
| 46 | + } |
| 47 | + |
| 48 | + public Single<Status> claimDevice(@NonNull String deviceAddress) { |
| 49 | + return claimDeviceInternal(null, deviceAddress, null, null); |
| 50 | + } |
| 51 | + |
| 52 | + public Single<Status> claimDevice(@NonNull String deviceAddress, long timeout, @NonNull TimeUnit timeUnit) { |
| 53 | + return claimDeviceInternal(null, deviceAddress, timeout, timeUnit); |
| 54 | + } |
| 55 | + |
| 56 | + private Single<Status> claimDeviceInternal(BleDevice bleDevice, String deviceAddress, Long timeout, TimeUnit timeUnit) { |
| 57 | + return Single.create(new BleClaimDeviceSingle(rxFit, bleDevice, deviceAddress, timeout, timeUnit)); |
| 58 | + } |
| 59 | + |
| 60 | + // getClaimedDevices |
| 61 | + |
| 62 | + public Observable<BleDevice> getClaimedDevices() { |
| 63 | + return getClaimedDeviceListInternal(null, null, null); |
| 64 | + } |
| 65 | + |
| 66 | + public Observable<BleDevice> getClaimedDevices(long timeout, @NonNull TimeUnit timeUnit) { |
| 67 | + return getClaimedDeviceListInternal(null, timeout, timeUnit); |
| 68 | + } |
| 69 | + |
| 70 | + public Observable<BleDevice> getClaimedDevices(DataType dataType) { |
| 71 | + return getClaimedDeviceListInternal(dataType, null, null); |
| 72 | + } |
| 73 | + |
| 74 | + private Observable<BleDevice> getClaimedDevicesInternal(DataType dataType, long timeout, @NonNull TimeUnit timeUnit) { |
| 75 | + return getClaimedDeviceListInternal(dataType, timeout, timeUnit); |
| 76 | + } |
| 77 | + |
| 78 | + private Observable<BleDevice> getClaimedDeviceListInternal(DataType dataType, Long timeout, TimeUnit timeUnit) { |
| 79 | + return Single.create(new BleListClaimedDevicesSingle(rxFit, dataType, timeout, timeUnit)) |
| 80 | + .flatMapObservable(new Func1<List<BleDevice>, Observable<BleDevice>>() { |
| 81 | + @Override |
| 82 | + public Observable<BleDevice> call(List<BleDevice> bleDevices) { |
| 83 | + return Observable.from(bleDevices); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + // scan |
| 89 | + |
| 90 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 91 | + public Observable<BleDevice> scan() { |
| 92 | + return scanInternal(null, null, null, null); |
| 93 | + } |
| 94 | + |
| 95 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 96 | + public Observable<BleDevice> scan(long timeout, @NonNull TimeUnit timeUnit) { |
| 97 | + return scanInternal(null, null, timeout, timeUnit); |
| 98 | + } |
| 99 | + |
| 100 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 101 | + public Observable<BleDevice> scan(@NonNull DataType... dataTypes) { |
| 102 | + return scanInternal(dataTypes, null, null, null); |
| 103 | + } |
| 104 | + |
| 105 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 106 | + public Observable<BleDevice> scan(@NonNull DataType[] dataTypes, long timeout, @NonNull TimeUnit timeUnit) { |
| 107 | + return scanInternal(dataTypes, null, timeout, timeUnit); |
| 108 | + } |
| 109 | + |
| 110 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 111 | + public Observable<BleDevice> scan(int stopTimeSecs) { |
| 112 | + return scanInternal(null, stopTimeSecs, null, null); |
| 113 | + } |
| 114 | + |
| 115 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 116 | + public Observable<BleDevice> scan(int stopTimeSecs, long timeout, @NonNull TimeUnit timeUnit) { |
| 117 | + return scanInternal(null, stopTimeSecs, timeout, timeUnit); |
| 118 | + } |
| 119 | + |
| 120 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 121 | + public Observable<BleDevice> scan(@NonNull DataType[] dataTypes, int stopTimeSecs) { |
| 122 | + return scanInternal(dataTypes, stopTimeSecs, null, null); |
| 123 | + } |
| 124 | + |
| 125 | + @RequiresPermission("android.permission.BLUETOOTH_ADMIN") |
| 126 | + public Observable<BleDevice> scan(@NonNull DataType[] dataTypes, int stopTimeSecs, long timeout, @NonNull TimeUnit timeUnit) { |
| 127 | + return scanInternal(dataTypes, stopTimeSecs, timeout, timeUnit); |
| 128 | + } |
| 129 | + |
| 130 | + @SuppressWarnings("MissingPermission") |
| 131 | + private Observable<BleDevice> scanInternal(DataType[] dataTypes, Integer stopTimeSecs, Long timeout, TimeUnit timeUnit) { |
| 132 | + return Observable.create(new BleScanObservable(rxFit, dataTypes, stopTimeSecs, timeout, timeUnit)); |
| 133 | + } |
| 134 | + |
| 135 | + // unclaim Device |
| 136 | + |
| 137 | + public Single<Status> unclaimDevice(@NonNull BleDevice bleDevice) { |
| 138 | + return unclaimDeviceInternal(bleDevice, null, null, null); |
| 139 | + } |
| 140 | + |
| 141 | + public Single<Status> unclaimDevice(@NonNull BleDevice bleDevice, long timeout, @NonNull TimeUnit timeUnit) { |
| 142 | + return unclaimDeviceInternal(bleDevice, null, timeout, timeUnit); |
| 143 | + } |
| 144 | + |
| 145 | + public Single<Status> unclaimDevice(@NonNull String deviceAddress) { |
| 146 | + return unclaimDeviceInternal(null, deviceAddress, null, null); |
| 147 | + } |
| 148 | + |
| 149 | + public Single<Status> unclaimDevice(@NonNull String deviceAddress, long timeout, @NonNull TimeUnit timeUnit) { |
| 150 | + return unclaimDeviceInternal(null, deviceAddress, timeout, timeUnit); |
| 151 | + } |
| 152 | + |
| 153 | + private Single<Status> unclaimDeviceInternal(BleDevice bleDevice, String deviceAddress, Long timeout, TimeUnit timeUnit) { |
| 154 | + return Single.create(new BleUnclaimDeviceSingle(rxFit, bleDevice, deviceAddress, timeout, timeUnit)); |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments