我将我的Android手机连接到HC-05蓝牙模块。它有点工作,我可以完成一些I / O,但我也有一些错误。我想要处理的第一个错误是,每次连接到我的蓝牙设备时,我都会收到" Service Discovery Failed" IO Exception,除了第一次。只有重新启动手机后才会干净连接。所以,我期待一些东西是开放的,但我唯一能想到的就是套接字,我就把它关闭了。
09-16 13:55:16.463 8085-8085/ltd.arctura.laro_can_bus I/BluetoothSocket_MTK: [JSR82] Bluetooth Socket Constructor
09-16 13:55:16.463 8085-8085/ltd.arctura.laro_can_bus I/BluetoothSocket_MTK: [JSR82] type=1 fd=-1 auth=false encrypt=false port=-1
09-16 13:55:16.466 8085-8085/ltd.arctura.laro_can_bus I/BluetoothSocket_MTK: [JSR82] connect: do SDP
09-16 13:55:16.738 8085-8098/ltd.arctura.laro_can_bus I/BluetoothSocket_MTK: [JSR82] SdpHelper::onRfcommChannelFound: channel=-1
09-16 13:55:16.757 8085-8085/ltd.arctura.laro_can_bus W/System.err: java.io.IOException: Service discovery failed
09-16 13:55:16.757 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:813)
09-16 13:55:16.758 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:382)
09-16 13:55:16.758 8085-8085/ltd.arctura.laro_can_bus W/System.err: at ltd.arctura.laro_can_bus.bluetooth$1.onItemClick(bluetooth.java:173)
09-16 13:55:16.758 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-16 13:55:16.758 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.widget.AbsListView$1.run(AbsListView.java:3571)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.os.Handler.handleCallback(Handler.java:725)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.os.Looper.loop(Looper.java:153)
09-16 13:55:16.759 8085-8085/ltd.arctura.laro_can_bus W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5341)
09-16 13:55:16.760 8085-8085/ltd.arctura.laro_can_bus W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
09-16 13:55:16.760 8085-8085/ltd.arctura.laro_can_bus W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
09-16 13:55:16.760 8085-8085/ltd.arctura.laro_can_bus W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
09-16 13:55:16.761 8085-8085/ltd.arctura.laro_can_bus W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
09-16 13:55:16.765 8085-8085/ltd.arctura.laro_can_bus W/System.err: at dalvik.system.NativeStart.main(Native Method)
重启HC-05或手机上的蓝牙都是不够的。
通过执行以下操作验证了UUID是否正确:
ParcelUuid[] uuids=btHC05.getUuids();
if (uuids != null) {
msg(uuids[0].toString());
} else {
msg("No UUID!?");
}
这会产生与我配置的相同的UUID。
我试图在套接字上做一个.close,但这没有帮助。我没有要关闭的输入或输出流。
该设备已配对并证明有效。我通常不了解Connect()如何使服务发现失败..我不希望connect()做任何发现..
private BluetoothAdapter myBluetooth = null;
myBluetooth = BluetoothAdapter.getDefaultAdapter();
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
String address = null;
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
和
try {
if (btSocket == null) {
BluetoothDevice btHC05 = myBluetooth.getRemoteDevice(address);
btSocket = btHC05.createInsecureRfcommSocketToServiceRecord(myUUID);
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
}
catch (IOException e){
Toast.makeText(getApplicationContext(), "Error: " + e.toString(), Toast.LENGTH_LONG).show();
ConnectSuccess = false;
}
if (!ConnectSuccess)
{
finish();
}
else {
msg("Connected.");
Intent returnIntent = new Intent();
returnIntent.putExtra("address",address);
setResult(MainActivity.RESULT_OK,returnIntent);
finish();
}
}
还有更多,但这是相关的......它有点凌乱,因为我已经尝试了很多错误; - )
此外,这可能是相关的。所有这些都发生在一个单独的活动中,该活动为用户提供了可供选择的BT设备列表,然后返回到Main_Activity。