Parsing data from hdp device for android (API 15)

2019-02-07 15:43发布

问题:

I am using the HDP Sample and I have a Blood pressure sensor (http://www.andonline.com/medical/products/details.php?catname=&product_num=UA-767PBT-C). I get the data from it, but I don't know how to parse it into something human readable.

this is what I tried until now with something like:

while(fis.read(data) > -1) {
    String value = null;
    BigInteger bi = new BigInteger(data);
//  for(byte b:data){
//  value+=b;
//  }

Log.d("read data binary", "the data "+bi.toString(2));

Log.d("read data decimal", "the data "+bi.toString());

Log.d("read data hexa", "the data "+bi.toString(16));

Log.d("read data pure", "the data "+data);

//  Log.d("read data in for", "the data "+value);

and I get something like:

05-01 20:17:56.208: D/read data hexa(4760): the data -1dffffcd7ffffffffffeffd5af86ffd97fffffff7fff7fffffffffffffffff7ffffffff7fff6e000017fe6c9fd43ff7eff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 05-01 20:17:56.208: D/read data pure(4760): the data [B@40d67e70 05-01 20:18:04.626: D/read data binary(4760): the data -110011111111111111111111111011111111111111101111111111111111111111111111111101111111111010101101011111000011011111111110110010111111111111111111111111111111101111111111111110111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111110111111111111111011011100000000000000000000101111111111001101100100111111101010000111111111101111110111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 05-01 20:18:06.828: D/read data decimal(4760): the data -20348350762396478161021871983386134046091893532090484905482699697369221290028547619764749977278680798469626704742235140694316240671429692337864639243870481836286701918984999353010323436511957773854417643317736462406886239299290151928444851447368820640400582675047865805950839848718049994903510826857318701138459976961556738865419685922528736334952434327140487856991057603825697448506980403050739618728888014246961589865799459896803298946835870198570716361891575292960074540686096107945256835771519689457256438534741757561147502739243069487804900407256776121853933063931528157203936670438328026195112818597708193775903006221916266459367440106091138592610480197614172510663306017827787443009273181995256805591067940005616029143719472574149887742279217792786608780484474880528749204990959138191004938891000063711358787824040223132479972416759584642866382108541120576160767677597715480386547121881849032192752633260581611407011853157254121988717573734877969134052424768517755583145720631774483604063081880432610973919518566690840732483917669693281934080872472643538204409333772095149193754543981303816862226602900932185731291218078343359909141826950522259981065979565825353771918309503909195400250270592097346140180255661323498680525840321588156171474185334819513148415443728234379507600359229093281074473243790684599034208199088683338454677621174483491191074351971358537028690315653099028602232230871902656137643830877283946170700882775921345249124458496887893344541537861170446155618915889154457316279931810886758741778466015814266662502325019252008275552963949417505527790579950678630694847351385927023263169674418774269260054062018597523133414318281716321106676019125971120320369677915824373299981376100493615932096583905733998938604880344209979507207389461881566668730566105933697505053128626488714804178563386957441767127359946655045064697358061551371027355282580217366653953689149217957268335824794738057555973823505958847701190906572003379211131572099550194123133402637104389561589064917255194334844269183378807442410026970212304915398750162342030862720562356574960395088223916289512342191699176716017309140213638536098383639091011729776310430620082118888497862622038695401701710387402100087840950940846152066514765108855402410952099725385450736354603210781118974198215554193785785859382435002755250153009251411946038850315108576684984106702193657963347897709677566091677495576556681656068507314031372074156531214767021895212919495909358260588291818568553609578646758938365326563499166628415041706131521582622947791012917744251071555344476280175567553030365868879087958701356749275597544555441192209535828193783528540378443653793007478915664994969246893177676787034473169357958664807853394146528881072611495728346161885141354339050534315795112871110590890270640490572109200167490678091854953195502952814139817930997734428369811037792170901222000605941722889749066363212792188473959033320542149557873698406208807103283915464017738102761293835485103233871320167686509199843523640244281618906825238870441363628235844748462133103276301174838663071551726880398794656562034968579187231564731597466312085292931401626425031219367372880105669524021703795487930074660269606852850348561943174481059683047939007210225601490478637988520094254176801854728550931572581072192577323801255123376568421142859931699183781795327546624931509936963718203679145081654772615676143950491156457709879833213770861474166550002893895004539378141404484514321501254425186584306317322820119080073521246583023928855680771674834459485547140458179583411000552946721557465941584607592684759973667328534420531200360125049393165249939125462273655943012452796198719607891446709638525939136041376672224464954765768003499209670257743095489459555485012164498854972198547405433983837840151414071849172134515511345320372998343869719318698995124097830586163312565522099108379088383907029795001179557033732925854106484925992046251994418246510444641426185467437327268959477603463411587255087111960727666751094267733565670470254329185066819049347190875320805571892878980923324446702125088753

Anyone has any ideea how to parse it, please?

PS: Sorry for putting everything from the logcat, but maybe someone finds any connection

回答1:

I found the solution. I needed the exact number of bytes that my sensor was sending + the way my sensor is sending the information.

I will put the part of the class that parse the data for my blood pressure monitor.

 public String byte2hex(byte[] b)
    {
     // String Buffer can be used instead
       String hs = "";
       String stmp = "";

       for (int n = 0; n < b.length; n++)
       {
          stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));

          if (stmp.length() == 1)
          {
             hs = hs + "0" + stmp;
          }
          else
          {
             hs = hs + stmp;
          }

          if (n < b.length - 1)
          {
             hs = hs + "";
          }
       }

       return hs;
    }

    public static int byteToUnsignedInt(byte b) {
        return 0x00 << 24 | b & 0xff;
      }

    // Thread to read incoming data received from the HDP device.  This sample application merely
    // reads the raw byte from the incoming file descriptor.  The data should be interpreted using
    // a health manager which implements the IEEE 11073-xxxxx specifications.
    private class ReadThread extends Thread {
        private ParcelFileDescriptor mFd;

        public ReadThread(ParcelFileDescriptor fd) {
            super();
            mFd = fd;
        }


        @Override
        public void run() {
            FileInputStream fis = new FileInputStream(mFd.getFileDescriptor());
            byte data[] = new byte[300];
            try {
                while(fis.read(data) > -1) {
                    // At this point, the application can pass the raw data to a parser that
                    // has implemented the IEEE 11073-xxxxx specifications.  Instead, this sample
                    // simply indicates that some data has been received.                   
                    if (data[0] != (byte) 0x00)
                    {
                        String test = byte2hex(data);
                        Log.i(TAG, test);
                        if(data[0] == (byte) 0xE2){
                            Log.i(TAG, "E2");
                            count = 1;
                            (new WriteThread(mFd)).start();
                            try {
                                sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            count = 2;
                            (new WriteThread(mFd)).start();
                        }
                        else if (data[0] == (byte)0xE7){
                            Log.i(TAG, "E7");

                            //work for legacy device...
                            if (data[18] == (byte) 0x0d && data[19] == (byte) 0x1d)  //fixed report
                            {
                                count = 3; 
                                //set invoke id so get correct response
                                invoke = new byte[] { data[6], data[7] };
                                //write back response
                                (new WriteThread(mFd)).start();     
                                //parse data!!
                                int length = data[21];
                                Log.i(TAG, "length is " + length);
                                // check data-req-id 
                                int report_no = data[22+3];
                                int number_of_data_packets = data[22+5];
                                //packet_start starts from handle 0 byte
                                int packet_start = 30;
                                final int SYS_DIA_MAP_DATA = 1;
                                final int PULSE_DATA = 2;
                                final int ERROR_CODE_DATA = 3;
                                for (int i = 0; i < number_of_data_packets; i++)
                                {
                                    int obj_handle = data[packet_start+1];
                                    switch (obj_handle)
                                    {
                                    case SYS_DIA_MAP_DATA:
                                        int sys = byteToUnsignedInt(data[packet_start+9]);
                                        int dia = byteToUnsignedInt(data[packet_start+11]);
                                        int map = byteToUnsignedInt(data[packet_start+13]);
                                        //create team string... 9+13~9+20   
                                        Log.i(TAG, "sys is "+ sys);
                                        sendMessage(RECEIVED_SYS, sys);
                                        Log.i(TAG, "dia is "+ dia);
                                        sendMessage(RECEIVED_DIA, dia);
                                        Log.i(TAG, "map is "+ map);
                                        //test
//                                      sendMessage(RECEIVED_MAP, map);
                                        break;
                                    case PULSE_DATA:
                                        //parse
                                        int pulse = byteToUnsignedInt(data[packet_start+5]);
                                        Log.i(TAG, "pulse is " + pulse);
                                        sendMessage(RECEIVED_PUL, pulse);
                                        break;
                                    case ERROR_CODE_DATA:
                                        //need more signal
                                        break;
                                    }
                                    packet_start += 4 + data[packet_start+3];   //4 = ignore beginning four bytes
                                }                               
                            }
                            else
                            {
                                count = 2;
                            }
                        }
                        else if (data[0] == (byte) 0xE4)
                        {
                            count = 4;
                            (new WriteThread(mFd)).start();
//                          sendMessage();
                        }
                        //zero out the data
                        for (int i = 0; i < data.length; i++){
                            data[i] = (byte) 0x00;
                        }
                    }
                    sendMessage(STATUS_READ_DATA, 0);
                }
            } catch(IOException ioe) {}
            if (mFd != null) {
                try {
                    mFd.close();
                } catch (IOException e) { /* Do nothing. */ }
            }   
            sendMessage(STATUS_READ_DATA_DONE, 0);
        }
    }


    private class WriteThread extends Thread {
        private ParcelFileDescriptor mFd;

        public WriteThread(ParcelFileDescriptor fd) {
            super();
            mFd = fd;
        }

        @Override
        public void run() {
            FileOutputStream fos = new FileOutputStream(mFd.getFileDescriptor());
//            FileInputStream fis = new FileInputStream(mFd.getFileDescriptor());
            final byte data_AR[] = new byte[] {         (byte) 0xE3, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x2C, 
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x50, (byte) 0x79,
                                                        (byte) 0x00, (byte) 0x26,
                                                        (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x80, (byte) 0x00,
                                                        (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x08,  //bt add for phone, can be automate in the future
                                                        (byte) 0x3C, (byte) 0x5A, (byte) 0x37, (byte) 0xFF, 
                                                        (byte) 0xFE, (byte) 0x95, (byte) 0xEE, (byte) 0xE3,
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00, 
                                                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
            final byte data_DR[] = new byte[] {         (byte) 0xE7, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x12,
                                                        (byte) 0x00, (byte) 0x10,
                                                        (byte) invoke[0], (byte) invoke[1],
                                                        (byte) 0x02, (byte) 0x01,
                                                        (byte) 0x00, (byte) 0x0A,
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x0D, (byte) 0x1D,
                                                        (byte) 0x00, (byte) 0x00 };

            final byte get_MDS[] = new byte[] {         (byte) 0xE7, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x0E,
                                                        (byte) 0x00, (byte) 0x0C,
                                                        (byte) 0x00, (byte) 0x24,
                                                        (byte) 0x01, (byte) 0x03,
                                                        (byte) 0x00, (byte) 0x06,
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x00 };

            final byte data_RR[] = new byte[] {         (byte) 0xE5, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x02,
                                                        (byte) 0x00, (byte) 0x00 };

            final byte data_RRQ[] = new byte[] {        (byte) 0xE4, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x02,
                                                        (byte) 0x00, (byte) 0x00 };

            final byte data_ABORT[] = new byte[] {      (byte) 0xE6, (byte) 0x00,
                                                        (byte) 0x00, (byte) 0x02,
                                                        (byte) 0x00, (byte) 0x00 };
            try {
                Log.i(TAG, String.valueOf(count));
                if (count == 1)
                {
                    fos.write(data_AR);
                    Log.i(TAG, "Association Responsed!");
                }  
                else if (count == 2)
                {
                    fos.write(get_MDS);
                    Log.i(TAG, "Get MDS object attributes!");
//                  fos.write(data_ABORT);
                }
                else if (count == 3) 
                {
                    fos.write(data_DR);
                    Log.i(TAG, "Data Responsed!");
                }
                else if (count == 4)
                {
                    fos.write(data_RR);
                    Log.i(TAG, "Data Released!");
                }
            } catch(IOException ioe) {}
        }
    }

In order to understand this code you have to read the IEEE 11073 specifications also. Every kind of sensor has his own number of bytes.

I didn't do this code, someone gave it to me. If he asks me to remove it, I will :)