android蓝牙连接三部曲



// 连接蓝牙的线程
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;

public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
tmp = BlueToothService.this.adapter.listenUsingRfcommWithServiceRecord("BTPrinter",
BlueToothService.MY_UUID);
} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "listen() failed", e);
}
this.mmServerSocket = tmp;
}

public void run() {
if (BlueToothService.this.D.booleanValue())
Log.d(BlueToothService.this.TAG, "BEGIN mAcceptThread" + this);
setName("AcceptThread");
BluetoothSocket socket = null;

while (BlueToothService.this.mState != 3) {
try {
socket = this.mmServerSocket.accept();
} catch (IOException e) {
break;
}

if (socket != null)
synchronized (BlueToothService.this) {
switch (BlueToothService.this.mState) {
case 1:
case 2:
BlueToothService.this.connected(socket, socket.getRemoteDevice());
break;
case 0:
case 3:
try {
socket.close();
} catch (IOException localIOException1) {
}
}
}
}
}

public void cancel() {
if (BlueToothService.this.D.booleanValue())
Log.d(BlueToothService.this.TAG, "cancel " + this);
try {
this.mmServerSocket.close();
} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "close() of server failed", e);
}
}
}

// 连接中的线程
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;

public ConnectThread(BluetoothDevice device) {
this.mmDevice = device;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(BlueToothService.MY_UUID);
} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "create() failed", e);
}
this.mmSocket = tmp;
}

public void run() {
Log.i(BlueToothService.this.TAG, "BEGIN mConnectThread");
setName("ConnectThread");

BlueToothService.this.adapter.cancelDiscovery();
BlueToothService.this.SetScanState(1);
try {
this.mmSocket.connect();
BlueToothService.this.connectionSuccess();
} catch (IOException e) {
BlueToothService.this.connectionFailed();
try {
this.mmSocket.close();
} catch (IOException e2) {
Log.e(BlueToothService.this.TAG, "unable to close() socket during connection failure", e2);
}

BlueToothService.this.start();
return;
}

synchronized (BlueToothService.this) {
BlueToothService.this.mConnectThread = null;
}

BlueToothService.this.connected(this.mmSocket, this.mmDevice);
}

public void cancel() {
try {
this.mmSocket.close();
} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "close() of connect socket failed", e);
}
}
}

// 连接之后的线程
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private boolean isCancle = false;

public ConnectedThread(BluetoothSocket socket) {
Log.d(BlueToothService.this.TAG, "create ConnectedThread");
this.mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
this.isCancle = false;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "temp sockets not created", e);
}
this.mmInStream = tmpIn;
this.mmOutStream = tmpOut;
}

public void run() {
Log.i(BlueToothService.this.TAG, "BEGIN mConnectedThread");
while (!this.isCancle)
try {
byte[] buffer = new byte[1];

int bytes = this.mmInStream.read(buffer);
if (bytes > 0) {
BlueToothService.this.SetPrinterType(2);

if (buffer[0] != 17) {
BlueToothService.this.SetWriteState(3);
} else {
BlueToothService.this.SetWriteState(2);
}

} else {
Log.e(BlueToothService.this.TAG, "disconnected1");
BlueToothService.this.connectionLost();
this.isCancle = true;
}

} catch (IOException e) {
Log.e(BlueToothService.this.TAG, "disconnected2", e);
BlueToothService.this.connectionLost();
this.isCancle = true;
}
}

public void write(byte[] buffer) {
try {
this.mmOutStream.write(buffer);
Log.i("BTPWRITE", new String(buffer, "GBK"));

BlueToothService.this.mHandler.obtainMessage(3, -1, -1, buffer).sendToTarget();
} catch (IOException localIOException) {
}
}

public void cancel() {
try {
this.isCancle = true;
this.mmSocket.close();
Log.d(BlueToothService.this.TAG, "562cancel suc");
BlueToothService.this.setState(1);
} catch (IOException e) {
Log.d(BlueToothService.this.TAG, "565cancel failed");
}
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值