这是MainActivity的代码,只有一个button,点击就会新建一个发送数据的线程:public class MainActivity extends Activity {
private BTSendThread sendThread = null;
private static int flag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sendThread = new BTSendThread(mHandler,"I am a DB" + flag);
new Thread(sendThread).start();
flag ++;
}
});
} private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1: {
Toast.makeText(MainActivity.this, (String) msg.obj, 1000)
.show();
if(sendThread != null){
sendThread = null;
}
break;
}
default:
break;
}
}
};
}
private BTSendThread sendThread = null;
private static int flag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
sendThread = new BTSendThread(mHandler,"I am a DB" + flag);
new Thread(sendThread).start();
flag ++;
}
});
} private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1: {
Toast.makeText(MainActivity.this, (String) msg.obj, 1000)
.show();
if(sendThread != null){
sendThread = null;
}
break;
}
default:
break;
}
}
};
}
private Handler handler;
private String msg; private BluetoothSocket socket;
private BluetoothDevice device;
private BluetoothAdapter mBTAdapter = BluetoothAdapter.getDefaultAdapter();;
private String address = "00:18:33:79:BD:71"; public BTSendThread(Handler handler, String msg) {
this.handler = handler;
this.msg = msg;
if (!mBTAdapter.isEnabled()) {
mBTAdapter.enable();
} device = mBTAdapter.getRemoteDevice(address);
try { socket = device.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket.connect();
ReadThread read = new ReadThread();
read.start();
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
try {
OutputStream os = socket.getOutputStream();
os.write(msg.getBytes());
} catch (IOException ex) {
}
} // 读取数据
private class ReadThread extends Thread { public void run() {
byte[] buffer = new byte[1024];
int bytes;
InputStream mmInStream = null; try {
mmInStream = socket.getInputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
String s = null;
try {
if ((bytes = mmInStream.read(buffer)) > 0) {
byte[] buf_data = new byte[bytes];
for (int i = 0; i < bytes; i++) {
buf_data[i] = buffer[i];
}
s = new String(buf_data);
if (s != null && !"".endsWith(s)) {
Message msg = new Message();
msg.what = 1;
msg.obj = s;
handler.sendMessage(msg);
if(socket != null){
try{
socket.close();
}catch(IOException ex){}
socket = null;
}
if(mBTAdapter.isEnabled()){
mBTAdapter.disable();
}
}
}
} catch (IOException e) {
try {
mmInStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
}
}
}