我把中间的部分省略了,应该是onResume()那里出了问题,请问应该如何解决
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class Control extends Activity implements Runnable {
String tag = "MyControl";
public boolean BluetoothFlag = true;
public int requestCode = 1, num1 = 0, num2 = 0,num3 = 0,runState = 0;
private float HCenterX,HCenterY;
private float RangeCenterX,RangeCenterY,RangeR = 100;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private ImageView iv1;
private ImageView iv2;
private ImageView iv3;
private ImageButton ib1;
FrameLayout.LayoutParams LP_iv2;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private static String address = null;//要连接的蓝牙设备MAC地址
private OutputStream outStream = null;
private InputStream InStream = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.control);
iv1 = (ImageView) findViewById(R.id.ct_iv1);
iv2 = (ImageView) findViewById(R.id.ct_iv2);
iv3 = (ImageView) findViewById(R.id.ct_iv3);
ib1 = (ImageButton) findViewById(R.id.ct_ib1);
ib1.setOnClickListener(new ClickEvent());
Animation Aiv1_zoom = AnimationUtils.loadAnimation(this, R.drawable.iv1_zoom);
iv1.startAnimation(Aiv1_zoom);
LP_iv2 = (FrameLayout.LayoutParams) iv2.getLayoutParams();//获取控件布局参数
Display display = getWindowManager().getDefaultDisplay();
RangeCenterX = display.getWidth() / 2;
RangeCenterY = display.getHeight() / 2;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "您的设备不支持蓝牙", Toast.LENGTH_LONG).show();
Log.i(tag, "设备不支持蓝牙");
}
}
…………
public void sendCmd(String message) {
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] msgBuffer;
msgBuffer = message.getBytes();
try {
outStream.write(msgBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
DisplayToast("正在尝试连接蓝牙设备,请稍后····");
if(address == null){
address = "00:12:06:01:51:26";
}else {}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);//我觉得是这的问题
} catch (IOException e) {
DisplayToast("套接字创建失败!");
}
DisplayToast("成功连接蓝牙设备!");
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();// Debug到这应用退出
DisplayToast("连接成功建立,可以开始操控了~~~");
myText.setText("蓝牙设备已准备好了!");
sendCmd("Connect OK!");
BluetoothFlag = true;
MyThread bluetoothThread = new MyThread();
bluetoothThread.start();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
DisplayToast("连接没有建立,无法关闭套接字!");
}
}
}
@Override
public void onPause() {
super.onPause();
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
btSocket.close();
BluetoothFlag = false;// 关闭蓝牙读线程
} catch (IOException e2) {
}
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroy() {
mBluetoothAdapter.disable();
super.onDestroy();
}
@Override
public void run() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 & resultCode == 1) {
address = data.getStringExtra("MAC");
}
}
private class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ct_ib1 : {
num2 = 1;
Intent intent = new Intent(Control.this, BlueTooth.class);
startActivityForResult(intent, requestCode);
}
}
}
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
Log.d(tag, "in the thread:age=" + data.getInt("age")
+ " name=" + data.getString("name"));
try {
InStream = btSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
byte[] tmp = new byte[5];
try {
InStream.read(tmp, 0, 5);
Log.i(tag, new String(tmp, 0, 5) + " ");
} catch (IOException e) {
e.printStackTrace();
}
}
MyHandler(Looper looper) {
super(looper);
}
MyHandler() {}
}
public class MyThread extends Thread {
MyThread() {
BluetoothFlag = true;
try {
InStream = btSocket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
byte[] tmp = null;
while (BluetoothFlag) {
try {
demo();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String demo() throws IOException, InterruptedException {
byte[] buffer = new byte[1];
int length = 0;
byte bb = 0;
String line = null;
Log.i(tag, "demo");
ArrayList<Byte> list = new ArrayList<Byte>();
while (InStream.read(buffer) == -1) {
Thread.sleep(100);
Log.i(tag, "-->" + InStream.read(buffer));
}
return null;
}
}
}