package tmay.bluetoothbacon.blescanner.services;
import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import org.androidannotations.annotations.EService;
import org.androidannotations.annotations.ServiceAction;
import java.util.List;
/**
* Created by Terry on 10/6/14.
*/
public class BleService extends Service {
public static final String TAG = "strip_control_service";
public final static String SERVICE_READY = "SERVICE_READY";
public final static String ACTION_GATT_CONNECTED = "ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED = "ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED = "ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_GATT_RSSI = "ACTION_GATT_RSSI";
public final static String ACTION_DATA_AVAILABLE = "ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA = "EXTRA_DATA";
private boolean isConnected;
private BluetoothGatt gatt;
public class LocalBinder extends Binder {
public BleService getService() {
return BleService.this;
}
}
private final IBinder binder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:");
isConnected = true;
BleService.this.gatt = gatt;
sendBroadcast(new Intent(ACTION_GATT_CONNECTED));
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Disco from GATT server.");
isConnected = false;
gatt.close();
gatt = null;
sendBroadcast(new Intent(ACTION_GATT_DISCONNECTED));
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
sendBroadcast(new Intent(ACTION_GATT_SERVICES_DISCOVERED));
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
sendBroadcast(new Intent(ACTION_GATT_RSSI));
}
};
public void discoverGattServices() {
gatt.discoverServices();
}
public List<BluetoothGattService> getGattServiceList() {
return gatt.getServices();
}
public boolean updateRSSI() {
return gatt.readRemoteRssi();
}
public void connectGatt(BluetoothDevice device) {
device.connectGatt(this, true, gattCallback);
}
public void disconnectGatt() {
if (gatt != null)
gatt.disconnect();
}
}