index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var bleno = require('bleno');
  2. var StringDecoder = require('string_decoder').StringDecoder;
  3. var name = 'wifitest';
  4. var serviceUuids = ['fffffffffffffffffffffffffffffff0']
  5. function bytesToString(buffer) {
  6. return String.fromCharCode.apply(null, new Uint8Array(buffer));
  7. }
  8. bleno.on('stateChange', function (state) {
  9. console.log(state);
  10. bleno.startAdvertising(name, serviceUuids, function (error) {
  11. if (error) {
  12. console.log(error);
  13. }
  14. console.log('start');
  15. var Descriptor = bleno.Descriptor;
  16. var descriptor = new Descriptor({
  17. uuid: '2901',
  18. value: 'test' // static value, must be of type Buffer or string if set
  19. });
  20. var Characteristic = bleno.Characteristic;
  21. var characteristic = new Characteristic({
  22. uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit
  23. properties: ['read', 'write', 'writeWithoutResponse'], // can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate'
  24. value: null, // optional static value, must be of type Buffer - for read only characteristics
  25. descriptors: [
  26. // see Descriptor for data type
  27. descriptor
  28. ],
  29. onWriteRequest: function (data, offset, withoutResponse) {
  30. console.log(bytesToString(data));
  31. }
  32. });
  33. var PrimaryService = bleno.PrimaryService;
  34. var primaryService = new PrimaryService({
  35. uuid: 'fffffffffffffffffffffffffffffff0', // or 'fff0' for 16-bit
  36. characteristics: [
  37. // see Characteristic for data type
  38. characteristic
  39. ]
  40. });
  41. bleno.setServices(primaryService, function (error) {
  42. if (error) {
  43. console.log(error);
  44. }
  45. console.log('success');
  46. });
  47. });
  48. });