Nexus 9 in Peripheral mode is not accepting connection from clients or not responding to client requests?

I am using Nexus 9 in peripheral mode. I have created a instance of GATT server:

mGattServer = mBluetoothManager.openGattServer(this, mBluetoothGattServerCallback);

I have created a BluetoothGattService & BluetoothGattCharacteristic. Added service to GATT server & characteristic to service.

BluetoothGattService service =new BluetoothGattService(SERVICE_UUID,
            BluetoothGattService.SERVICE_TYPE_PRIMARY);

BluetoothGattCharacteristic offsetCharacteristic =
            new BluetoothGattCharacteristic(CHARACTERISTIC_NUM_UUID,
                    //Read+write permissions
                    BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
                    BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

service.addCharacteristic(offsetCharacteristic);

mGattServer.addService(service);

Instance of BluetoothGattServerCallBack:

private BluetoothGattServerCallback mBluetoothGattServerCallback = new BluetoothGattServerCallback() {
    @Override
    public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
        //super.onConnectionStateChange(device, status, newState);
        Log.i(TAG, "onConnectionStateChange "
                + getStatusDescription(status) + " "
                + getStateDescription(newState));

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "Connected..");

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "Disconnected..");
        }
    }

    @Override
    public void onServiceAdded(int status, BluetoothGattService service) {
        //super.onServiceAdded(status, service);
        Log.i(TAG, "onServiceAdded");
    }

    @Override
    public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
        super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
        Log.i(TAG, "onCharacteristicWriteRequest");
    }

    @Override
    public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
        Log.i(TAG, "onCharacteristicReadRequest");
    }

    @Override
    public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
        super.onDescriptorReadRequest(device, requestId, offset, descriptor);
        Log.i(TAG, "onDescriptorReadRequest");
    }

    @Override
    public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
        super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);
        Log.i(TAG, "onDescriptorWriteRequest");
    }

    @Override
    public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
        super.onExecuteWrite(device, requestId, execute);
        Log.i(TAG, "onExecuteWrite");
    }

    @Override
    public void onNotificationSent(BluetoothDevice device, int status) {
        super.onNotificationSent(device, status);
        Log.i(TAG, "onNotificationSent");
    }
};

Now when I am trying to connect a GATT client to this server, onConnectionStateChange() method of BluetoothGattServerCallback never invoked.

Code from Client Side app:

To connect to GATT server running on Nexus 9

mConnectedGatt = device.connectGatt(this, false, mGattCallback); 

Instance of BluetoothGattCallback:

private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
            Log.i(TAG, "Connected to server");
            gatt.discoverServices();
        } else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
            Log.i(TAG, "Disconnected from server");
        } else if (status != BluetoothGatt.GATT_SUCCESS) {
            gatt.disconnect();
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {         
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {        
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {         
    }

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

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

    }
};

Device(Nexus 9) is discoverable & I am getting it in onLeScan(). But when I am trying to connect to GATT server running on Nexus 9, status in onConnectionStateChange it always STATE_DISCONNECTED.

I tried to connect from some third party applications like "B-BLE", "BLE Scanner", "Bluetooth 4.0 Explorer". Nexus 9 is discoverable in these applications but when I am trying to connect to GATT server, status in onConnectionStateChange() is always STATE_DISCONNECTED.

Any type of help will be appreciated.

Thanks in advance.

Upvotes: 3

Views: 704

Answers (1)

origami addict
origami addict

Reputation: 108

As a first step, check if the addService(..) method returns true - only then has the service successfully been added.

Also, try removing the GATT_SUCCESS check from the onConnectionStateChange() method. As far as I know you don't need to test for this there...

Upvotes: 0

Related Questions