Example usage for android.bluetooth BluetoothGattServerCallback BluetoothGattServerCallback

List of usage examples for android.bluetooth BluetoothGattServerCallback BluetoothGattServerCallback

Introduction

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

Prototype

BluetoothGattServerCallback

Source Link

Usage

From source file:io.v.android.libs.discovery.ble.BlePlugin.java

public BlePlugin(Context androidContext) {
    this.androidContext = androidContext;
    cachedDevices = new DeviceCache(Duration.standardMinutes(1));
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        return;/*w w w .java2  s .  c o  m*/
    }

    if (!hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
            && !hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
        return;
    }
    isEnabled = true;
    bluetoothLeAdvertise = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
    BluetoothManager manager = (BluetoothManager) androidContext.getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothGattServer = manager.openGattServer(androidContext, new BluetoothGattServerCallback() {
        @Override
        public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
            super.onConnectionStateChange(device, status, newState);
        }

        @Override
        public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
                BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
            byte[] total = characteristic.getValue();
            byte[] res = {};
            // Only send MTU - 1 bytes. The first byte of all packets is the op code.
            if (offset < total.length) {
                int finalByte = offset + MTU - 1;
                if (finalByte > total.length) {
                    finalByte = total.length;
                }
                res = Arrays.copyOfRange(total, offset, finalByte);
                bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, res);
            } else {
                // This should probably be an error, but a bug in the paypal/gatt code causes an
                // infinite loop if this returns an error rather than the empty value.
                bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, res);
            }
        }
    });
}

From source file:com.android.car.trust.CarBleTrustAgent.java

private void maybeStartBleUnlockService() {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "Trying to open a Ble GATT server");
    }/*from ww w.  j av  a  2  s .c om*/

    BluetoothManager btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    BluetoothGattServer mGattServer = btManager.openGattServer(this, new BluetoothGattServerCallback() {
        @Override
        public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
            super.onConnectionStateChange(device, status, newState);
        }
    });

    // The BLE stack is started up before the trust agent service, however Gatt capabilities
    // might not be ready just yet. Keep trying until a GattServer can open up before proceeding
    // to start the rest of the BLE services.
    if (mGattServer == null) {
        Log.e(TAG, "Gatt not available, will try again...in " + BLE_RETRY_MS + "ms");

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                maybeStartBleUnlockService();
            }
        }, BLE_RETRY_MS);
    } else {
        mGattServer.close();
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "GATT available, starting up UnlockService");
        }
        mCarUnlockService.start();
    }
}