各位大神们,小生的毕设是android平台,蓝牙通信+数据库的应用,眼看程序渐渐转安,却发现了百度不得其解,我连英文google都用上了,却什么都查不到的错误。##### ERROR : wrp_wsock_create: wrp_sock_create : out of wsock blocks#####解释一下程序的流程,UI线程调用一个子线程来负责传输,子线程先搜索10秒,把搜索到的设备都存到ArrayList里,然后结束搜索,开始对每一个发现的设备进行uuid检验,uuid是通用设备的00001101-0000-1000-8000-00805F9B34FB,然后connect(),成功后调用另一个子线程来负责这个设备的传输,为了保证没有阻塞进程的发生,这里将该线程进行了join(5000),即阻塞5秒结束,然后开始下一个设备的传输,如果所有设备都结束了,等15秒,重新开始该过程。测试是只有一个可传输设备和一个“哑”设备,问题在刚开始运行是不会出现,大概进行了6-7次循环后,就会出现那个错误,会导致uuid检验抛出异常,连接也就不可能被执行到了,而且一出错误就一直有,再也恢复不了了……个人推测:
这个wsock block是个什么空间,用来存和蓝牙相关的什么东西,可能是socket,太多了就溢出了,用不了了,难道是我socket关闭的不正确?uuid检验的异常抛出条件是,蓝牙设备不可用或不足权限。相关代码:
一个由UI线程调用的子线程的run中的一部分:while(true){
//searching section
possibleDevices.clear();
//searching section
if(!bluetooth.isDiscovering()){
Log.i(TAG, "start discovery: "+bluetooth.startDiscovery());
//bluetooth.startDiscovery();
try {
sleep(10000);//搜索10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "cancel discovery: "+bluetooth.cancelDiscovery());
//bluetooth.cancelDiscovery();
}else{
Log.i(TAG, "cancel discovery 2");
bluetooth.cancelDiscovery();
}
//wait for cancel discovery,不知道有没有用
try {
sleep(500);
} catch (InterruptedException e1) {
Log.i(TAG, "wait for cancel discovery failed: InterruptedException");
}
//connecting section
for(BluetoothDevice oneDevice:possibleDevices){
oneSocket = null;
try {
oneSocket = oneDevice.createRfcommSocketToServiceRecord(uuid);
if(oneSocket != null){
try{
oneSocket.connect();
Log.i(TAG, "one socket connected");
try {
ConnectOne connectOne = new ConnectOne(oneSocket);
connectOne.start();
connectOne.join(5000);
if(connectOne.isAlive()){
connectOne.interrupt();
}
} catch (InterruptedException e) {
Log.i(TAG, "one connection time out: InterruptedException");
}finally{
try {
oneSocket.close();
Log.i(TAG, "one socket closed");
} catch (IOException e1) {
Log.i(TAG, "one socket close failed: IOException");
}
}
} catch(IOException e) {
Log.i(TAG, "connect failed: IOException");
}
}
} catch (IOException e) {
Log.i(TAG, "uuid failed: IOException"); //一报错这里就开始catch了
continue;
}
}
try {
sleep(15000); //time consuming
} catch (InterruptedException e) {
Log.i(TAG, "protection failed: InterruptedException");
}
}上述部分用到的ConnectOne线程:private class ConnectOne extends Thread{
private BluetoothSocket oneSocket = null;
private InputStream in = null;
private ObjectInputStream oin = null;
private OutputStream out = null;
private ObjectOutputStream oout = null;
public ConnectOne(BluetoothSocket oneSocket){
this.oneSocket = oneSocket;
} @SuppressWarnings("unchecked")
@Override
public void run(){
//receive section
Log.i(TAG, "receive started");
try {
in = oneSocket.getInputStream();//get input stream
try {
oin = new ObjectInputStream(in);//get object input stream
try {
ArrayList<String> shoppingList = (ArrayList<String>)oin.readObject();//read object
for(String oneProduct:shoppingList)
Log.i(TAG, "one product received: "+oneProduct);//log out product received
ArrayList<String> shoppingChoice = process.returnChoice(shoppingList);//process shopping list
//transmit section
try {
out = oneSocket.getOutputStream();//get output stream
try {
oout = new ObjectOutputStream(out);//get object output stream
try{
oout.writeObject(shoppingChoice);//write object
oout.flush();
try{//close all streams
oin.close();
oout.close();
in.close();
out.close();
} catch(IOException e){
Log.i(TAG, "close failed: IOException");
}
} catch(IOException e){
Log.i(TAG, "write object failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "object output stream failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "output stream failed: IOException");
}
} catch (OptionalDataException e) {
Log.i(TAG, "read object failed: OptionalDataException");
} catch (ClassNotFoundException e) {
Log.i(TAG, "read object failed: ClassNotFoundException");
} catch (IOException e) {
Log.i(TAG, "read object failed: IOException");
}
} catch (StreamCorruptedException e) {
Log.i(TAG, "object input stream failed: StreamCorruptedExceptio");
} catch (IOException e) {
Log.i(TAG, "object input stream failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "input stream failed: IOException");
}
}
}
这个wsock block是个什么空间,用来存和蓝牙相关的什么东西,可能是socket,太多了就溢出了,用不了了,难道是我socket关闭的不正确?uuid检验的异常抛出条件是,蓝牙设备不可用或不足权限。相关代码:
一个由UI线程调用的子线程的run中的一部分:while(true){
//searching section
possibleDevices.clear();
//searching section
if(!bluetooth.isDiscovering()){
Log.i(TAG, "start discovery: "+bluetooth.startDiscovery());
//bluetooth.startDiscovery();
try {
sleep(10000);//搜索10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG, "cancel discovery: "+bluetooth.cancelDiscovery());
//bluetooth.cancelDiscovery();
}else{
Log.i(TAG, "cancel discovery 2");
bluetooth.cancelDiscovery();
}
//wait for cancel discovery,不知道有没有用
try {
sleep(500);
} catch (InterruptedException e1) {
Log.i(TAG, "wait for cancel discovery failed: InterruptedException");
}
//connecting section
for(BluetoothDevice oneDevice:possibleDevices){
oneSocket = null;
try {
oneSocket = oneDevice.createRfcommSocketToServiceRecord(uuid);
if(oneSocket != null){
try{
oneSocket.connect();
Log.i(TAG, "one socket connected");
try {
ConnectOne connectOne = new ConnectOne(oneSocket);
connectOne.start();
connectOne.join(5000);
if(connectOne.isAlive()){
connectOne.interrupt();
}
} catch (InterruptedException e) {
Log.i(TAG, "one connection time out: InterruptedException");
}finally{
try {
oneSocket.close();
Log.i(TAG, "one socket closed");
} catch (IOException e1) {
Log.i(TAG, "one socket close failed: IOException");
}
}
} catch(IOException e) {
Log.i(TAG, "connect failed: IOException");
}
}
} catch (IOException e) {
Log.i(TAG, "uuid failed: IOException"); //一报错这里就开始catch了
continue;
}
}
try {
sleep(15000); //time consuming
} catch (InterruptedException e) {
Log.i(TAG, "protection failed: InterruptedException");
}
}上述部分用到的ConnectOne线程:private class ConnectOne extends Thread{
private BluetoothSocket oneSocket = null;
private InputStream in = null;
private ObjectInputStream oin = null;
private OutputStream out = null;
private ObjectOutputStream oout = null;
public ConnectOne(BluetoothSocket oneSocket){
this.oneSocket = oneSocket;
} @SuppressWarnings("unchecked")
@Override
public void run(){
//receive section
Log.i(TAG, "receive started");
try {
in = oneSocket.getInputStream();//get input stream
try {
oin = new ObjectInputStream(in);//get object input stream
try {
ArrayList<String> shoppingList = (ArrayList<String>)oin.readObject();//read object
for(String oneProduct:shoppingList)
Log.i(TAG, "one product received: "+oneProduct);//log out product received
ArrayList<String> shoppingChoice = process.returnChoice(shoppingList);//process shopping list
//transmit section
try {
out = oneSocket.getOutputStream();//get output stream
try {
oout = new ObjectOutputStream(out);//get object output stream
try{
oout.writeObject(shoppingChoice);//write object
oout.flush();
try{//close all streams
oin.close();
oout.close();
in.close();
out.close();
} catch(IOException e){
Log.i(TAG, "close failed: IOException");
}
} catch(IOException e){
Log.i(TAG, "write object failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "object output stream failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "output stream failed: IOException");
}
} catch (OptionalDataException e) {
Log.i(TAG, "read object failed: OptionalDataException");
} catch (ClassNotFoundException e) {
Log.i(TAG, "read object failed: ClassNotFoundException");
} catch (IOException e) {
Log.i(TAG, "read object failed: IOException");
}
} catch (StreamCorruptedException e) {
Log.i(TAG, "object input stream failed: StreamCorruptedExceptio");
} catch (IOException e) {
Log.i(TAG, "object input stream failed: IOException");
}
} catch (IOException e) {
Log.i(TAG, "input stream failed: IOException");
}
}
}
这里有链接
http://code.google.com/p/android-bluez-ime/issues/detail?id=7