Writing characteristic failed error when writing to ESP32 GATT characteristic using protobufjs and ionic capacitor BLE p

karunt
Posts: 93
Joined: Sat Apr 03, 2021 7:58 am

Writing characteristic failed error when writing to ESP32 GATT characteristic using protobufjs and ionic capacitor BLE p

Postby karunt » Fri Sep 03, 2021 10:13 am

Building Android app from Angular (using Ionic) to scan a BLE device (ESP32 chip) and write to its GATT characteristic. Using Capacitor BLE plug-in for BLE communication and protobufjs to create message (since ESP32 code uses Google's Protocol Buffer mechanism to accept messages). App deployed on Samsung Galaxy Tab A7 to deploy app. Get "Writing characteristic failed" error message when executing BLEClient.write and can't understand why it's failing. Here's my code (getBLE() invoked when user clicks a button):

Code: Select all

import { load } from '../../../node_modules/protobufjs';
import { BleClient } from '@capacitor-community/bluetooth-le';

export class SetupPage implements OnInit {

  async getBLE(){
    try {
      let wifiid = 'wifiSSID';
      let password = 'somePassword';
      await load ('../../assets/wifi_config.proto')
      .then((root) => {
        var wifi_config_message = root.lookupType("wifi_config_package.CmdSetConfig");
        let message = wifi_config_message.create({
          ssid: wifiid,
          passphrase: password
        });
        let buffer1 = wifi_config_message.encode(message).finish();
        return buffer1;
      })
      .then(async buffer1 => {
        await BleClient.initialize();
        let connectedDevice = await BleClient.requestDevice();
        await BleClient.connect(connectedDevice.deviceId);
        let bufferView = await new DataView(buffer1.buffer);
        const result = await BleClient.write(connectedDevice.deviceId, '021a9004-0382-4aea-bff4-6b3f1c5adfb4', '021aff52-0382-4aea-bff4-6b3f1c5adfb4',bufferView);
      });
    }
    catch(error) {
      console.log('bluetooth error is: ', error);
    }
  }
}
This is what the wifi_config.proto file looks like:

Code: Select all

package wifi_config_package;
syntax = "proto3";

import "constants.proto";
import "wifi_constants.proto";

message CmdGetStatus {}

message RespGetStatus {
    Status status = 1;
    WifiStationState sta_state = 2;
    oneof state {
        WifiConnectFailedReason fail_reason = 10;
        WifiConnectedState connected = 11;
    }
}

message CmdSetConfig {
    string ssid = 1;
    string passphrase = 2;
    bytes bssid = 3;
    int32 channel = 4;
}

message RespSetConfig {
    Status status = 1;
}

message CmdApplyConfig {}

message RespApplyConfig {
    Status status = 1;
}

enum WiFiConfigMsgType {
    TypeCmdGetStatus = 0;
    TypeRespGetStatus = 1;
    TypeCmdSetConfig = 2;
    TypeRespSetConfig = 3;
    TypeCmdApplyConfig = 4;
    TypeRespApplyConfig = 5;
}

message WiFiConfigPayload {
    WiFiConfigMsgType msg = 1;
    oneof payload {
        CmdGetStatus cmd_get_status = 10;
        RespGetStatus resp_get_status = 11;
        CmdSetConfig cmd_set_config = 12;
        RespSetConfig resp_set_config = 13;
        CmdApplyConfig cmd_apply_config = 14;
        RespApplyConfig resp_apply_config = 15;
    }
}
What am I doing incorrectly? Any alternatives to achieve the same objective (send data over BLE to ESP32 by writing to GATT characteristic)? I'd prefer not to resort to native Android app development since I have no experience with it and am on a short deadline. Also, not very comfortable with Protocol Buffer mechanism, so some guidance is appreciated.

Who is online

Users browsing this forum: No registered users and 19 guests