外围模式下的Nexus 9不接受来自客户端的连接或不响应客户端请求?

时间:2015-02-13 06:05:19

标签: android bluetooth-lowenergy android-bluetooth

我在外设模式下使用Nexus 9。我创建了一个GATT服务器实例:

mGattServer = mBluetoothManager.openGattServer(this, mBluetoothGattServerCallback);

我创建了一个BluetoothGattService& BluetoothGattCharacteristic。 为GATT服务器增加了服务&服务的特点。

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

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

现在,当我尝试将GATT客户端连接到此服务器时,从不调用BluetoothGattServerCallback的onConnectionStateChange()方法。

来自客户端应用的代码:

连接到在Nexus 9上运行的GATT服务器

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

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) {

    }
};

设备(Nexus 9)是可发现的&我在onLeScan()中得到它。但是,当我尝试连接到在Nexus 9上运行的GATT服务器时,onConnectionStateChange中的状态始终为STATE_DISCONNECTED。

我尝试连接某些第三方应用程序,例如" B-BLE"," BLE Scanner"," Bluetooth 4.0 Explorer"。 Nexus 9在这些应用程序中是可发现的,但当我尝试连接到GATT服务器时,onConnectionStateChange()中的状态始终为STATE_DISCONNECTED。

任何类型的帮助都将受到赞赏。

提前致谢。

1 个答案:

答案 0 :(得分:0)

作为第一步,检查addService(..)方法是否返回true - 只有这样才能成功添加服务。

另外,尝试从onConnectionStateChange()方法中删除GATT_SUCCESS检查。据我所知,你不需要在那里测试......