Example usage for android.bluetooth BluetoothProfile GATT

List of usage examples for android.bluetooth BluetoothProfile GATT

Introduction

In this page you can find the example usage for android.bluetooth BluetoothProfile GATT.

Prototype

int GATT

To view the source code for android.bluetooth BluetoothProfile GATT.

Click Source Link

Document

GATT

Usage

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

@Override
public void getConnectedDevices(JSONArray json, CallbackContext callbackContext) {
    Log.i(TAG, "getConnectedDevices");
    JSONArray ary = new JSONArray();
    List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    for (int i = 0; i < devices.size(); i++) {
        JSONObject obj = new JSONObject();
        Tools.addProperty(obj, Tools.DEVICE_ADDRESS, devices.get(i).getAddress());
        Tools.addProperty(obj, Tools.DEVICE_NAME, devices.get(i).getName());
        ary.put(obj);/*from   w w  w.j a v  a 2s  .  c o  m*/
    }
    callbackContext.success(ary);
}

From source file:com.ec.android.module.bluetooth40.BluetoothLeService.java

/**
 * Connects to the GATT server hosted on the Bluetooth LE device.
 *
 * @param address The device address of the destination device.
 * @return Return true if the connection is initiated successfully. The connection result
 * is reported asynchronously through the
 * {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
 * callback.//from  ww  w.ja v a2  s .  c om
 */
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }
    //address??mac??
    if (!BluetoothAdapter.checkBluetoothAddress(address)) {
        Log.w(TAG, "address??mac??");
        return false;
    }
    ////FIXME
    /*if (mBluetoothGatt != null) {
    disconnect();
    close();
    }*/
    if (mBluetoothGatt != null && mBluetoothManager != null) {
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

        if (connectionState == BluetoothGatt.STATE_CONNECTED) {
            //
            if (DEBUG) {
                Toast.makeText(getApplicationContext(), "?", Toast.LENGTH_SHORT)
                        .show();
            }
            Log.d(TAG, "?");
            return false;
        }
    }
    ////
    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }
    //
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;

}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

@Override
public void onCreate() {
    super.onCreate();
    mDeviceListeners = new ArrayList<BleDeviceListener>();
    bleDevices = new BleDevices() {
        @Override//from  ww w . j a  v  a 2s . c  o m
        public void onDeviceAdded(BluetoothDevice device, int rssi, byte[] scanRecord) {
            for (BleDeviceListener listener : mDeviceListeners) {
                listener.onDeviceAdded(device);
            }
        }

        @Override
        public void onDeviceRemoved(BluetoothDevice device) {
            for (BleDeviceListener listener : mDeviceListeners) {
                listener.onDeviceRemoved(device);
            }
        }
    };
    handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (MSG_PRUNE == msg.what && bleDevices != null) {
                Log.v(TAG, "Pruning devices");
                BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
                bleDevices.pruneOldDevices(manager.getConnectedDevices(BluetoothProfile.GATT));
            }
            return false;
        }
    });
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

boolean connect(String address) {
    BluetoothDevice device = btAdapter.getRemoteDevice(address);
    if (device == null) {
        return false;
    }//w  w  w  .j a  v  a2s .c o  m
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    int connectionState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
    if (bluetoothGatt != null && connectionState != BluetoothProfile.STATE_CONNECTED) {
        return bluetoothGatt.connect();
    }
    if (bluetoothGatt != null && connectionState == BluetoothProfile.STATE_CONNECTED) {
        sendGattBroadcast(address, BleEvents.GATT_CONNECT, null);
        return true;
    }

    if (bluetoothGatt != null) {
        bluetoothGatt.close();
    }
    bluetoothGatt = device.connectGatt(this, false, // autoConnect = false
            gattCallbacks);
    addressToGattClient.put(address, bluetoothGatt);
    return true;
}

From source file:com.google.android.apps.forscience.ble.MyBleService.java

public void disconnectDevice(String address) {
    BluetoothGatt bluetoothGatt = addressToGattClient.get(address);
    if (btAdapter == null || address == null || bluetoothGatt == null) {
        return;//from www  .  j a v a  2s .  com
    }
    BluetoothDevice device = btAdapter.getRemoteDevice(address);
    int bleState = bluetoothManager.getConnectionState(device, BluetoothProfile.GATT);
    if (bleState != BluetoothProfile.STATE_DISCONNECTED && bleState != BluetoothProfile.STATE_DISCONNECTING) {
        bluetoothGatt.disconnect();
    } else {
        bluetoothGatt.close();
        addressToGattClient.remove(address);
        sendGattBroadcast(address, BleEvents.GATT_DISCONNECT, null);
    }
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeGattPeripheral.java

@Override
@RequiresPermission(Manifest.permission.BLUETOOTH)
public @ConnectivityStatus int getConnectionStatus() {
    if (gatt != null) {
        final @ConnectivityStatus int status = stack.bluetoothManager.getConnectionState(bluetoothDevice,
                BluetoothProfile.GATT);
        return status;
    } else {/*from  w  w w  .j av a  2 s.c om*/
        return STATUS_DISCONNECTED;
    }
}

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

private boolean enableBluetoothConnectivity() {
    Log.d(TAG, "EnableBluetoothConnectivity");
    if (mBluetoothManager == null) {
        mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
        if (mBluetoothManager == null) {
            Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothManager");
            return false;
        }/*www . j a  va  2  s.  c om*/
    }

    mBluetoothAdapter = mBluetoothManager.getAdapter();
    if (mBluetoothAdapter == null) {
        Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothAdapter");
        return false;
    }

    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    if (mBluetoothLeScanner == null) {
        Log.w(TAG, "EnableBluetoothConnectivity: Failed to obtain BluetoothLeScanner");
        return false;
    }

    // The first auto-connection after boot might be missed due to starting time of the updater service.
    List<BluetoothDevice> connectedDevices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
    for (BluetoothDevice device : connectedDevices) {
        if (isTargetKeyboard(device) && !isUpdateServiceInUse()) {
            Log.d(TAG, "enableBluetoothConnectivity: Found keyboard " + device.getName() + " ["
                    + device.getAddress() + "] connected");
            obtainKeyboardInfo(device.getName(), device.getAddress());
            break;
        }
    }

    return true;
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void connectToDevice(BluetoothDevice device) {
    try {//from w  ww  .  j a  va2 s .c  o m
        int connState = mBluetoothManager.getConnectionState(device, BluetoothGatt.GATT);

        switch (connState) {
        case BluetoothGatt.STATE_CONNECTED: {
            if (mBluetoothAdapter == null)
                return;

            final BluetoothDevice leDevice = mBluetoothAdapter.getRemoteDevice((String) null);
            int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

            if (mGatt != null) {
                if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
                    mGatt.disconnect();
                }
            }
        }
            break;
        case BluetoothGatt.STATE_DISCONNECTED:
            if (mGatt == null) {
                scanLeDevice(false);
                mGatt = device.connectGatt(this, false, gattCallback);
            }
            break;
        default:
            Log.i("connect to device : ", "device busy");
            break;
        }
    } catch (Exception ex) {
        Log.i("Exception", "connectToDevice");
    }
}

From source file:br.liveo.ndrawer.ui.activity.MainActivity.java

License:asdf

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void disconnect(String address) {
    if (mBluetoothAdapter == null)
        return;//from w  ww .j  ava2s.c o  m

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    int connectionState = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

    if (mGatt != null) {
        if (connectionState != BluetoothProfile.STATE_DISCONNECTED) {
            mGatt.disconnect();
        }
    }
}