Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write custom data #7

Open
priya-kato opened this issue May 19, 2023 · 5 comments
Open

Cannot write custom data #7

priya-kato opened this issue May 19, 2023 · 5 comments

Comments

@priya-kato
Copy link

When send custom data before provision And after scan networks it show undefined is not an object/ provision init failed .
After provision also not working.

@pikann
Copy link
Collaborator

pikann commented May 19, 2023

Could you please give me the app and device logging?

@priya-kato
Copy link
Author

`import React, {useState} from 'react';
import {
View,
Button,
Text,
ToastAndroid,
PermissionsAndroid,
} from 'react-native';
import EspIdfProvisioningReactNative from '@digitalfortress-dev/esp-idf-provisioning-react-native';

const App = () => {
const [uuid, setUuid] = useState('');
const [pop, setPop] = useState('');

EspIdfProvisioningReactNative.create();

const scanFunc = async () => {
await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN
);
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT
);
console.log('scan init');
EspIdfProvisioningReactNative.scanBleDevices('PROV_')
.then((res) => {
console.log(res);
if (res.length > 0) {
setUuid(res[0].serviceUuid);
} else {
setUuid('');
}
})
.catch((e) => {
console.log(e);
});
};

const connectFun = () => {
EspIdfProvisioningReactNative.connectToBLEDevice(uuid)
.then((_res) => {
ToastAndroid.show('Connected to device', ToastAndroid.LONG);
})
.catch((e) => {
ToastAndroid.show('Connect to device error', ToastAndroid.LONG);
console.log(e);
});
};

const disconnectFun = () => {
EspIdfProvisioningReactNative.disconnectBLEDevice();
};

const setProof = () => {
EspIdfProvisioningReactNative.setProofOfPossession('abcd1234');
ToastAndroid.show('Pop: abcd1234', ToastAndroid.LONG);
};

const getProof = async () => {
EspIdfProvisioningReactNative.getProofOfPossession()
.then((_pop) => {
setPop(_pop);
})
.catch((e) => {
ToastAndroid.show('Get pop error', ToastAndroid.LONG);
console.error(e);
});
};

const scanNetworks = async() => {
await EspIdfProvisioningReactNative.create();
await EspIdfProvisioningReactNative.scanNetworks()
.then((res) => {
ToastAndroid.show(
'Number of networks found: ' + res.length,
ToastAndroid.LONG,
);
})
.catch((e) => {
ToastAndroid.show('Scan networks error', ToastAndroid.LONG);
console.log(e);
});
await EspIdfProvisioningReactNative.sendCustomData('custom-endpoint', 'testinho')
.then((resp) => {
ToastAndroid.show(
'Custom data provisioned successfully',
ToastAndroid.LONG,
);
console.log(resp);
})
.catch((e) => {
ToastAndroid.show(
'Provision Custom Data Error, ' + e.getMessage(),
ToastAndroid.LONG,
);
console.log(e);
});
};

const provCreds = () => {
EspIdfProvisioningReactNative.provisionNetwork(
'UniFi',
'q6RiVZZNcs',
)
.then((resp) => {
ToastAndroid.show(
'Credentials provided with success',
ToastAndroid.LONG,
);
console.log(resp);
})
.catch((e) => {
ToastAndroid.show('Provide creds error', ToastAndroid.LONG);
console.log(e);
});
};

const provCustom = () => {
EspIdfProvisioningReactNative.sendCustomData('custom-endpoint', 'testinho')
.then((resp) => {
ToastAndroid.show(
'Custom data provisioned successfully',
ToastAndroid.LONG,
);
console.log(resp);
})
.catch((e) => {
ToastAndroid.show(
'Provision Custom Data Error, ' + e.getMessage(),
ToastAndroid.LONG,
);
console.log(e);
});
};

const provCustomWithByteData = () => {
// Often the strings communicating over BLE are really binary represented as UTF-8
// however UTF-16 is the react-native spec for strings
// These strings are usually generated with a cipher, perhaps protobuf (on npmjs)
// what's important is that React-Native is programmed in C. And C will trim trailing
// \x00 characters from strings. This has a big impact on BLE communication

// usually using Protocl Buffers, one would cmd.serializeBinary()
// which renders protcol buffer messages into binary wire format, a Uint8Array in JS
// const cmdContent = cmd.serializeBinary();
// however for demonstration let's start with a string with problematic trailing characters
const string = 'R\u0000';
console.log(string); // R�
// render string as Uint8Array, a typed array that represents an array of 8-bit unsigned integers
// this is the medium most common to protocol buffers
const strToBuf = (str) => {
  var buf = new ArrayBuffer(str.length);
  var bufView = new Uint8Array(buf);
  for (let i = 0; i < str.length; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return bufView;
};
const stringAsByteArray = strToBuf(string);
console.log(stringAsByteArray); // [82, 0] these numbers are in decimal, need to be hexi
// a Uint8Array in JS isn't exactly an array, more like {0: 82, 1:0} and yet there's more differences
// below converts the Uint8Array into a JS array of strings,
// each string is hexidecimal value of corresponding byte

// using JSON.stringify below so it makes clear whether the values are strings or numbers
console.log('hexArrayOfCmdContent: ', JSON.stringify(hexArrayOfCmdContent)); // ["52", "0"]

EspIdfProvisioningReactNative.sendCustomDataWithByteData(
  'custom-endpoint',
  hexArrayOfCmdContent,
)
  .then((resp) => {
    // this is worth tweaking, but I have found that the data response
    // is wrapped in some other packaging I have to strip away from
    // the beginning and then JSON parsing works. The Native Backend manages
    // encryption at the session level
    const data = JSON.parse(resp.data.substring(8));
    ToastAndroid.show(
      'Custom data with byte accuracy provisioned successfully',
      ToastAndroid.LONG,
    );
    console.log(data);
  })
  .catch((e) => {
    console.log(e && e.message ? e.message : 'error querying live data');
  });

};

return (
<View
style={{
justifyContent: 'space-around',
flex: 1,
padding: 10,
alignContent: 'center',
}}>

<Text style={{color: 'black', textAlign: 'center'}}>
Service UUID: {uuid}





<Text style={{color: 'white', textAlign: 'center'}}>Pop: {pop}





);
};

export default App;
Screenshot from 2023-05-19 14-02-28
Screenshot from 2023-05-19 14-03-54
`

@priya-kato
Copy link
Author

Error: Custom data provision failed

@pikann
Copy link
Collaborator

pikann commented May 27, 2023

Did you set the correct PoP?

@priya-kato
Copy link
Author

Did you set the correct PoP?

yes but when i call the customdata function after set pop but it returns undefind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants