關於手機藍芽開發時遇到的java.io.IOException: Service discovery failed和 @@@ ABORTING: INVALID HEAP ADDRESS IN dlf

來源:互聯網
上載者:User

   我做的是關於藍芽串口的開發,開始串連裝置的時候用的是// btSocket = btDevice.createRfcommSocketToServiceRecord(UUID

// .fromString("00001101-0000-1000-8000-00805F9B34FB"));

用這句代碼、再次串連別的裝置時候就會出現java.io.IOException: Service discovery failed這個異常、解決方案是

/*
* 初始化Socket
*/
private void initSocket() {
BluetoothSocket temp = null;
try {
// temp =
// mTouchObject.bluetoothDevice.createRfcommSocketToServiceRecord(UUID.fromString(CONNECT_UUID));
// 以上取得socket方法不能正常使用, 用以下方法代替
Method m = btDevice.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
temp = (BluetoothSocket) m.invoke(btDevice, 1);
// 怪異錯誤: 直接賦值給socket,對socket操作可能出現異常, 要通過中間變數temp賦值給socket
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
btSocket = temp;
}

通過中間變數temp賦值給socket, 

第二個問題就是在調用socket.close()這個方法時候出現了@@@ ABORTING: INVALID HEAP ADDRESS IN dlf這個錯誤,網上查詢這是記憶體對齊的問題,找了好久的原因是因為自己沒有對inputstream 正確關閉

try {
if (mmInStream != null) {
mmInStream.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
shutdownClient();

if (btAdapter != null && btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
if (btSocket != null) {
try {

btSocket.close();// 結束socket
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btSocket = null;
}

正確關閉就好了,恩這就是我解決這兩個問題的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.