我写了一个Android应用程序(Peripheral),可以做广告和设置gatt服务器 . 当我与其他Android设备(中央)连接时,例如使用 nRFConnect application或 LightBlue ,外围设备端在收到连接回调后立即断开连接 .

这是我设置gatt服务器的剪切代码:

// Create our primary service
    BluetoothGattService primaryService = new BluetoothGattService(
            UUIDs.UUID_SERVICE,
            BluetoothGattService.SERVICE_TYPE_PRIMARY
    );

    // Create our write characteristic
    BluetoothGattCharacteristic writeCharacteristic = new BluetoothGattCharacteristic(
            UUIDs.UUID_CHARACTERISTIC,
            BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
            BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM
    );

    // [START Add Client Characteristic Configuration]
    BluetoothGattDescriptor descriptor1 = new BluetoothGattDescriptor(
            UUIDs.UUID_CLIENT_CHARACTERISTIC_CONFIGURATION,
            (BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
    );
    descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    writeCharacteristic.addDescriptor(descriptor1);
    // [END Add Client Characteristic Configuration]

    // [START Add Characteristic User Description]
    BluetoothGattDescriptor descriptor2 = new BluetoothGattDescriptor(
            UUIDs.UUID_CHARACTERISTIC_USER_DESCRIPTION,
            (BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
    );
    try
    {
        descriptor2.setValue((new String("Sample Description")).getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    writeCharacteristic.addDescriptor(descriptor2);
    // [END Add Characteristic User Description]

    // Add the write characteristic to service
    if (primaryService != null)
    {
        // Add the characteristic to service
        primaryService.addCharacteristic(writeCharacteristic);
    }
    else
    {
        // TODO: handle this state...
    }

    // Add the primary service to gatt server
    if (mGattServer != null)
    {
        // Add the service to gatt server
        mGattServer.addService(primaryService);
    }
    else
    {
        // TODO: handle this state...
    }

我的 BluetoothGattServerCallback

public void onConnectionStateChange(BluetoothDevice device, int status, int newState)
{
    Log.d(TAG, "Our gatt server connection state changed, new state: " + Integer.toString(newState));

    super.onConnectionStateChange(device, status, newState);

    switch (newState)
    {
        case BluetoothProfile.STATE_CONNECTED:
            Log.d(TAG, "Connection State: STATE_CONNECTED");
            // Stop the advertisement
            //BLEManager.getInstance().stopAdvertising();
            // todo later...
            break;

        case BluetoothProfile.STATE_DISCONNECTED:
            Log.d(TAG, "Connection State: STATE_DISCONNECTED");
            // Start the advertisement again
            //BLEManager.getInstance().startAdvertising();
            // todo later...
            break;

        case BluetoothProfile.STATE_CONNECTING:
            Log.d(TAG, "Connection State: STATE_CONNECTING");
            // Nothing
            break;

        case BluetoothProfile.STATE_DISCONNECTING:
            Log.d(TAG, "Connection State: STATE_DISCONNECTING");
            // Nothing
            break;

        default:
            // Nothing
    }

    if (null != mConnectionCallback && BluetoothGatt.GATT_SUCCESS == status)
    {
        mConnectionCallback.onConnectionStateChange(device, newState);
    }
}

这是我的Peripheral方面的日志:

03-04 11:37:05.115  BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:05.118  GattServerCallback: Our gatt server connection state changed, new state: 2
03-04 11:37:05.118  GattServerCallback: Connection State: STATE_CONNECTED
03-04 11:37:06.975  BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:06.975  GattServerCallback: Our gatt server connection state changed, new state: 0
03-04 11:37:06.975  GattServerCallback: Connection State: STATE_DISCONNECTED
03-04 11:37:07.476  BluetoothGattServer: close()
03-04 11:37:07.476  BluetoothGattServer: unregisterCallback() - mServerIf=5

关于如何宣传和设置gatt服务器没有任何模糊,但是我无法通过Android Peripheral API或任何其他低级配置设置 BLE Connection Parameters ,所以一切都在这里!

有什么想法可以帮助我吗?!