0

I Need to connect to Xiaomi Smart Scale with Bluetooth in C#

Is this possible ?

Adriaan
  • 17,741
  • 7
  • 42
  • 75

2 Answers2

0

I see that some people on github already have made a few projects to access it (JavaScript, Android, iOS specifically), but I see nothing for C#.

So I'd say it's definitely possible. But maybe not that easy.

You could try to port this little JavaScript project to C# and see for yourself.

edit: some insights about finding the actual bluetooth device. It most likely will have to be paired to the client machine already.

Community
  • 1
  • 1
Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • thank you @Kilazur How should this javascript code be used? How do this JavaScript can be connect to Bluetooth? – Ali Tajmir Riahi Feb 08 '17 at 19:24
  • I can't say for sure, but for what I gather it simply lists every connected bluetooth devices with a module named [noble](https://github.com/sandeepmistry/noble), and read data for all of them. Only way to know for sure is to try it. – Kilazur Feb 08 '17 at 20:18
0

After several hours of searching I found this video I discovered MI_SCALE Bluetooth device

discovered MI_SCALE Bluetooth

with code 880f10a0dd92 and Command

node peripheral-explorer.js 880f10a0dd92

Base on video Connected smart scale and receive data :

    > peripheral with ID 880f10a0dd92 found
  Local Name        = MI_SCALE
  Manufacturer Data = 5701880f10a0dd92
  Service Data      = [object Object]
  Service UUIDs     = 181d
  services and characteristics:
  1800 (Generic Access)
  2a00 (Device Name)
    properties  read, write
    value       4d495f5343414c45 | 'MI_SCALE'
   2a01 (Appearance)
    properties  read
    value       800c | ' '
  2a02 (Peripheral Privacy Flag)
    properties  read, write
    value       00 | ' '
  2a04 (Peripheral Preferred Connection Parameters)
    properties  read
    value       6400c8000000d007 | 'd H   P'
  2a03 (Reconnection Address)
    properties  read, writeWithoutResponse, write
    value        | ''
  2a9d (Weight Measurement)
    properties  indicate
  00002a2f0000351221180009af100700
    properties  write, notify
  000015300000351221180009af100700
  000015310000351221180009af100700
    properties  write, notify
  000015320000351221180009af100700
    properties  writeWithoutResponse
  2a04 (Peripheral Preferred Connection Parameters)
    properties  read, write, notify
    value       0c000c000000c800 | '    H '
  000015420000351221180009af100700
    properties  read, write, notify
    value       0301 | ''
  000015430000351221180009af100700
    properties  read, write, notify
    value       0100 | ' '

but base this code :

'use strict';
const noble = require('noble');
let lastvalue = '';

noble.on('stateChange', state => {
  if (state === 'poweredOn') {
    noble.startScanning(['181d'], true);
  } else {
    noble.stopScanning();
  }
});

noble.on('discover', peripheral => {
  let serviceData = peripheral.advertisement.serviceData,
    unit = '',
    newval = false,
    measured;
  if (serviceData && serviceData.length) {
    for (let i in serviceData) {
      if (serviceData.hasOwnProperty(i)) {
        let data = serviceData[i].data.toString('hex');
        switch (data.substr(0, 2)) {
        case '03':
          unit = 'lbs';
          newval = true;
          break;
        case 'a3':
          unit = 'lbs';
          break;
        case '12':
          unit = 'jin';
          newval = true;
          break;
        case 'b2':
          unit = 'jin';
          break;
        case '22':
          unit = 'kg';
          newval = true;
          break;
        case 'a2':
          unit = 'kg';
          break;
        default:
          unit = '';
          break;
        }
        measured = parseInt(data.slice(4, 6) + data.slice(2, 4), 16) * 0.01;
        measured = unit === 'kg' ? measured / 2 : measured;
        if (unit) {
          if (lastvalue !== measured && !newval) {
            console.log('Last measured: ' + measured + ' ' + unit);
          } else if (newval && lastvalue !== measured) {
            console.log('New measure: ' + measured + ' ' + unit);
          }
          lastvalue = measured;
        }
      }
    }
  }
});

i worked with serviceData variable and finally i got weight

But My Questions is What is Simple Way to use This methods in C# ? if i use noble i should install vs and node.js and noble and Zadig on any Users system and is too Complicate

another Questions , in recived data for example

2a03 (Reconnection Address) properties read, writeWithoutResponse, write

What are these properties and how should them be used?