我正在开发一个联合工作的Android应用程序/ ESP32应用程序,以通过BLE进行通信。 目标是能够做广告,并管理连接以为每个Android手机交换特定数据。 在我的ESP32上,我希望能够有一个针对GATT特性的回调,以及一个针对服务器的回调。 第一个用于广告数据,第二个用于管理 indicate 功能并通过连接进行通信。 如果仅针对该特性设置一套,则可以正常工作,但我无法访问连接状态(无论是否连接了设备),如果仅针对服务器设置一个设置,我会得到一个大师冥想错误,可能是因为我在同一时间做广告...
结果,在我的Android代码中,我从来没有进入过onCharacteristicChanged()方法。
是否有可能实现我的目标?如果是,怎么办?
我主要遵循以下示例:https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/examples/BLE_notify/BLE_notify.ino
ESP32
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE_Carousel.h>
#include <BLE2902.h>
#include <iostream>
#include <algorithm>
#include <ArduinoJson.h>
using namespace std;
#define PACKET_SIZE 24
#define HEADER_SIZE 3 // Object ID 1 byte, Object size 1 byte Packet Number 1 byte
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" // UUID dedicated to consent transmission
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" // Characterisitic that will receive the consent
int idx = 0;
BLEAdvertising *pAdvertising;
BLEServer *pServer = NULL;
BLE_Carousel carousel = BLE_Carousel(PACKET_SIZE, HEADER_SIZE);
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
uint32_t value = 0;
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
std::cout << "1" << "\n";
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
std::cout << "0" << "\n";
}
};
class MyCallBack: public BLECharacteristicCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
std::cout << "1" << "\n";
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
std::cout << "0" << "\n";
}
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if ((rxValue.length() > 0) && (rxValue.find ('&') == std::string::npos)) {
Serial.println("*********");
}
}
}; //end of callback
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("InCon_Beacon");
pServer = BLEDevice::createServer();
//pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
pCharacteristic->addDescriptor(new BLE2902());
// Configure the characteristic
// The maximum length of an attribute value shall be 512 octets.
pCharacteristic->setValue("Value");
pCharacteristic->setCallbacks(new MyCallBack());
Serial.println("Characteristic defined!");
// Allocate the buffer that will contain the privacy policy
byte pp[pp_size] = { ... };
// Initialize the Carousel
carousel.set_Data(pp, pp_size);
// Configure the payload of advertising packets
byte* packet = (byte*)carousel.get_Packet( 0);
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
//std::string strServiceData = "";
std::string strServiceData = "";
strServiceData += (char)11; // Len
strServiceData += (char)0xFF; // Type
strServiceData += "0123456789";
oAdvertisementData.addData(strServiceData);
pAdvertising = pServer->getAdvertising();
//Serial.println("Payload:");
//Serial.println(oAdvertisementData.getPayload().c_str());
//pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
set_new_adv_payload(idx);
// Start service and advertising
pService->start();
pAdvertising->start();
}
void set_new_adv_payload(int idx) {
//Serial.println(idx);
BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04
std::string strServiceData = "";
//Serial.println(carousel.get_Nb_Packets());
strServiceData += (char)(PACKET_SIZE + 1); // Len
strServiceData += (char)0xFF; // Type
byte* packet = (byte*)carousel.get_Packet( idx);
for (int i = 0; i < PACKET_SIZE; i++) {
strServiceData += (char) packet[i];
}
free(packet);
//Serial.println(idx);
oAdvertisementData.addData(strServiceData);
pAdvertising->setAdvertisementData(oAdvertisementData);
}
void loop() {
delay(200);
//Serial.println(idx);
set_new_adv_payload(idx);
idx = (idx + 1) % carousel.get_Nb_Packets();
if (deviceConnected) {
pCharacteristic->setValue((uint8_t*)&value, 4);
pCharacteristic->notify();
value++;
std::cout << value << "\n";
delay(3); // 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
}
}
处理通知/指示
BluetoothGatt bleGatt;
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString("4fafc201-1fb5-459e-8fcc-c5c9c331914b"))
.getCharacteristic(UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"));
characteristic.setValue(new byte[]{1, 1});
gatt.writeCharacteristic(characteristic);
// String test = "test";
// characteristic.setValue(test);
// gatt.writeCharacteristic(characteristic);
gatt.disconnect();
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (newState == STATE_CONNECTED){
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status){
BluetoothGattCharacteristic characteristic =
gatt.getService(UUID.fromString("4fafc201-1fb5-459e-8fcc-c5c9c331914b"))
.getCharacteristic(UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8"));
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));//enable gattcharac to have indication
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
characteristic.getValue();
Log.i("ohyes",characteristic.getValue().toString());
}
};
bleGatt = mDeviceStore.getDeviceList().get(0).getDevice().connectGatt(this, true, gattCallback);
在处理常规关贸协定时
BGS = mBluetoothLeService.getSupportedGattServices();
for (BluetoothGattService gattService : BGS) {
if (gattService.getUuid().toString().equals("4fafc201-1fb5-459e-8fcc-c5c9c331914b")) {
List<BluetoothGattCharacteristic> BGC = gattService.getCharacteristics();
for (BluetoothGattCharacteristic gattCharac : BGC) {
if (gattCharac.getUuid().toString().equals("beb5483e-36e1-4688-b7f5-ea07361b26a8")) {
//String s = PolicyEngine.policyModelToProto(intersection).toString();
PilotPolicyProto.PilotPolicy.Builder pol = PolicyEngine.policyModelToBuilder(DSP);
try {
String s = JsonFormat.printer().print(pol);
// s = s.substring(s.indexOf('\n')+1);
// s = s+"{";
Log.i("dede", s);
gattCharac.setValue("&" + s);
boolean consent = mBluetoothLeService.mBluetoothGatt.writeCharacteristic(gattCharac);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
}
}
}
Core 1 register dump:
PC : 0x40166dc5 PS : 0x00060330 A0 : 0x800d6ae5 A1 : 0x3ffc9af0
A2 : 0x00000074 A3 : 0x3ffc9b24 A4 : 0x3ffc4e70 A5 : 0x00000000
A6 : 0x00000001 A7 : 0x3ffe05e4 A8 : 0x3ffc9b30 A9 : 0x3ffc9ad0
A10 : 0x3ffc9b2c A11 : 0x3ffc4e6c A12 : 0x3ffc4e70 A13 : 0x3f401c72
A14 : 0x00000002 A15 : 0x00000001 SAR : 0x00000008 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000074 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0xffffffff