在Android中解析BLE广告包

问题描述 投票:0回答:1

我正在尝试从连接到以公斤为单位的秤的 BLE 发射器读取数据,我能够读取数据,并且当我更改秤上的重量时数据会发生变化,但我无法提取以公斤为单位的重量

BLE B24 技术资料为: https://www.mantracourt.com/userfiles/documents/b24_telemetry_technical_manual.pdf

我尝试过在论坛上看到的几个解析器,但我什么也得不到......

活动代码是:

public class PruebaConexion18_12_2023Activity extends AppCompatActivity {

private BluetoothAdapter mBluetoothAdapter;
private int REQUEST_ENABLE_BT = 1;
private Handler mHandler;
private static final long SCAN_PERIOD = 10000;
private BluetoothLeScanner mLEScanner;
private ScanSettings settings;
private List<ScanFilter> filters;
private BluetoothGatt mGatt;
TextView bLTextView;
Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prueba_conexion18_12_2023);

    bLTextView = (TextView) findViewById(R.id.bLTextView);
    startButton = (Button) findViewById(R.id.startButton);
    startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startReceivingAdversitement();

        }
    });
    mHandler = new Handler();
}

private void startReceivingAdversitement() {
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    } else {
        mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
        settings = new ScanSettings.Builder()
          .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
          .build();
        filters = new ArrayList<ScanFilter>();
        scanLeDevice(true);
    }
}
private void scanLeDevice(final boolean enable) {
    if (enable) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {

                    mLEScanner.stopScan(mScanCallback);

            }
        }, SCAN_PERIOD);
        mLEScanner.startScan(filters, settings, mScanCallback);
    } else {
        mLEScanner.stopScan(mScanCallback);
    }
}

StringBuilder sBMapa = new StringBuilder();



private ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        Log.e("callbackType", "MAX " + String.valueOf(callbackType));
        Log.e("result", "MAX " + result.toString());
        BluetoothDevice btDevice = result.getDevice();
        if(null!=btDevice.getName()){
            if(btDevice.getName().equals("B24")){
                byte[] scanRecord = result.getScanRecord().getBytes();
                for(int i=0; i<scanRecord.length;i++){
                    sBMapa.append(scanRecord[i]);
                }
                sBMapa.append("\n");
                updateScreen();
                Log.e("ScanResult - Results", "MAX " + result.toString());
            }
        }
    }

    @Override
    public void onBatchScanResults(List<ScanResult> results) {
        for (ScanResult sr : results) {
            Log.e("ScanResult - Results", "MAX " + sr.toString());
        }
    }

    @Override
    public void onScanFailed(int errorCode) {
        Log.e("Scan Failed", "MAX " + "Error Code: " + errorCode);
    }
};



public void updateScreen(){
    try {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    bLTextView.setText(sBMapa.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

以及从设备接收到的数据:

21616-1-614110212010895-351467-120112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3514-34-76112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-15112124112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3515-3211112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-15-122-60112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-1548-8112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-351428-85112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-1565-29112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-15-110-20112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3515-74-84112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-15117-37112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-351510178112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3515-12103112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3515-105-122112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-35-1587-45112131039496650520000000000000000000000000000000000000
21616-1-614110212010895-3514-5646112131039496650520000000000000000000000000000000000000

感谢您的帮助!!

java android bluetooth bluetooth-lowenergy
1个回答
0
投票

        int n,len,getout;
        byte[] rec;
   
        rec = result.getScanRecord().getBytes();
        len = rec.length;
        n = 0;
        getout = 0;
        while(n < len-2 && rec[n] > 0 && getout == 0)
          {
          // rec[n] is length of item
          if(rec[n] > 1 && rec[n+1] == 0xFF)
            {  // manuf data
            // IEEE 4-byte float data is rec[n+9],rec[n+10],rec[n+11],rec[n+12]
            // Easy in C, but I don't know how to persuade Java that this
            // is a float type variable, but someone will
            getout = 1;
            }
          else
            n += rec[n] + 1;
          }
© www.soinside.com 2019 - 2024. All rights reserved.