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

Android 外设模式下的Nexus 9不接受来自客户端的连接或不响应客户端请求?,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,我正在外设模式下使用Nexus9。我已经创建了GATT服务器的一个实例: mGattServer = mBluetoothManager.openGattServer(this, mBluetoothGattServerCallback); 我创建了一个BluetoothGattService&BluetoothGattCharacteristic。 将服务添加到GATT服务器&将特性添加到服务 BluetoothGattService service =new BluetoothGattSe

我正在外设模式下使用Nexus9。我已经创建了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()中获取它。但是,当我试图连接到运行在Nexus9上的GATT服务器时,onConnectionStateChange中的状态总是表示_DISCONNECTED

我尝试从一些第三方应用程序连接,如“B-BLE”、“BLE扫描仪”、“蓝牙4.0浏览器”。在这些应用程序中可以发现Nexus 9,但当我尝试连接到GATT服务器时,onConnectionStateChange()中的状态始终为STATE_DISCONNECTED

任何形式的帮助都将不胜感激


提前感谢。

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


另外,尝试从onConnectionStateChange()方法中删除GATT_成功检查。据我所知,您不需要在那里进行测试…

您能解决这个问题吗?我有相同的问题?