80,349
社区成员
发帖
与我相关
我的任务
分享
//服务端线程代码
public class AcceptThread extends Thread{
private BluetoothServerSocket mServerSocket;
private BluetoothSocket mSocket;
private InputStream btIs;
private OutputStream btOs;
private PrintWriter writer;
private boolean canAccept;
private boolean canRev;
public AcceptThread(){
canAccept = true;
canRev = true;
}
@Override
public void run(){
try {
//获取套接字
BluetoothServerSocket temp = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("TEST", UUID.fromString(BT_UUID));
mServerSocket = temp;
//监听连接请求 -- 作为测试,只允许连接一个设备
if(mServerSocket != null){
mSocket = mServerSocket.accept();//阻塞等待连接
AddShow("有客户端连接");
}
//获取输出输入流
btIs = mSocket.getInputStream();
btOs = mSocket.getOutputStream();
//通讯-接收消息
BufferedReader reader = new BufferedReader(new InputStreamReader(btIs, "UTF-8"));
String revTxt = null;
if(canRev){
revTxt = reader.readLine();
AddShow("接收消息:" + revTxt);
}
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(mSocket != null){
mSocket.close();
}
}catch (IOException e){
e.printStackTrace();
AddShow("错误消息:" + e.getMessage());
}
}
}
//客户端线程
class ConnectThread extends Thread {
private BluetoothDevice mDevice;
private BluetoothSocket mSocket;
private InputStream btIs;
private OutputStream btOs;
private boolean canRev;
private PrintWriter writer;
public ConnectThread(BluetoothDevice device) {
mDevice = device;
canRev = true;
}
@Override
public void run() {
if (mDevice != null) {
try {
//获取套接字
BluetoothSocket tmp = mDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101A-0000-1000-8000-00805F9B34FB"));
mSocket = tmp;
// Method m = mDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
// mSocket = (BluetoothSocket) m.invoke(mDevice, 1);
//发起连接请求
if (mSocket != null) {
mSocket.connect();
}
AddShow("连接 " + mDevice.getName() + "成功!");
//获取输入输出流
btIs = mSocket.getInputStream();
btOs = mSocket.getOutputStream();
//通讯-接收消息
BufferedReader reader = new BufferedReader(new InputStreamReader(btIs, "UTF-8"));
String content = null;
while (canRev) {
content = reader.readLine();
AddShow("收到消息:" + content);
}
} catch (IOException e) {
e.printStackTrace();
String log = e.getMessage().toString();
AddShow("错误:" + log);
} finally {
try {
if (mSocket != null) {
mSocket.close();
}
//btIs.close();//两个输出流都依赖socket,关闭socket即可
//btOs.close();
} catch (IOException e) {
e.printStackTrace();
AddShow("错误:" + e.getMessage());
}
}
}
}
public void write(String msg) {
if (btOs != null) {
try {
if (writer == null) {
writer = new PrintWriter(new OutputStreamWriter(btOs, "UTF-8"), true);
}
writer.println(msg);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
writer.close();
AddShow("错误:" + e.getMessage());
}
}
}
}
if(device.getName() != null) {
AddShow("找到设备" + device.getName());
if(device.getUuids() != null){
AddShow(device.getUuids()[0].toString());
}
if(device.getName().equals(m_blueName))
{
m_device = device;
Toast.makeText(MainActivity.this, "找到设备", Toast.LENGTH_SHORT).show();
stopDiscovery();
mConnectThread = new ConnectThread(device);
mConnectThread.start();
}
//m_devicelist.add(device);
}