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);
}
}
}
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;
}
}