0
我試圖設置偵聽傳入連接的BluetoothServerSocket。從日誌和使用應用程序我可以看到,它啓用藍牙,如果它被禁用。它也能夠創建一個ServerSocket。它沒有比這更進一步,我沒有得到run()方法的任何日誌消息。設置藍牙偵聽服務器套接字時出錯
11-19 21:50:20.237 5187 5187 E bluetoothd: Invalid rfcomm listen params
11-19 21:50:20.237 5127 5127 E BluetoothServiceJni: Socket listen failed: 7
11-19 21:50:20.237 5127 5127 E BluetoothAdapterService: Failed to create socket channel
11-19 21:50:20.237 5127 5127 E ObexServerSockets: Error create ServerSockets
11-19 21:50:20.237 5127 5127 E ObexServerSockets: java.io.IOException: Error: -1
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at android.bluetooth.BluetoothAdapter.listenUsingRfcommOn(BluetoothAdapter.java:1542)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at android.bluetooth.BluetoothAdapter.listenUsingRfcommOn(BluetoothAdapter.java:1508)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.ObexServerSockets.create(ObexServerSockets.java:118)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.ObexServerSockets.create(ObexServerSockets.java:84)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.map.BluetoothMapMasInstance.startRfcommSocketListener(BluetoothMapMasInstance.java:273)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.map.BluetoothMapService.startRfcommSocketListeners(BluetoothMapService.java:200)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.map.BluetoothMapService.-wrap10(BluetoothMapService.java)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.bluetooth.map.BluetoothMapService$1.handleMessage(BluetoothMapService.java:327)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at android.os.Looper.loop(Looper.java:148)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at android.app.ActivityThread.main(ActivityThread.java:5415)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at java.lang.reflect.Method.invoke(Native Method)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:744)
11-19 21:50:20.237 5127 5127 E ObexServerSockets: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
11-19 21:50:20.538 5127 5127 E ObexServerSockets: Error to create listening socket after 10 try
11-19 21:50:20.538 5127 5127 E BluetoothMapMasInstance0: Failed to start the listeners
MainActivity.java:
package com.example.android;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.UUID;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AcceptThread()**.run()**; // not enough to create new thread, its method run() also needs to be called!
}
private class AcceptThread extends Thread {
private final String LOG_TAG = getClass().getSimpleName();
private final BluetoothAdapter oBTAdapter = null;
private final BluetoothServerSocket oBTServerSocket;
private final UUID MY_UUID = UUID.fromString("4efaedef-7424-4bf6-bd70-673a7bff43fe");
private int REQUEST_ENABLE_BT = 1;
public AcceptThread() {
BluetoothAdapter oBTAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp = null;
if (!oBTAdapter.isEnabled()) {
Log.i(LOG_TAG, "bluetooth not enabled");
Intent iEnableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(iEnableBT, REQUEST_ENABLE_BT);
}
// Do not go further if bluetooth is not enabled
while (!oBTAdapter.isEnabled()) Log.i(LOG_TAG, "waiting for bluetooth to be enabled");
try {
tmp = oBTAdapter.listenUsingRfcommWithServiceRecord("BTServer", MY_UUID);
Log.i(LOG_TAG, "created ServerSocket");
} catch (IOException e) {
Log.i(LOG_TAG, "couldn't acquire ServerSocket");
Log.i(LOG_TAG, e.getMessage());
}
oBTServerSocket = tmp;
}
public void run() {
Log.i(LOG_TAG, "run()");
BluetoothSocket oBTSocket = null;
while (true) {
try {
oBTSocket = oBTServerSocket.accept();
Log.i(LOG_TAG, "accept()");
} catch (IOException e) {
Log.i(LOG_TAG, "failure on accept()");
Log.i(LOG_TAG, e.getMessage());
break;
}
if (oBTSocket != null) {
Log.i(LOG_TAG, "a connection was accepted");
//manageConnectedThread(oBTSocket);
try {
oBTServerSocket.close();
} catch (IOException e) {
Log.i(LOG_TAG, "failure while closing ServerSocket");
Log.i(LOG_TAG, e.getMessage());
}
break;
}
}
}
public void cancel() {
try {
oBTServerSocket.close();
} catch (IOException e) {
Log.i(LOG_TAG, e.getMessage());
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == RESULT_OK) {
Log.i(LOG_TAG, "bluetooth enabled with success");
} else {
Log.i(LOG_TAG, "failed to enable bluetooth");
}
}
}
}
}
EDIT1:原來方法運行的線程()不叫,因爲...沒有人打電話吧! 現在它進入accept()調用。但有一點後,我得到MainActivity沒有響應。你想關閉它嗎? 我明白accept()是一個阻塞調用,它在連接被接受或有一些錯誤時返回。我不明白爲什麼它說MainActivity沒有響應。這個accept()調用是在一個不同的線程中,那麼這是一個怎樣的問題?