我在 Android 应用程序(Java)和 ESP32 BLE 服务器之间的 Gatt 连接有问题

Posted

技术标签:

【中文标题】我在 Android 应用程序(Java)和 ESP32 BLE 服务器之间的 Gatt 连接有问题【英文标题】:I have a problem with Gatt connection between Android app(Java) and ESP32 BLE Server 【发布时间】:2021-03-20 18:55:22 【问题描述】:

我正在尝试将我的 android 应用程序连接到 ESP32 BLE gatt 服务器,并在收到通知后通过该服务器的特性接收一些整数值 但是它似乎不起作用 这是我的 Java 代码:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import com.example.logintask.R;

import java.util.ArrayList;
import java.util.List;


public class BluetoothConnection  extends AppCompatActivity 

    String TAG = "ScanActivity";
    BluetoothAdapter mBluetoothAdapter;
    BluetoothGatt mBluetoothGatt;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        Button bt = findViewById(R.id.button);
        bt.setOnClickListener(v -> 
            final BluetoothManager bluetoothManager =  (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();
            for(BluetoothDevice dt : mBluetoothAdapter.getBondedDevices())
                if(dt.getName().equals("ISCOPE"))
                    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(dt.getAddress());
                    BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback);
                    Log.d("Result",String.valueOf(bluetoothGatt.connect()));
                
            
        );
    
    BluetoothGattCallback mGattCallback =
            new BluetoothGattCallback() 
                @Override
                public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) 
                    if (newState == BluetoothProfile.STATE_CONNECTED) 
                        Log.i(TAG, "DEVICE CONNECTED. DISCOVERING SERVICES...");
                        mBluetoothGatt.discoverServices();
                     else if (newState == BluetoothProfile.STATE_DISCONNECTED) 
                        Log.i(TAG, "DEVICE DISCONNECTED");
                    
                
                @Override
                public void onServicesDiscovered(BluetoothGatt gatt, int status) 
                    if (status == BluetoothGatt.GATT_SUCCESS) 
                        Log.i(TAG, "SERVICES DISCOVERED. PARSING...");
                        displayGattServices(gatt.getServices());
                     else 
                        Log.i(TAG, "FAILED TO DISCOVER SERVICES");
                    
                
                @Override
                public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
                    if (status == BluetoothGatt.GATT_SUCCESS) 
                        Log.i(TAG, "ON CHARACTERISTIC READ SUCCESSFUL");
                     else 
                        Log.i(TAG, "ERROR READING CHARACTERISTIC");
                    
                
                @Override
                public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) 
                    super.onCharacteristicWrite(gatt, characteristic, status);
                    if (status == BluetoothGatt.GATT_SUCCESS) 
                        Log.i(TAG, "ON CHARACTERISTIC WRITE SUCCESSFUL");
                     else 
                        Log.i(TAG, "ERROR WRITING CHARACTERISTIC");
                    
                
                @Override
                public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) 
                    Log.i(TAG, "NEW NOTIFICATION RECEIVED");
                
                @Override
                public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) 
                    Log.i(TAG, "NEW RSSI VALUE RECEIVED");
                
            ;
    private void displayGattServices(List<BluetoothGattService> gattServices) 
        if (gattServices == null) return;
        for (BluetoothGattService gattService : gattServices) 
            Log.i(TAG, "SERVICE FOUND: " + gattService.getUuid().toString());
            for (BluetoothGattCharacteristic gattCharacteristic : gattService.getCharacteristics()) 
                Log.i(TAG, "  CHAR. FOUND: " + gattCharacteristic.getUuid().toString());
            
        
    

这是我的 Logcat:

2021-03-20 20:49:19.651 30080-30080/com.example.logintask D/BluetoothGatt: registerApp()
2021-03-20 20:49:19.651 30080-30080/com.example.logintask D/BluetoothGatt: registerApp() - UUID=2318d701-b8f5-4cd9-be2c-cd83fee35f8d
2021-03-20 20:49:19.654 30080-30080/com.example.logintask D/Result: true
2021-03-20 20:49:19.654 30080-30116/com.example.logintask D/BluetoothGatt: onClientRegistered() - status=0 clientIf=6
2021-03-20 20:49:24.812 30080-30116/com.example.logintask D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=6 device=9C:9C:1F:C9:B5:72
2021-03-20 20:49:24.812 30080-30116/com.example.logintask D/State: 0
2021-03-20 20:49:24.812 30080-30116/com.example.logintask I/ScanActivity: DEVICE DISCONNECTED

最后这是我的 ESP32 代码:

#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"


class MyServerCallbacks: public BLEServerCallbacks 
    void onConnect(BLEServer* pServer) 
      deviceConnected = true;
      BLEDevice::startAdvertising();
    ;

    void onDisconnect(BLEServer* pServer) 
      deviceConnected = false;
    
;



void setup() 
  Serial.begin(115200);

  // Create the BLE Device
  BLEDevice::init("ISCOPE");

  // Create the BLE Server
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pCharacteristic = pService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY |
                      BLECharacteristic::PROPERTY_INDICATE
                    );

  // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
  // Create a BLE Descriptor
  pCharacteristic->addDescriptor(new BLE2902());

  // Start the service
  pService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(false);
  pAdvertising->setMinPreferred(0x0);  // set value to 0x00 to not advertise this parameter
  BLEDevice::startAdvertising();
  Serial.println("Waiting a client connection to notify...");


void loop() 
    // notify changed value
    if (deviceConnected) 
        pCharacteristic->setValue((uint8_t*)&value, 4);
        pCharacteristic->notify();
        value++;
        delay(10); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
    
    // disconnecting
    if (!deviceConnected && oldDeviceConnected) 
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
    
    // connecting
    if (deviceConnected && !oldDeviceConnected) 
        // do stuff here on connecting
        oldDeviceConnected = deviceConnected;
    

有趣的是,当我通过 nRF Connect 应用程序进行连接时,它可以正常工作并获得我的特征的价值。我是一个新手程序员,所以任何帮助将不胜感激。提前致谢!!

【问题讨论】:

【参考方案1】:

我建议开始使用:https://github.com/NordicSemiconductor/Android-nRF-Toolbox,您可以在“问题”部分找到代码和疑难解答。 然后你修改代码并制作你自己的版本,看看 .java 文件的结构,使用 Singleton。建议最好从您知道可行的事情开始。

【讨论】:

【参考方案2】:

您的连接失败,状态为 133。这里有很多关于该主题的问题。这是android的一个已知问题。 This answer 建议使用不同版本的 connectGatt 方法,对您来说看起来像这样:

BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback, BluetoothDevice.TRANSPORT_LE);

这需要您使用 Android 6.0 (API 23) 或更高版本,但应该可以解决您的问题。如果您需要使用较早版本的 Android,您可以重试连接直到它正常工作。

【讨论】:

谢谢你成功了! @АтанасДонев 如果对您有帮助,请选择我的答案作为正确答案:)

以上是关于我在 Android 应用程序(Java)和 ESP32 BLE 服务器之间的 Gatt 连接有问题的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android上使用OpenGL ES 2.0绘制点

Android之使用JAVA占位符格式数据(很实用)

Android:了解 OnDrawFrame、FPS 和 VSync (OpenGL ES 2.0)

在 Android 中使用带有 OpenGL ES 的 VBO 性能不佳

在 OpenGL ES Android 中使用 glBufferSubData 和 glMultiDrawArrays

我在 Android 应用程序(Java)和 ESP32 BLE 服务器之间的 Gatt 连接有问题