나는 안드로이드 개발자 페이지에서 블루투스 튜토리얼에서 파생 된 코드를 사용하고 있습니다. 내 앱이 BT 저울에 연결하여 무게 정보를 기록하려고합니다.Android 블루투스 socket.connect가 실패합니다. 자바 입출력 예외를 던졌습니다
내 프로그램이 BluetoothSocket.connect();를 수행하려고 할 때 오류가 발생합니다. 프로그램은 BT 장치에서 데이터를 수신하기 위해 Bluetooth 클라이언트/슬레이브로 실행됩니다. 사용자는 화면에서 페어링 된 장치 목록을보고 특정 장치를 클릭하여 연결합니다.
따라서 사용자가 특정 기기를 클릭하면 앱이 이미 페어링되어 있으므로 앱이 연결을 시도합니다.
이 앱은 BluetoothSocket을 얻는다하지만 연결을 수행하는 데 실패()
난 그냥 다른 블루투스 장치와 연결을 설정하기 위해 시도했지만 난 항상 반복해서 같은 오류가 결국. 응용 프로그램을 실행하는 동안 내가 얻은 로그 응용 프로그램에 대한 전체 코드가 here
다음 찾을 수 있습니다
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
Log.i(tag, "construct");
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.i(tag, "get socket failed");
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
Log.i(tag, "connect - run");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.i(tag, "connect - succeeded");
} catch (IOException connectException) { Log.i(tag, "connect failed");
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
됩니다 :
다음 는connectThread의 자바 코드
01-01 01:46:00.359: I/ActivityManager(1553): Displayed com.test.bluetooth/.Main_Activity: +579ms
01-01 01:46:00.953: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:00.968: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:19:15:66:C4:2D)
01-01 01:46:00.984: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:1A:88:00:01:25)
01-01 01:46:01.000: D/BluetoothService(1553): uuid(system): 00001105-0000-1000-8000-00805f9b34fb 1
01-01 01:46:04.421: I/debugging(2400): construct
01-01 01:46:04.421: D/BluetoothEventLoop(1553): Property Changed: Discovering : false
01-01 01:46:04.421: I/debugging(2400): in click listener
01-01 01:46:04.421: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.429: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.437: I/debugging(2400): connect - run
01-01 01:46:04.437: V/BluetoothDiscoveryReceiver(1864): Received: android.bluetooth.adapter.action.DISCOVERY_FINISHED
01-01 01:46:04.445: E/BluetoothEventLoop.cpp(1553): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
01-01 01:46:05.484: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: true
01-01 01:46:06.554: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:06.562: D/BluetoothService(1553): uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1
01-01 01:46:06.562: D/BluetoothService(1553): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1
01-01 01:46:06.562: I/debugging(2400): connect failed
01-01 01:46:06.562: W/System.err(2400): java.io.IOException: Invalid argument
01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connectNative(Native Method)
01-01 01:46:06.562: W/System.err(2400): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
01-01 01:46:06.562: W/System.err(2400): at com.test.bluetooth.Main_Activity$ConnectThread.run(Main_Activity.java:326)
01-01 01:46:10.132: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: false
01-01 01:47:05.914: D/SurfaceFlinger(1311): About to give-up screen, flinger = 0x41876af0
기본/모든 BT BT 연결을 설정하는 데 큰 도움이됩니다. preciated.
감사합니다.
좋아요, 바보 같은 질문 : 자습서는 어떤 버전의 Android입니다 를 위해 쓰여지고, 당신은 어느 쪽을 달리고 있습니까? 현재 환경에서 너무 오래된 (또는 너무 새로운) 작업을하는 경우 오류가 발생할 수 있습니다. –
@RachelKeslensky 내 API 레벨 15 (ICS)를 사용하고 있지만 내 장치는 Jelly Bean을 실행합니다. 그래서, 내 장치는 OS의 고급 버전을 실행하고 내 시야에 문제가 안됩니다. – Student91