Android Open Source - BleAdapter Gatt Service






From Project

Back to project page BleAdapter.

License

The source code is released under:

Apache License

If you think the Android project BleAdapter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.indy.labs.android.BleEventAdapter.service.gatt;
//from  w w w  .  j av  a  2s . c  om
import android.app.Service;
import android.bluetooth.*;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.indy.labs.android.BleEventAdapter.BleEventAdapter;
import com.indy.labs.android.BleEventAdapter.BleEventBus;
import com.indy.labs.android.BleEventAdapter.events.*;

public class GattService extends Service {


    private BleEventBus mBleEventBus;
    private BluetoothGatt mBluetoothGatt;
    private String TAG = this.getClass().getSimpleName();
    private BluetoothDevice mDevice;

    @Override
    public IBinder onBind(Intent intent) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        mBleEventBus = BleEventBus.getInstance();
        mDevice = BleEventAdapter.getInstance().getBluetoothDevice();
        if (mBluetoothGatt == null || mBluetoothGatt.connect())
            mBluetoothGatt = mDevice.connectGatt(this, false, mGattCallBack);


        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.

        if (mBluetoothGatt != null) {
            mBluetoothGatt.close();
            mBluetoothGatt = null;
        }


    }

    private BluetoothGattCallback mGattCallBack = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            mBleEventBus.post(new GattConnectionStateChangedEvent(mBluetoothGatt, status, newState));

            if (newState == GattConnectionState.STATE_CONNECTED) {
                mBluetoothGatt.discoverServices();
                mBleEventBus.post(new DiscoveryServiceEvent(1));
            } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
                mBluetoothGatt = mDevice.connectGatt(GattService.this, false, mGattCallBack);
            }


        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            mBleEventBus.post(new ServiceDiscoveredEvent(mBluetoothGatt, status));
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            mBleEventBus.post(new CharacteristicReadEvent(mBluetoothGatt, characteristic, status));
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            mBleEventBus.post(new CharacteristicWriteEvent(mBluetoothGatt, characteristic, status));
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            mBleEventBus.post(new CharacteristicChangedEvent(mBluetoothGatt, characteristic));
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
            mBleEventBus.post(new DescriptorReadEvent(mBluetoothGatt, descriptor, status));
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
            mBleEventBus.post(new DescriptorWriteEvent(mBluetoothGatt, descriptor, status));
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
            mBleEventBus.post(new ReliableWriteCompleted(mBluetoothGatt, status));

        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
            mBleEventBus.post(new ReadRemoteRssiEvent(mBluetoothGatt, rssi, status));
        }
    };


}




Java Source Code List

com.indy.labs.android.BleEventAdapter.BleEventAdapter.java
com.indy.labs.android.BleEventAdapter.BleEventBus.java
com.indy.labs.android.BleEventAdapter.EventLogger.java
com.indy.labs.android.BleEventAdapter.events.CharacteristicChangedEvent.java
com.indy.labs.android.BleEventAdapter.events.CharacteristicReadEvent.java
com.indy.labs.android.BleEventAdapter.events.CharacteristicWriteEvent.java
com.indy.labs.android.BleEventAdapter.events.DescriptorReadEvent.java
com.indy.labs.android.BleEventAdapter.events.DescriptorWriteEvent.java
com.indy.labs.android.BleEventAdapter.events.DiscoveredDevicesEvent.java
com.indy.labs.android.BleEventAdapter.events.DiscoveryServiceEvent.java
com.indy.labs.android.BleEventAdapter.events.GattConnectionStateChangedEvent.java
com.indy.labs.android.BleEventAdapter.events.ReadRemoteRssiEvent.java
com.indy.labs.android.BleEventAdapter.events.ReliableWriteCompleted.java
com.indy.labs.android.BleEventAdapter.events.ScanningEvent.java
com.indy.labs.android.BleEventAdapter.events.ServiceDiscoveredEvent.java
com.indy.labs.android.BleEventAdapter.service.discovery.device.DeviceDiscoveryService.java
com.indy.labs.android.BleEventAdapter.service.gatt.GattConnectionState.java
com.indy.labs.android.BleEventAdapter.service.gatt.GattService.java