Java 我需要有关成功连接到带有galaxy s3的zephyr HXM蓝牙设备的建议

Java 我需要有关成功连接到带有galaxy s3的zephyr HXM蓝牙设备的建议,java,android,sockets,bluetooth,health-monitoring,Java,Android,Sockets,Bluetooth,Health Monitoring,我正在使用Android的HDPSample应用程序。我正在尝试在我的三星Galaxy S3 ANDROID V17和Zephyr HXM蓝牙心率监视器之间配置蓝牙连接。 我已成功配对,并且正在成功写入日志文件。 但是,我希望使用serversockets进行连接,因此我将Android示例中给出的两个类组合在一起。 我想将“作为客户端连接”的代码与ConnectThread类结合起来 然后使用ConnectedThread类管理连接。 将这两个类合并是一个糟糕的策略吗?每个类所完成的线程是重要

我正在使用Android的HDPSample应用程序。我正在尝试在我的三星Galaxy S3 ANDROID V17和Zephyr HXM蓝牙心率监视器之间配置蓝牙连接。 我已成功配对,并且正在成功写入日志文件。 但是,我希望使用serversockets进行连接,因此我将Android示例中给出的两个类组合在一起。 我想将“作为客户端连接”的代码与ConnectThread类结合起来 然后使用ConnectedThread类管理连接。 将这两个类合并是一个糟糕的策略吗?每个类所完成的线程是重要的还是仅仅是Androids显示两个不同动作的方式来进行澄清。 这确实是我的问题。我当前在运行此操作时遇到异常。 非常感谢。这是我的密码

    package com.example.bluetooth.health;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;

    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.UUID;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothServerSocket;
    import android.bluetooth.BluetoothSocket;
    import android.content.BroadcastReceiver;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.ServiceConnection;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Message;
    import android.os.Messenger;
    import android.os.RemoteException;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;



    /**
     * Main user interface for the Sample application.  All Bluetooth health-related
     * operations happen in {@link BluetoothHDPService}.  This activity passes messages to and from
     * the service.
     */
    public class BluetoothHDPActivity extends Activity {
        private static final String TAG = "BluetoothHealthActivity";

        // Use the appropriate IEEE 11073 data types based on the devices used.
        // Below are some examples.  Refer to relevant Bluetooth HDP specifications for detail.
        //     0x1007 - blood pressure meter
        //     0x1008 - body thermometer
        //     0x100F - body weight scale
        private static final int HEALTH_PROFILE_SOURCE_DATA_TYPE = 0x1007;

        private static final int REQUEST_ENABLE_BT = 1;
        //private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        private static final UUID MY_UUID = UUID.fromString("HXM020735");

        public static final int MESSAGE_READ = 0;
        private TextView mConnectIndicator;
        private ImageView mDataIndicator;
        private TextView mStatusMessage;

        private BluetoothAdapter mBluetoothAdapter;
        private BluetoothDevice[] mAllBondedDevices;
        private BluetoothDevice mDevice;
        //jt
        private BluetoothServerSocket mServerSocket;
        //jt
        private int mDeviceIndex = 0;
        private Resources mRes;
        private Messenger mHealthService;
        private boolean mHealthServiceBound;

        private class ConnectThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final BluetoothDevice mmDevice;

            /**/
            private final InputStream mmInStream = null;
            private final OutputStream mmOutStream = null;
            private Handler mHandler;
            /**/

            public ConnectThread(BluetoothDevice device) {
                // Use a temporary object that is later assigned to mmSocket,
                // because mmSocket is final

                BluetoothSocket tmp = null;
                mmDevice = device;

                // Get a BluetoothSocket to connect with the given BluetoothDevice
                try {
                    // MY_UUID is the app's UUID string, also used by the server code
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) { }
                mmSocket = tmp;

                InputStream tmpIn = null;
                OutputStream tmpOut = null;

            }

            public void ConnectedThread() {
                //mmSocket = socket;
                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the input and output streams, using temp objects because
                // member streams are final
                try {
                    tmpIn = mmSocket.getInputStream();
                    tmpOut = mmSocket.getOutputStream();
                } catch (IOException e) { }


            }

            public void run() {
                // Cancel discovery because it will slow down the connection
                mBluetoothAdapter.cancelDiscovery();

                try {
                    // Connect the device through the socket. This will block
                    // until it succeeds or throws an exception
                    mmSocket.connect();
                } catch (IOException connectException) {
                    // Unable to connect; close the socket and get out
                    try {
                        mmSocket.close();
                    } catch (IOException closeException) { }
                    return;
                }

                // Do work to manage the connection (in a separate thread)
                //manageConnectedSocket(mmSocket);

                /***/
                byte[] buffer = new byte[1024];  // buffer store for the stream
                int bytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs
                while (true) {
                    try {
                        // Read from the InputStream
                        bytes = mmInStream.read(buffer);
                        // Send the obtained bytes to the UI activity
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)       .sendToTarget();

                    } catch (IOException e) {
                        break;
                    }
                }
                /***/

            }

            /** Will cancel an in-progress connection, and close the socket */
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) { }
            }
        };

      /*  private class ConnectedThread extends Thread {
            private final BluetoothSocket mmSocket;
            private final InputStream mmInStream;
            private final OutputStream mmOutStream;
            private Handler mHandler;

            public ConnectedThread(BluetoothSocket socket) {
                mmSocket = socket;
                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the input and output streams, using temp objects because
                // member streams are final
                try {
                    tmpIn = socket.getInputStream();
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) { }

                mmInStream = tmpIn;
                mmOutStream = tmpOut;
            }

            public void run() {
                byte[] buffer = new byte[1024];  // buffer store for the stream
                int bytes; // bytes returned from read()

                // Keep listening to the InputStream until an exception occurs
                while (true) {
                    try {
                        // Read from the InputStream
                        bytes = mmInStream.read(buffer);
                        // Send the obtained bytes to the UI activity
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)       .sendToTarget();

                    } catch (IOException e) {
                        break;
                    }
                }
            }
         */
            /* Call this from the main activity to send data to the remote device */
         /*   public void write(byte[] bytes) {
                try {
                    mmOutStream.write(bytes);
                } catch (IOException e) { }
            }
         */
            /* Call this from the main activity to shutdown the connection */
           /* public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) { }
            }
        };

        */

        //Added by Jason to append data to a file on the android phone
        public void appendLog(String text)
        {       
           File logFile = new File("/storage/sdcard0/log.txt");
           if (!logFile.exists())
           {
              try
              {
                 logFile.createNewFile();
              } 
              catch (IOException e)
              {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
              }
           }
           try
           {
              //BufferedWriter for performance, true to set append to file flag
              BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
              buf.append(text);
              buf.newLine();
              buf.close();
           }
           catch (IOException e)
           {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }

        }


        // Handles events sent by {@link HealthHDPService}.
        private Handler mIncomingHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    // Application registration complete.
                    case BluetoothHDPService.STATUS_HEALTH_APP_REG:
                        mStatusMessage.setText(
                                String.format(mRes.getString(R.string.status_reg),
                                msg.arg1));
                        break;
                    // Application unregistration complete.
                    case BluetoothHDPService.STATUS_HEALTH_APP_UNREG:
                        mStatusMessage.setText(
                                String.format(mRes.getString(R.string.status_unreg),
                                msg.arg1));
                        break;
                    // Reading data from HDP device.
                    case BluetoothHDPService.STATUS_READ_DATA:
                        mStatusMessage.setText(mRes.getString(R.string.read_data));
                        //get data from here and write to a file



                        mDataIndicator.setImageLevel(1);
                        break;
                    // Finish reading data from HDP device.
                    case BluetoothHDPService.STATUS_READ_DATA_DONE:
                        mStatusMessage.setText(mRes.getString(R.string.read_data_done));
                        mDataIndicator.setImageLevel(0);
                        break;
                    // Channel creation complete.  Some devices will automatically establish
                    // connection.
                    case BluetoothHDPService.STATUS_CREATE_CHANNEL:
                        mStatusMessage.setText(
                                String.format(mRes.getString(R.string.status_create_channel),
                                msg.arg1));
                        mConnectIndicator.setText(R.string.connected);
                        break;
                    // Channel destroy complete.  This happens when either the device disconnects or
                    // there is extended inactivity.
                    case BluetoothHDPService.STATUS_DESTROY_CHANNEL:
                        mStatusMessage.setText(
                                String.format(mRes.getString(R.string.status_destroy_channel),
                                msg.arg1));
                        mConnectIndicator.setText(R.string.disconnected);
                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };

        private final Messenger mMessenger = new Messenger(mIncomingHandler);

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Check for Bluetooth availability on the Android platform.
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                Toast.makeText(this, R.string.bluetooth_not_available, Toast.LENGTH_LONG);
                finish();
                return;
            }
            setContentView(R.layout.console);
            mConnectIndicator = (TextView) findViewById(R.id.connect_ind);
            mStatusMessage = (TextView) findViewById(R.id.status_msg);
            mDataIndicator = (ImageView) findViewById(R.id.data_ind);
            mRes = getResources();
            mHealthServiceBound = false;

            // Initiates application registration through {@link BluetoothHDPService}.
            Button registerAppButton = (Button) findViewById(R.id.button_register_app);
            registerAppButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    sendMessage(BluetoothHDPService.MSG_REG_HEALTH_APP,
                            HEALTH_PROFILE_SOURCE_DATA_TYPE);
                }
            });

            // Initiates application unregistration through {@link BluetoothHDPService}.
            Button unregisterAppButton = (Button) findViewById(R.id.button_unregister_app);
            unregisterAppButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    sendMessage(BluetoothHDPService.MSG_UNREG_HEALTH_APP, 0);
                }
            });

            // Initiates channel creation through {@link BluetoothHDPService}.  Some devices will
            // initiate the channel connection, in which case, it is not necessary to do this in the
            // application.  When pressed, the user is asked to select from one of the bonded devices
            // to connect to.
            Button connectButton = (Button) findViewById(R.id.button_connect_channel);
            connectButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mAllBondedDevices =
                            (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices().toArray(
                                    new BluetoothDevice[0]);

                    if (mAllBondedDevices.length > 0) {
                        int deviceCount = mAllBondedDevices.length;
                        appendLog("device count = "+deviceCount);
                        /*if (mDeviceIndex < deviceCount) mDevice = mAllBondedDevices[mDeviceIndex];
                        else {
                            mDeviceIndex = 0;
                            mDevice = mAllBondedDevices[1];
                        }*/

                        String first3 = "";// mAllBondedDevices[j].substring(0, Math.min(s.length(), 3));
                        String bd;
                        for(int j=0; j < mAllBondedDevices.length;j++)
                        {
                            bd = mAllBondedDevices[j].getName();
                            first3 = bd.substring(0, 3);
                            if(first3.equalsIgnoreCase("HXM")){
                                appendLog("in if");
                                mDeviceIndex = 0;
                                mDevice= mAllBondedDevices[j];
                                break;
                            }else {
                                appendLog("not in if");
                                appendLog("first3 is "+first3);
                                    mDeviceIndex=0;
                                    mDevice = mAllBondedDevices[0];
                                  }
                        }

                        String[] deviceNames = new String[deviceCount];
                        int i = 0;
                        for (BluetoothDevice device : mAllBondedDevices) {
                            deviceNames[i++] = device.getName();
                        }
                        SelectDeviceDialogFragment deviceDialog =
                                SelectDeviceDialogFragment.newInstance(deviceNames, mDeviceIndex);
                        deviceDialog.show(getFragmentManager(), "deviceDialog");
                    }
                  //jt
                    appendLog("This is the name of the connected device");
                    appendLog(mDevice.getName());
                    appendLog(mDevice.toString());

                    appendLog("Current bondstate value " + mDevice.getBondState());
                    java.util.Date date= new java.util.Date();
                    appendLog(new Timestamp(date.getTime()).toString());    
                    //appendLog(mBluetoothAdapter.getBondedDevices());



                    try {

                        mServerSocket.accept(20000);//twenty seconds or 20k milli-seconds before timeout

                    } catch (IOException e) {
                      // TODO Auto-generated catch block
                       e.printStackTrace();
                    }
                    //jt
                }
            });



            // Initiates channel disconnect through {@link BluetoothHDPService}.
            Button disconnectButton = (Button) findViewById(R.id.button_disconnect_channel);
            disconnectButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    disconnectChannel();
                }
            });
            registerReceiver(mReceiver, initIntentFilter());
        }

        // Sets up communication with {@link BluetoothHDPService}.
        private ServiceConnection mConnection = new ServiceConnection() {
            public void onServiceConnected(ComponentName name, IBinder service) {
                mHealthServiceBound = true;
                Message msg = Message.obtain(null, BluetoothHDPService.MSG_REG_CLIENT);
                msg.replyTo = mMessenger;
                mHealthService = new Messenger(service);
                try {
                    mHealthService.send(msg);
                } catch (RemoteException e) {
                    Log.w(TAG, "Unable to register client to service.");
                    e.printStackTrace();
                }
            }

            public void onServiceDisconnected(ComponentName name) {
                mHealthService = null;
                mHealthServiceBound = false;
            }
        };

        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mHealthServiceBound) unbindService(mConnection);
            unregisterReceiver(mReceiver);
        }

        @Override
        protected void onStart() {
            super.onStart();
            // If Bluetooth is not on, request that it be enabled.
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            } else {
                initialize();
            }
        }

        /**
         * Ensures user has turned on Bluetooth on the Android device.
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
            case REQUEST_ENABLE_BT:
                if (resultCode == Activity.RESULT_OK) {
                    initialize();
                } else {
                    finish();
                    return;
                }
            }
        }

        /**
         * Used by {@link SelectDeviceDialogFragment} to record the bonded Bluetooth device selected
         * by the user.
         *
         * @param position Position of the bonded Bluetooth device in the array.
         */
        public void setDevice(int position) {
            mDevice = this.mAllBondedDevices[position];
            mDeviceIndex = position;
        }

        private void connectChannel() {
            sendMessageWithDevice(BluetoothHDPService.MSG_CONNECT_CHANNEL);
        }

        private void disconnectChannel() {
            sendMessageWithDevice(BluetoothHDPService.MSG_DISCONNECT_CHANNEL);
        }

        private void initialize() {
            // Starts health service.
            Intent intent = new Intent(this, BluetoothHDPService.class);
            startService(intent);
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }

        // Intent filter and broadcast receive to handle Bluetooth on event.
        private IntentFilter initIntentFilter() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
            return filter;
        }

        private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) ==
                        BluetoothAdapter.STATE_ON) {
                        initialize();
                    }
                }
            }
        };

        // Sends a message to {@link BluetoothHDPService}.
        private void sendMessage(int what, int value) {
            if (mHealthService == null) {
                Log.d(TAG, "Health Service not connected.");
                return;
            }

            try {
                mHealthService.send(Message.obtain(null, what, value, 0));
            } catch (RemoteException e) {
                Log.w(TAG, "Unable to reach service.");
                e.printStackTrace();
            }
        }

        // Sends an update message, along with an HDP BluetoothDevice object, to
        // {@link BluetoothHDPService}.  The BluetoothDevice object is needed by the channel creation
        // method.
        private void sendMessageWithDevice(int what) {
            if (mHealthService == null) {
                Log.d(TAG, "Health Service not connected.");
                return;
            }

            try {
                mHealthService.send(Message.obtain(null, what, mDevice));
            } catch (RemoteException e) {
                Log.w(TAG, "Unable to reach service.");
                e.printStackTrace();
            }
        }

        /**
         * Dialog to display a list of bonded Bluetooth devices for user to select from.  This is
         * needed only for channel connection initiated from the application.
         */
        public static class SelectDeviceDialogFragment extends DialogFragment {

            public static SelectDeviceDialogFragment newInstance(String[] names, int position) {
                SelectDeviceDialogFragment frag = new SelectDeviceDialogFragment();
                Bundle args = new Bundle();
                args.putStringArray("names", names);
                args.putInt("position", position);
                frag.setArguments(args);
                return frag;
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                String[] deviceNames = getArguments().getStringArray("names");
                int position = getArguments().getInt("position", -1);
                if (position == -1) position = 0;
                return new AlertDialog.Builder(getActivity())
                        .setTitle(R.string.select_device)
                        .setPositiveButton(R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    ((BluetoothHDPActivity) getActivity()).connectChannel();
                                }
                            })
                        .setSingleChoiceItems(deviceNames, position,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    ((BluetoothHDPActivity) getActivity()).setDevice(which);
                                }
                            }
                        )
                        .create();
            }
        }
    }
package com.example.bluetooth.health;
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.OutputStream;
导入java.io.OutputStreamWriter;
导入java.sql.Timestamp;
导入java.util.Date;
导入java.util.UUID;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.app.Dialog;
导入android.app.DialogFragment;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothServerSocket;
导入android.bluetooth.BluetoothSocket;
导入android.content.BroadcastReceiver;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.content.ServiceConnection;
导入android.content.res.Resources;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.IBinder;
导入android.os.Message;
导入android.os.Messenger;
导入android.os.RemoteException;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*示例应用程序的主用户界面。所有蓝牙健康相关
*操作发生在{@link BluetoothHDPService}中。此活动将消息传入和传出
*服务。
*/
公共类BluetoothHDPActivity扩展活动{
私有静态最终字符串TAG=“BluetoothHealthActivity”;
//根据使用的设备,使用适当的IEEE 11073数据类型。
//下面是一些示例。有关详细信息,请参阅相关的蓝牙HDP规范。
//0x1007-血压计
//0x1008-体温计
//0x100F-体重秤
私有静态最终整数运行状况配置文件源数据类型=0x1007;
私有静态最终整数请求_ENABLE_BT=1;
//私有静态最终UUID MY_UUID=UUID.fromString(“00001101-0000-1000-8000-00805f9b34fb”);
私有静态最终UUID MY_UUID=UUID.fromString(“HXM020735”);
公共静态最终整型消息_READ=0;
专用文本视图MConnecticator;
私有图像视图mDataIndicator;
私有文本视图mStatusMessage;
私人蓝牙适配器mBluetoothAdapter;
私人蓝牙设备[]mAllBondedDevices;
私人蓝牙设备;
//jt
私有BluetoothServerSocket mServerSocket;
//jt
私有int mDeviceIndex=0;
私人资源;
私人信使健康服务;
私有布尔mHealthServiceBound;
私有类ConnectThread扩展线程{
私人最终蓝牙插座mmSocket;
专用最终蓝牙设备;
/**/
私有最终输入流mmInStream=null;
私有最终输出流mmOutStream=null;
私人经理人;
/**/
公共连接线程(蓝牙设备){
//使用稍后指定给mmSocket的临时对象,
//因为mmSocket是最终的
BluetoothSocket tmp=null;
mmDevice=设备;
//获取BluetoothSocket以连接给定的BluetoothDevice
试一试{
//MY_UUID是应用程序的UUID字符串,也由服务器代码使用
tmp=device.createrFComSocketToServiceRecord(我的UUID);
}捕获(IOE){}
mmSocket=tmp;
InputStream tmpIn=null;
OutputStream tmpOut=null;
}
public void ConnectedThread(){
//mmSocket=插座;
InputStream tmpIn=null;
OutputStream tmpOut=null;
//使用临时对象获取输入和输出流,因为
//成员流是最终的
试一试{
tmpIn=mmSocket.getInputStream();
tmpOut=mmSocket.getOutputStream();
}捕获(IOE){}
}
公开募捐{
//取消查找,因为它会减慢连接速度
mBluetoothAdapter.cancelDiscovery();
试一试{
//通过插座连接设备。这将阻止
//直到它成功或抛出异常
mmSocket.connect();
}捕获(IOException-connectException){
//无法连接;请关闭插座并退出
试一试{