300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android 蓝牙自动断开 Android蓝牙:连接()/断开()

android 蓝牙自动断开 Android蓝牙:连接()/断开()

时间:2019-11-01 02:21:18

相关推荐

android 蓝牙自动断开 Android蓝牙:连接()/断开()

我目前正在设计一个应用程序,它需要连接到设备,写入/读取数据,并可靠地关闭连接。目前我有写/读固体。我的断开连接然后重新连接非常不可靠,并且经常实际上使手机崩溃。我一直在寻找通过大量文章试图弄清楚和..没有运气..Android蓝牙:连接()/断开()

****连接功能**

public boolean connect()

{

ConfigData.getInstance();

BluetoothSocket tmp = null;

BluetoothDevice device = ConfigData.m_SharedBluetoothDevice;

Method m;

try {

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);//(BluetoothSocket)

m.invoke(device, 1);

} catch (SecurityException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

ConfigData.m_SharedBluetoothSocket = tmp;

try {

ConfigData.m_SharedBluetoothSocket.connect();

ConfigData.bIsBTConnected = true;

} catch (IOException e) {

try {

closeSocket();

m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

tmp = (BluetoothSocket) m.invoke(device, 1);

} catch (SecurityException e1) {

e1.printStackTrace();

} catch (NoSuchMethodException e1) {

e1.printStackTrace();

} catch (IllegalArgumentException e1) {

e.printStackTrace();

} catch (IllegalAccessException e1) {

e.printStackTrace();

} catch (InvocationTargetException e1) {

e.printStackTrace();

}

ConfigData.m_SharedBluetoothSocket = tmp;

try {

ConfigData.m_SharedBluetoothSocket.connect();

ConfigData.bIsBTConnected = true;

} catch (IOException e1) {

ConfigData.m_BluetoothException += e1.toString();

ConfigData.bIsBTConnected = false;

return false;

}

e.printStackTrace();

return true;

}

return true;

}

****断开功能**

public void destroySocket()

{

try {

if(m_InStream != null)

{

m_InStream.close();

m_InStream = null;

}

if(m_OutStream != null)

{

m_OutStream.close();

m_OutStream = null;

}

if(ConfigData.m_SharedBluetoothSocket != null)

{

ConfigData.m_SharedBluetoothSocket.close();

ConfigData.m_SharedBluetoothSocket = null;

}

if(m_InStream == null && m_OutStream == null && ConfigData.m_SharedBluetoothSocket == null)

{

ConfigData.bIsBTConnected = false;

}

} catch (IOException e1) {

m_InStream = null;

m_OutStream = null;

ConfigData.m_SharedBluetoothSocket = null;

e1.printStackTrace();

}

}

因此断开连接成功并返回一切为空。问题是,当我在第二次连接尝试中重新连接数据块时,它们会坐在那里或完全崩溃,导致几次重新启动。

有没有人有任何建议吗?它非常令人沮丧。任何帮助将非常感激!!

谢谢Gig'Em! TxAg

-08-19

TxAg

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。