Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析从蓝牙HDP设备接收的原始数据_Java_Android_Bluetooth - Fatal编程技术网

Java 解析从蓝牙HDP设备接收的原始数据

Java 解析从蓝牙HDP设备接收的原始数据,java,android,bluetooth,Java,Android,Bluetooth,我正在尝试从欧姆龙708-BT血压监测仪读取数据,该监测仪经过continua健康联盟认证。我遵循这个例子。我正在设法连接到设备并检索字节格式的数据,但无法解析数据以获得所需的值。我正试图遵循本手册第19页规定的IEEE血压规范 (byte) 0x00, (byte) 0x08, //bt add for phone, can be automate in the future (byte) 0x3C, (byte) 0x5A, (byte) 0x37, (byte) 0xFF, (by

我正在尝试从欧姆龙708-BT血压监测仪读取数据,该监测仪经过continua健康联盟认证。我遵循这个例子。我正在设法连接到设备并检索字节格式的数据,但无法解析数据以获得所需的值。我正试图遵循本手册第19页规定的IEEE血压规范

(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,
下面是添加到上面链接的示例代码中的代码

//Variables in the class
private int count;
private byte[] invoke = {0x00, 0x00};
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());
        final 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
                Log.e("Test", data.toString()); 

                 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();
                         }
                         Log.i(TAG, "Seconds Write Thread");
                         count = 2;
                         (new WriteThread(mFd)).start();
                     }
                     else if (data[0] == (byte)0xE7){
                         Log.i(TAG, "E7");

                         count = 3; 
                         //set invoke id so get correct response
                         invoke = new byte[] { data[6], data[7] };
                         (new WriteThread(mFd)).start();     
                         //parse data!!
                         int number_of_data_packets = data[25];
                         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++){
                                Log.e("TEST", Integer.toString(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.e("RESULT", "sys is "+ sys);
                                   sendMessage(RECEIVED_SYS, sys);
                                   Log.e("RESULT", "dia is "+ dia);
                                   sendMessage(RECEIVED_DIA, dia);
                                   Log.e("RESULT", "map is "+ map);

                                   break;
                               case PULSE_DATA:
                                   //parse
                                   int pulse = byteToUnsignedInt(data[packet_start+5]);
                                   Log.e("RESULT", "pulse is " + pulse);
                                   sendMessage(RECEIVED_PUL, pulse);
                                   break;
                               case ERROR_CODE_DATA:
                                   //need more signal
                                   break;
                               }
                               packet_start += 1;//4 + data[packet_start+3];   //4 = ignore beginning four bytes
                           }  
                     }
                     else if (data[0] == (byte) 0xE4)
                     {

                         count = 4;
                         (new WriteThread(mFd)).start();
                     }
                     //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());
        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 Responded!");
            }  
            else if (count == 2)
            {
                fos.write(get_MDS);
                Log.i(TAG, "Get MDS object attributes!");
            }
            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) {}
    }
(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,
//类中的变量
私人整数计数;
私有字节[]invoke={0x00,0x00};
私有类ReadThread扩展线程{
私人包裹描述符mFd;
公共读取线程(ParcelFileDescriptor fd){
超级();
mFd=fd;
}
@凌驾
公开募捐{
FileInputStream fis=新的FileInputStream(mFd.getFileDescriptor());
最终字节数据[]=新字节[300];
试一试{
而(财务读取(数据)>-1){
//此时,应用程序可以将原始数据传递给
//已实现IEEE 11073-xxxxx规范。相反,此示例
//仅表示已接收到一些数据
Log.e(“Test”,data.toString());
如果(数据[0]!=(字节)0x00)
{
字符串测试=字节2hex(数据);
Log.i(标签、测试);
if(数据[0]==(字节)0xE2){
Log.i(标签“E2”);
计数=1;
(新的WriteThread(mFd)).start();
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
Log.i(标记“秒写入线程”);
计数=2;
(新的WriteThread(mFd)).start();
}
else if(数据[0]==(字节)0xE7){
Log.i(标签“E7”);
计数=3;
//设置调用id以便获得正确的响应
invoke=新字节[]{data[6],data[7]};
(新的WriteThread(mFd)).start();
//解析数据!!
数据包的整数=数据[25];
int packet_start=30;
最终int系统直径图数据=1;
最终int脉冲_数据=2;
最终整数错误代码数据=3;
对于(int i=0;i<数据包的数量;i++){
Log.e(“TEST”,Integer.toString(i));
int obj_handle=数据[数据包开始+1];
开关(obj_手柄)
{
案例系统直径图数据:
int sys=bytetunsignedint(数据[数据包开始+9]);
int dia=字节数(数据[数据包开始+11]);
int map=bytetunsignedint(数据[数据包开始+13]);
//创建团队字符串…9+13~9+20
Log.e(“结果”,“系统为”+sys);
sendMessage(已接收系统,系统);
Log.e(“结果”,“直径为”+直径);
发送消息(接收直径,直径);
Log.e(“结果”,“映射为”+映射);
打破
病例脉象数据:
//解析
int pulse=bytetunsignedint(数据[数据包开始+5]);
Log.e(“结果”,“脉冲为”+脉冲);
发送消息(接收脉冲,脉冲);
打破
案例错误\代码\数据:
//需要更多信号吗
打破
}
packet_start+=1;//4+数据[packet_start+3];//4=忽略开头的四个字节
}  
}
else if(数据[0]==(字节)0xE4)
{
计数=4;
(新的WriteThread(mFd)).start();
}
//把数据归零
对于(int i=0;i(byte) 0x00, (byte) 0x08,  //bt add for phone, can be automate in the future

(byte) 0x00, (byte) 0x22, (byte) 0x09, (byte) 0x22, 

(byte) 0x58, (byte) 0x35, (byte) 0xc3, (byte) 0x28,
final byte data_AR[] = new byte[] {         
             (byte) 0xE3, (byte) 0x00,
             (byte) 0x00, (byte) 0x2C, 
             (byte) 0x00, (byte) 0x03,
EUI 64 Bit [00:11:22:FF:FE:33:44:55] 
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
else if (data[18] == (byte) 0x0d && data[19] == (byte) 0x1f)  //mp var report
                        {
                                count = 999; 
                                //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[25];
                                int number_of_data_packets = data[29];
                                //packet_start starts from handle 0 byte
                                int packet_start = 36;
                                final int SYS_DIA_MAP_DATA = 1;
                                final int PULSE_DATA = 2;
                                final int STATUS = 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]);

                                        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);

                                        break;
                                    case PULSE_DATA:
                                        //parse
                                        int pulse = byteToUnsignedInt(data[packet_start+5]);
                                        Log.i(TAG, "pulse is " + pulse);
                                        sendMessage(RECEIVED_PUL, pulse);
                                        break;
                                    case STATUS:
                                        //need more signal
                                        Log.i(TAG, "STATUS");
                                        break;
                                    }
                                    packet_start += byteToUnsignedInt(data[packet_start+3]) + 4;   //4 = ignore beginning four bytes
                                }                               
                        }
public class BluetoothHDPService extends Service {
    private static final String TAG = "BluetoothHDPService";

    public static final int RESULT_OK = 0;
    public static final int RESULT_FAIL = -1;

    // Status codes sent back to the UI client.
    // Application registration complete.
    public static final int STATUS_HEALTH_APP_REG = 100;
    // Application unregistration complete.
    public static final int STATUS_HEALTH_APP_UNREG = 101;
    // Channel creation complete.
    public static final int STATUS_CREATE_CHANNEL = 102;
    // Channel destroy complete.
    public static final int STATUS_DESTROY_CHANNEL = 103;
    // Reading data from Bluetooth HDP device.
    public static final int STATUS_READ_DATA = 104;
    // Done with reading data.
    public static final int STATUS_READ_DATA_DONE = 105;

    // Message codes received from the UI client.
    // Register client with this service.
    public static final int MSG_REG_CLIENT = 200;
    // Unregister client from this service.
    public static final int MSG_UNREG_CLIENT = 201;
    // Register health application.
    public static final int MSG_REG_HEALTH_APP = 300;
    // Unregister health application.
    public static final int MSG_UNREG_HEALTH_APP = 301;
    // Connect channel.
    public static final int MSG_CONNECT_CHANNEL = 400;
    // Disconnect channel.
    public static final int MSG_DISCONNECT_CHANNEL = 401;

    public static final int RECEIVED_SYS = 901;

    public static final int RECEIVED_DIA = 902;

    public static final int RECEIVED_PUL = 903;

    private int count;
    private byte[] invoke = {0x00, 0x00};

    private BluetoothHealthAppConfiguration mHealthAppConfig;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothHealth mBluetoothHealth;
    private BluetoothDevice mDevice;
    private int mChannelId;

    private Messenger mClient;

    // Handles events sent by {@link HealthHDPActivity}.
    @SuppressLint("HandlerLeak")
    private class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // Register UI client to this service so the client can receive messages.
                case MSG_REG_CLIENT:
                    Log.d(TAG, "Activity client registered");
                    mClient = msg.replyTo;
                    break;
                // Unregister UI client from this service.
                case MSG_UNREG_CLIENT:
                    mClient = null;
                    break;
                // Register health application.
                case MSG_REG_HEALTH_APP:
                    registerApp(msg.arg1);
                    break;
                // Unregister health application.
                case MSG_UNREG_HEALTH_APP:
                    unregisterApp();
                    break;
                // Connect channel.
                case MSG_CONNECT_CHANNEL:
                    mDevice = (BluetoothDevice) msg.obj;
                    connectChannel();
                    break;
                // Disconnect channel.
                case MSG_DISCONNECT_CHANNEL:
                    mDevice = (BluetoothDevice) msg.obj;
                    disconnectChannel();
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    }

    final Messenger mMessenger = new Messenger(new IncomingHandler());

    /**
     * Make sure Bluetooth and health profile are available on the Android device.  Stop service
     * if they are not available.
     */
    @SuppressLint("ShowToast")
    @Override
    public void onCreate() {
        super.onCreate();

        Log.e("TEST", "HDPService Created");

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            // Bluetooth adapter isn't available.  The client of the service is supposed to
            // verify that it is available and activate before invoking this service.
            stopSelf();
            return;
        }
        if (!mBluetoothAdapter.getProfileProxy(this, mBluetoothServiceListener,
                BluetoothProfile.HEALTH)) {
            Toast.makeText(this, "bluetooth_health_profile_not_available", 
                    Toast.LENGTH_LONG);
            stopSelf();
            return;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "BluetoothHDPService is running.");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    };

    // Register health application through the Bluetooth Health API.
    private void registerApp(int dataType) {
        Log.e(TAG, "registerApp()");
        mBluetoothHealth.registerSinkAppConfiguration(TAG, dataType, mHealthCallback);
    }

    // Unregister health application through the Bluetooth Health API.
    private void unregisterApp() {
        Log.e(TAG, "unregisterApp()");
        mBluetoothHealth.unregisterAppConfiguration(mHealthAppConfig);
    }

    // Connect channel through the Bluetooth Health API.
    private void connectChannel() {
        Log.i(TAG, "connectChannel()");
        mBluetoothHealth.connectChannelToSource(mDevice, mHealthAppConfig);
    }

    // Disconnect channel through the Bluetooth Health API.
    private void disconnectChannel() {
        Log.i(TAG, "disconnectChannel()");
        mBluetoothHealth.disconnectChannel(mDevice, mHealthAppConfig, mChannelId);
    }

    // Callbacks to handle connection set up and disconnection clean up.
    private final BluetoothProfile.ServiceListener mBluetoothServiceListener =
            new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.HEALTH) {
                mBluetoothHealth = (BluetoothHealth) proxy;
                if (Log.isLoggable(TAG, Log.DEBUG))
                    Log.d(TAG, "onServiceConnected to profile: " + profile);
            }
        }

        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.HEALTH) {
                mBluetoothHealth = null;
            }
        }
    };

    private final BluetoothHealthCallback mHealthCallback = new BluetoothHealthCallback() {
        // Callback to handle application registration and unregistration events.  The service
        // passes the status back to the UI client.
        public void onHealthAppConfigurationStatusChange(BluetoothHealthAppConfiguration config,
                int status) {
            if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_FAILURE) {
                mHealthAppConfig = null;
                sendMessage(STATUS_HEALTH_APP_REG, RESULT_FAIL);
            } else if (status == BluetoothHealth.APP_CONFIG_REGISTRATION_SUCCESS) {
                mHealthAppConfig = config;
                sendMessage(STATUS_HEALTH_APP_REG, RESULT_OK);
            } else if (status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_FAILURE ||
                    status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS) {
                sendMessage(STATUS_HEALTH_APP_UNREG,
                        status == BluetoothHealth.APP_CONFIG_UNREGISTRATION_SUCCESS ?
                        RESULT_OK : RESULT_FAIL);
            }
        }

        // Callback to handle channel connection state changes.
        // Note that the logic of the state machine may need to be modified based on the HDP device.
        // When the HDP device is connected, the received file descriptor is passed to the
        // ReadThread to read the content.
        public void onHealthChannelStateChange(BluetoothHealthAppConfiguration config,
                BluetoothDevice device, int prevState, int newState, ParcelFileDescriptor fd,
                int channelId) {

            Log.e("Read Thread", "Start If Statements");
            Log.e("Read Thread", "prevState: " + prevState);
            Log.e("Read Thread", "newState: " + newState);

            if (Log.isLoggable(TAG, Log.DEBUG))
                Log.d(TAG, String.format("prevState\t%d ----------> newState\t%d",
                        prevState, newState));

//            if (prevState != BluetoothHealth.STATE_CHANNEL_CONNECTED &&
//                    newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {

            if (prevState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED &&
                    newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
                if (config.equals(mHealthAppConfig)) {
                    mChannelId = channelId;
                    sendMessage(STATUS_CREATE_CHANNEL, RESULT_OK);
                    Log.e("Read Thread", "Read  Start 1");
                    (new ReadThread(fd)).start();
                } else {
                    Log.e("Read Thread", "Status Create Channel Fail 1");
                    sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
                }
            } else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING &&
                       newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
                sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
                Log.e("Read Thread", "Status Create Channel Fail 2");
            } else if (newState == BluetoothHealth.STATE_CHANNEL_DISCONNECTED) {
                if (config.equals(mHealthAppConfig)) {
                    sendMessage(STATUS_DESTROY_CHANNEL, RESULT_OK);
                    Log.e("Read Thread", "Status Disconnect OK");
                } else {
                    sendMessage(STATUS_DESTROY_CHANNEL, RESULT_FAIL);
                    Log.e("Read Thread", "Status Disconnect FAIL");
                }
            }else if (prevState == BluetoothHealth.STATE_CHANNEL_CONNECTING && newState == BluetoothHealth.STATE_CHANNEL_CONNECTED) {
                if (config.equals(mHealthAppConfig)) {
                    mChannelId = channelId;
                    sendMessage(STATUS_CREATE_CHANNEL, RESULT_OK);
                    Log.e("Read Thread", "Read  Start 2");
                    (new ReadThread(fd)).start();
                } else {
                    sendMessage(STATUS_CREATE_CHANNEL, RESULT_FAIL);
                    Log.e("Read Thread", "Status Create Channel Fail 3");
                }
            }
        }
    };

    // Sends an update message to registered UI client.
    private void sendMessage(int what, int value) {
        if (mClient == null) {
            Log.d(TAG, "No clients registered.");
            return;
        }

        try {
            mClient.send(Message.obtain(null, what, value, 0));
        } catch (RemoteException e) {
            // Unable to reach client.
            e.printStackTrace();
        }
    }

    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;
    }

    public static int toInt(byte[] bytes) {
          int ret = 0;
          for (int i=0; i<4 && i<bytes.length; i++) {
            ret <<= 8;
            ret |= (int)bytes[i] & 0xFF;
          }
          return ret;
    }

    // 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() {
            Log.e("TEST", "Read Data 1");
            FileInputStream fis = new FileInputStream(mFd.getFileDescriptor());
            final byte data[] = new byte[200];
            Log.i(TAG, "Read Data 2");
            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.

                    Log.i(TAG, "INBOUND");
                    String test = byte2hex(data);
                    Log.i(TAG, test);

                     if (data[0] != (byte) 0x00)
                     {

                         if(data[0] == (byte) 0xE2){
                             Log.i(TAG, "E2 - Association Request");
                             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 - Data Given");


                             if(data[3] != (byte)0xda){

                                 invoke[0] = data[6];
                                 invoke[1] = data[7];

                                    Log.i(TAG, "E7 - Reading?");
                                    ByteBuffer sys = ByteBuffer.allocate(2);
                                     sys.order(ByteOrder.LITTLE_ENDIAN);
                                     sys.put(data[45]);
                                     sys.put(data[46]);
                                     short sysVal = sys.getShort(0);
                                     Log.i(TAG, " Sys - "+sysVal);

                                    ByteBuffer dia = ByteBuffer.allocate(2);
                                     dia.order(ByteOrder.LITTLE_ENDIAN);
                                     dia.put(data[47]);
                                     dia.put(data[48]);
                                     short diaVal = dia.getShort(0);
                                     Log.i(TAG, " Dia - "+diaVal);

                                     sendMessage(9919, diaVal);
                                     sendMessage(9920, sysVal);



                                   for(int i=0; i<(data.length-2); i++){
                                     ByteBuffer bb = ByteBuffer.allocate(2);
                                     bb.order(ByteOrder.LITTLE_ENDIAN);
                                     bb.put(data[i]);
                                     bb.put(data[i+1]);
                                     short shortVal = bb.getShort(0);
                                     Log.i(TAG, i+" Short Val - "+shortVal);

                                 }

                                   count = 3; 
                                   //set invoke id so get correct response 
                                   (new WriteThread(mFd)).start();
                             }
                             //parse data!!
                         }
                         else if (data[0] == (byte) 0xE4)
                         {
                             //count = 4;
                            // (new WriteThread(mFd)).start();
                         }
                         //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());
            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) 0x00, (byte) 0x24,
                                                        (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 Responded!");
                }  
                else if (count == 2)
                {
                    fos.write(get_MDS);
                    Log.i(TAG, "Get MDS object attributes!");
                }
                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) {}
        }

    }
}