Android 10 против Android 7 BLE соединение - PullRequest
0 голосов
/ 15 марта 2020

У меня проблема с приложением Android, подключающим модуль RN4020. Когда я создаю приложение на Samsung S7, у меня есть успех в подключении и передаче данных, но как только я пытаюсь сделать это с моей запиской, я не могу подключиться. Само устройство не видит ни одного телефона, подключенного при использовании Примечание 9. На модуле это светодиод подключения, который показывает, подключено ли устройство. При использовании S7 Bluetooth показывает соединение (индикатор включен), но в Note 9 индикатор выключен.

// Open a BluetoothGatt connection to a BLE device given its address
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {                             //Check that we have a Bluetooth adappter and device address
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");     //Log a warning that something went wrong
        return false;                                                               //Failed to connect
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.d(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false. 
    mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

введите описание изображения здесь введите описание изображения здесь

введите описание изображения здесь

// Open a BluetoothGatt connection to a BLE device given its address
public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {                             //Check that we have a Bluetooth adappter and device address
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");     //Log a warning that something went wrong
        return false;                                                               //Failed to connect
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.d(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
    } else {
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
    }
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

1 Ответ

0 голосов
/ 15 марта 2020

Для Android M и выше в методе connect вы должны передать транспортный тип LE в качестве аргумента.

Я написал библиотеку Ble общего назначения в одном из моих приложений и пока что работает хорошо. Убедитесь, что вы вызываете этот метод в главном потоке.

 private boolean connectGatt(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }
        final BluetoothDevice device = mBluetoothAdapter
                .getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
        } else {
            mBluetoothGatt = device.connectGatt(mContext, false, mGattCallback);
        }
        Log.d(TAG, "Trying to create a new connection.");
        return mBluetoothGatt.connect();
    }

Надеюсь, это поможет !! Удачного кодирования.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...