Android app(Java) 和 ESP32 BLE Server 之间的 Gatt 连接有问题
I have a problem with Gatt connection between Android app(Java) and ESP32 BLE Server
我正在尝试将我的 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 应用程序进行连接时,它可以正常工作并获得我的特征值。我是一名新手程序员,因此我们将不胜感激。提前致谢!!
您的连接失败,状态为 133。这里有很多关于该主题的问题。这是 android 的一个已知问题。 This answer suggest to use a different version of the connectGatt 方法对您来说是这样的:
BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
这需要您使用 Android 6.0 (API 23) 或更新版本,但应该可以解决您的问题。如果您需要使用早期版本的 Android,您可以重试连接直到它起作用。
我建议开始使用:https://github.com/NordicSemiconductor/Android-nRF-Toolbox 您可以在“问题”部分找到代码和故障排除。
然后你修改代码并制作你自己的版本,看看 .java 文件的结构,使用 Singleton。建议,最好从你知道它有效的东西开始。
我正在尝试将我的 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 应用程序进行连接时,它可以正常工作并获得我的特征值。我是一名新手程序员,因此我们将不胜感激。提前致谢!!
您的连接失败,状态为 133。这里有很多关于该主题的问题。这是 android 的一个已知问题。 This answer suggest to use a different version of the connectGatt 方法对您来说是这样的:
BluetoothGatt bluetoothGatt = device.connectGatt(getApplicationContext(), false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
这需要您使用 Android 6.0 (API 23) 或更新版本,但应该可以解决您的问题。如果您需要使用早期版本的 Android,您可以重试连接直到它起作用。
我建议开始使用:https://github.com/NordicSemiconductor/Android-nRF-Toolbox 您可以在“问题”部分找到代码和故障排除。 然后你修改代码并制作你自己的版本,看看 .java 文件的结构,使用 Singleton。建议,最好从你知道它有效的东西开始。