Skip to content

Instantly share code, notes, and snippets.

@cwhittl
Created March 14, 2019 12:39
Show Gist options
  • Save cwhittl/2bc19de82df28f2b415b8cd77180d2f4 to your computer and use it in GitHub Desktop.
Save cwhittl/2bc19de82df28f2b415b8cd77180d2f4 to your computer and use it in GitHub Desktop.
Bleno Gist
import bleno from 'bleno-mac';
class MessageCharacteristic extends bleno.Characteristic {
constructor(config) {
const props = Object.assign(config, {
uuid: '13333333-3333-3333-3333-800000000001',
properties: ['read', 'write', 'notify'],
value: null,
});
super(props);
}
}
export default MessageCharacteristic;
import blenoMac from 'bleno-mac';
import MessageCharacteristic from './characteristics/message';
const messageStartDelimeter = '<m$g>';
const messageEndDelimeter = '</m$g>';
function chunkData(str, size) {
const chunks = new Array(Math.ceil(str.length / size));
let newo = 0;
for (let i = 0, o = 0; i < chunks.length; i += 1, o = newo) {
newo += size;
chunks[i] = str.substr(o, size);
}
return chunks;
}
function wait(ms) {
const waitTill = new Date(new Date().getTime() + ms);
while (waitTill > new Date()) { /* Don nothing */ }
}
function sendData(data, callback, maxValueSize) {
const totalMessage = `${messageStartDelimeter}${data}${messageEndDelimeter}`;
const chunks = chunkData(totalMessage, maxValueSize);
console.log(chunks.length);
chunks.forEach((chunk) => {
const dataAsBuffer = Buffer.from(chunk, 'utf-8');
console.log(chunk);
callback(dataAsBuffer);
wait(10); // Comment this out to fail
});
}
class TestService extends blenoMac.PrimaryService {
constructor() {
const message = new MessageCharacteristic(
{
onWriteRequest: (data, offset, withoutResponse, callback) => {
console.log(`MessageCharacteristic - onWriteRequest: value = ${data.toString('utf8')}`);
callback(MessageCharacteristic.RESULT_SUCCESS, data);
},
onSubscribe: (maxValueSize, updateValueCallback) => {
sendData('Really Long Text That Needs Chunking due to OSX max valuesize', updateValueCallback, maxValueSize);
},
},
);
const props = {
uuid: '13333333-3333-3333-3333-800000000000',
characteristics: [
message,
],
};
super(props);
}
}
export default TestService;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment