Example usage for android.bluetooth BluetoothGattServer close

List of usage examples for android.bluetooth BluetoothGattServer close

Introduction

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

Prototype

public void close() 

Source Link

Document

Close this GATT server instance.

Usage

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 ava  2s. co m*/

    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();
    }
}