Android Open Source - LeBlue Le Remote Device43






From Project

Back to project page LeBlue.

License

The source code is released under:

Apache License

If you think the Android project LeBlue 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 houtbecke.rs.le.fourdotthree;
// w  w w . j a  va  2  s .  co  m
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.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Set;
import java.util.UUID;

import houtbecke.rs.le.LeCharacteristicListener;
import houtbecke.rs.le.LeGattService;
import houtbecke.rs.le.LeRemoteDevice;
import houtbecke.rs.le.LeRemoteDeviceListener;
import houtbecke.rs.le.LeUtil;

public class LeRemoteDevice43 extends BluetoothGattCallback implements LeRemoteDevice {

    final LeDevice43 leDevice43;
    final BluetoothDevice remoteDevice43;

    final Map<UUID, LeCharacteristicListener> uuidCharacteristicListeners = new HashMap<UUID, LeCharacteristicListener>(0);
    final ConcurrentLinkedQueue<BluetoothGattDescriptor> descriptorWriteQueue = new ConcurrentLinkedQueue<BluetoothGattDescriptor>();


    public LeRemoteDevice43(LeDevice43 leDevice43, BluetoothDevice device)  {
        this.leDevice43 = leDevice43;
        this.remoteDevice43 = device;
    }

    Set<LeRemoteDeviceListener> listeners = new LinkedHashSet<>();
    @Override
    public void addListener(LeRemoteDeviceListener listener) {
        if (listeners.contains(listener))
            listeners.remove(listener);

        listeners.add(listener);
    }
    @Override
    public void removeListener(LeRemoteDeviceListener listener) {
        listeners.remove(listener);
    }

    @Override
    public String getAddress() {
        return remoteDevice43.getAddress();
    }

    @Override
    public void connect() {
        if (gatt != null) {
            gatt.connect();
            return;
        }

        remoteDevice43.connectGatt(leDevice43.context, false, this);

    }

    @Override
    public void disconnect() {
        if (gatt != null) {
            gatt.disconnect();
            close();
            for (LeRemoteDeviceListener listener: listeners)
                listener.leDevicesDisconnected(leDevice43, this);
        }
    }

    @Override
    public void close() {
        if (gatt != null) {
            gatt.close();
            gatt = null;
            for (LeRemoteDeviceListener listener: listeners)
                listener.leDevicesClosed(leDevice43, this);

        }
    }

    @Override
    public void startServicesDiscovery() {
        if (gatt !=null)
            gatt.discoverServices();
    }

    @Override
    public void setCharacteristicListener(LeCharacteristicListener listener, UUID... uuids) {

        if (uuids == null || uuids.length == 0)
            uuidCharacteristicListeners.put(null, listener);
        else for (UUID uuid: uuids)
            uuidCharacteristicListeners.put(uuid, listener);
    }

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);

        try {

            if (newState == BluetoothProfile.STATE_CONNECTED) {
                this.gatt = gatt;
                for (LeRemoteDeviceListener listener : listeners)
                    listener.leDevicesConnected(leDevice43, this);
            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                close();
                for (LeRemoteDeviceListener listener : listeners)
                    listener.leDevicesDisconnected(leDevice43, this);
            }
        } catch (Throwable t) {
            Log.w("LeBlue", "error during onConnectionStateChange callback", t);
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);

        try {

            List<BluetoothGattService> services43 = gatt.getServices();
            LeGattService[] services = new LeGattService[services43.size()];
            for (int i = 0; i < services43.size(); i++)
                services[i] = new LeGattService43(leDevice43, this, services43.get(i).getUuid());


            LeRemoteDeviceListener[] arrayListeners = new LeRemoteDeviceListener[listeners.size()];
            arrayListeners = listeners.toArray(arrayListeners);
            for (LeRemoteDeviceListener listener : arrayListeners)
                listener.serviceDiscovered(leDevice43, this, leDevice43.toGattStatus(status), services);
        } catch (Throwable t) {
            Log.w("LeBlue", "error during onServicesDiscovered callback", t);
        }

    }

    public void writeGattDescriptor(BluetoothGattDescriptor d){
        //put the descriptor into the write queue
        descriptorWriteQueue.add(d);
        //if there is only 1 item in the queue, then write it.  If more than 1, we handle asynchronously in the callback above
        if(descriptorWriteQueue.size() == 1){
            gatt.writeDescriptor(d);
        }
    }

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        try {
            descriptorWriteQueue.remove();  //pop the item that we just finishing writing
            //if there is more to write, do it!
            if (descriptorWriteQueue.size() > 0)
                gatt.writeDescriptor(descriptorWriteQueue.element());

        } catch (Throwable t) {
            Log.w("LeBlue", "error during onDescriptorWrite callback", t);
        }
    }


    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        try {


            UUID uuid = characteristic.getUuid();

            byte[] bytes = characteristic.getValue();

            LeCharacteristicListener nullListener = uuidCharacteristicListeners.get(null);
            LeCharacteristicListener uuidListener = uuidCharacteristicListeners.get(uuid);

            if ((nullListener != null || uuidListener != null) && gatt != null) {
                LeGattCharacteristic43 characteristic43 = new LeGattCharacteristic43(gatt, characteristic);
                if (nullListener != null)
                    nullListener.leCharacteristicChanged(uuid, this, characteristic43);
                if (uuidListener != null)
                    uuidListener.leCharacteristicChanged(uuid, this, characteristic43);
            }
        } catch (Throwable t) {
            Log.w("LeBlue", "error during onCharacteristicChanged callback", t);
        }
    }



    @Override
    public String getName() {
        return remoteDevice43.getName();
    }

    @Override
    public void readRssi() {
        if (gatt != null) {
            gatt.readRemoteRssi();
        }
    }

    @Override
    public void onReadRemoteRssi(android.bluetooth.BluetoothGatt gatt, int rssi, int status)
    {
    if (status == BluetoothGatt.GATT_SUCCESS) {
        for (LeRemoteDeviceListener listener : listeners)
            listener.rssiRead(leDevice43, this, rssi);
    }
    }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || !(o instanceof LeRemoteDevice)) return false;
        LeRemoteDevice that = (LeRemoteDevice) o;
        return remoteDevice43.getAddress().equals(that.getAddress());
    }

    @Override
    public int hashCode() {
        return remoteDevice43.getAddress().hashCode();
    }

    @Override
    public String toString() {
        return "remote device: "+getAddress()+" "+getName();
    }

    BluetoothGatt gatt = null;
    BluetoothGattService gattService;
    List<BluetoothGattService> mBluetoothGattServices = null;
}




Java Source Code List

houtbecke.rs.le.BleException.java
houtbecke.rs.le.LeCharacteristicListener.java
houtbecke.rs.le.LeDefinedUUIDs.java
houtbecke.rs.le.LeDeviceListener.java
houtbecke.rs.le.LeDeviceState.java
houtbecke.rs.le.LeDevice.java
houtbecke.rs.le.LeFormat.java
houtbecke.rs.le.LeGattCharacteristic.java
houtbecke.rs.le.LeGattDescriptorValue.java
houtbecke.rs.le.LeGattDescriptor.java
houtbecke.rs.le.LeGattService.java
houtbecke.rs.le.LeGattStatus.java
houtbecke.rs.le.LeRecord.java
houtbecke.rs.le.LeRemoteDeviceListener.java
houtbecke.rs.le.LeRemoteDevice.java
houtbecke.rs.le.LeScanRecordImpl.java
houtbecke.rs.le.LeScanRecord.java
houtbecke.rs.le.LeUtil.java
houtbecke.rs.le.dummy.DummyLeRemoteDevice.java
houtbecke.rs.le.example.MainActivity.java
houtbecke.rs.le.fourdotthree.LeDevice43.java
houtbecke.rs.le.fourdotthree.LeGattCharacteristic43.java
houtbecke.rs.le.fourdotthree.LeGattService43.java
houtbecke.rs.le.fourdotthree.LeRemoteDevice43.java
houtbecke.rs.le.interceptor.AndroidEventSinkUtil.java
houtbecke.rs.le.interceptor.BaseIntercepting.java
houtbecke.rs.le.interceptor.EventSinkUtil.java
houtbecke.rs.le.interceptor.InterceptingLeCharacteristicListener.java
houtbecke.rs.le.interceptor.InterceptingLeDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeDevice.java
houtbecke.rs.le.interceptor.InterceptingLeGattCharacteristic.java
houtbecke.rs.le.interceptor.InterceptingLeGattService.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDeviceListener.java
houtbecke.rs.le.interceptor.InterceptingLeRemoteDevice.java
houtbecke.rs.le.interceptor.LeInterceptor.java
houtbecke.rs.le.interceptor.LeLogCatInterceptor.java
houtbecke.rs.le.interceptor.LeSessionInterceptor.java
houtbecke.rs.le.mock.LeDeviceMock.java
houtbecke.rs.le.mock.LeGattCharacteristicMock.java
houtbecke.rs.le.mock.LeGattServiceMock.java
houtbecke.rs.le.mock.LeMockController.java
houtbecke.rs.le.mock.LeRemoteDeviceMock.java
houtbecke.rs.le.mock.LeSessionController.java
houtbecke.rs.le.mock.NativeWaitNotify.java
houtbecke.rs.le.mock.SimpleWaitNotify.java
houtbecke.rs.le.mock.WaitNotify.java
houtbecke.rs.le.session.CharacteristicsMockerObject.java
houtbecke.rs.le.session.DeviceMockerObject.java
houtbecke.rs.le.session.EventSinkFiller.java
houtbecke.rs.le.session.EventSink.java
houtbecke.rs.le.session.EventSource.java
houtbecke.rs.le.session.EventType.java
houtbecke.rs.le.session.Event.java
houtbecke.rs.le.session.GattServiceMockerObject.java
houtbecke.rs.le.session.ListEventSinkSource.java
houtbecke.rs.le.session.MockedResponseObject.java
houtbecke.rs.le.session.MockedResponse.java
houtbecke.rs.le.session.MockerObject.java
houtbecke.rs.le.session.Mocker.java
houtbecke.rs.le.session.RemoteDeviceMockerObject.java
houtbecke.rs.le.session.SessionObject.java
houtbecke.rs.le.session.Session.java