我正在開發一個簡單的藍牙應用程序,我想發送文本消息。在嘗試配對設備時遇到問題,它會拋出異常「java.io.IOException:服務發現失敗」。欲瞭解更多詳情,我在此發佈代碼。在LG的手機Android版本 移植的應用程序2.3.3問題與藍牙設備配對
private static final UUID MY_UUID = UUID.fromString("00000003-0000-1000-8000-00805F9B34FB");
BluetoothDevice btDevice = BluetoothActivity.btDevices.get(position).getBtDevice();
clientSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);
//Method m = btDevice.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] { UUID.class });
//clientSocket =(BluetoothSocket) m.invoke(btDevice, MY_UUID);
//Method m = btDevice.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
//clientSocket =(BluetoothSocket) m.invoke(btDevice, 1);
if(clientSocket!=null)
{
if(BluetoothActivity.btAdapter.isDiscovering()){
BluetoothActivity.btAdapter.cancelDiscovery();
}
//facing issue during paring
clientSocket.connect();
tmpOut = clientSocket.getOutputStream();
tmpOut.write("HelloWorld.txt".getBytes());
if(tmpOut!=null){
tmpOut.close();
}
}
任何形式的幫助表示讚賞,
因此'clientSocket.connect()'引起異常的線? btDevice是否被正確設置爲您想要的設備?我發現我需要在設置套接字之前通過手機設置手動連接到其他BT設備。 –
@ Pravin008您只能連接Android設備或PC與其他設備? – Venky