Geo*_*los 12 android bluetooth serversocket
我正在编译SDK 10(2.3.3):
android:minSdkVersion="10"
android:targetSdkVersion="16"
Run Code Online (Sandbox Code Playgroud)
我正在测试两款索尼爱立信智能手机.其中一个有Android 2.3.7,另一个有4.0.1.
我listenUsingInsecureRfcommWithServiceRecord用来在蓝牙上打开一个新的服务器套接字并听取连接.
如果成功接受连接,那么一切正常.我甚至可以尝试取消服务器套接字,但这似乎并没有打扰刚刚创建的连接套接字.
但是当我想在线路bluetoothServerSocket.close();被执行后接受任何连接之前取消服务器套接字时,整个活动就会关闭并且进程终止.而且这不是我能处理的常规例外.
实际上即使logcat本身也退出!! 我必须再次快速执行它以获取您可以在下面看到的错误:
Zygote D Process 25471 terminated by signal (11)
295 InputDispatcher W channel '2c2e20a8 com.pligor.test/activities.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
295 InputDispatcher E channel '2c2e20a8 com.pligor.test/activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
295 dalvikvm D GC_FOR_ALLOC freed 1299K, 21% free 13252K/16583K, paused 93ms
295 InputDispatcher W Attempted to unregister already unregistered input channel '2c2e20a8 com.pligor.test/activities.MainActivity (server)'
295 BluetoothService D Tracked app 25471 diedType:10
295 BluetoothService D Removing service record 10009 for pid 25471
132 SurfaceFlinger D Release buffer at 0x61c08
295 WindowManager I WINDOW DIED Window{2c2e20a8 com.pligor.test/activities.MainActivity paused=false}
295 ActivityManager I Process com.pligor.test (pid 25471) has died.
295 ActivityManager W Force removing ActivityRecord{2c021800 com.pligor.test/activities.MainActivity}: app died, no saved state
295 WindowManager W Failed looking up window
295 WindowManager W java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@2bf3e798 does not exist
295 WindowManager W at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7165)
295 WindowManager W at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7156)
295 WindowManager W at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1545)
295 WindowManager W at android.os.BinderProxy.sendDeathNotice(Binder.java:417)
295 WindowManager W at dalvik.system.NativeStart.run(Native Method)
295 WindowManager I WIN DEATH: null
295 BluetoothEventLoop D Property Changed: UUIDs : 11
295 hAdapterStateMachine D BluetoothOn process message: 51
295 InputManagerService W Got RemoteException sending setActive(false) notification to pid 25471 uid 10040
Run Code Online (Sandbox Code Playgroud)
注意:由信号(11)终止的过程意味着分段错误(http://en.wikipedia.org/wiki/SIGSEGV).
编辑
我使用以下代码()创建蓝牙服务器套接字Scala:
private val bluetoothServerSocket: BluetoothServerSocket = try {
bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(MY_SERVICE_NAME_INSE??CURE, MY_UUID_INSECURE);
}
catch {
case e: IOException => throw new ServerSocketException;
}
Run Code Online (Sandbox Code Playgroud)
我用这段代码关闭蓝牙插座:
try {
isCancelled = true;
bluetoothServerSocket.close();
} catch {
case e: IOException => throw new NotClosedException;
}
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,问题的根本原因是多次在套接字上调用close.为了解决这个问题,我将蓝牙套接字包装在一个特殊的类中,以防止关闭方法被多次调用.
请注意,关闭蓝牙套接字创建的流能够在套接字上调用close.以下解决了这个问题.
public class CloseOnceBluetoothSocket
{
private final BluetoothSocket mSocket;
private boolean mIsClosed;
public CloseOnceBluetoothSocket(BluetoothSocket socket)
{
this.mSocket = socket;
}
public void connect() throws IOException
{
mSocket.connect();
}
public InputStream getInputStream() throws IOException
{
return new FilterInputStream(mSocket.getInputStream()) {
@Override
public void close() throws IOException
{
CloseOnceBluetoothSocket.this.close();
}
};
}
public OutputStream getOutputStream() throws IOException
{
return new FilterOutputStream(mSocket.getOutputStream()) {
@Override
public void close() throws IOException
{
CloseOnceBluetoothSocket.this.close();
}
};
}
public void close() throws IOException
{
synchronized (mSocket) {
if (!mIsClosed) {
mSocket.close();
mIsClosed = true;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想我已经找到了解决这个问题的方法。“我认为”是因为这还没有在许多设备上进行过测试。
下面的代码是Scala
对于解决方法,我利用具有超时的重载方法accept(int)
所以我们有一个变量来表示您在下面看到的无限循环的状态
private var toContinue = true;
Run Code Online (Sandbox Code Playgroud)
我们只是在 while 循环中重复接受
while (toContinue) {
try {
//this is a blocking call and will only return on a successful connection or an exception, or on timeout
val socket = bluetoothServerSocket.accept(10000); //msec
connectionAccepted(socket);
} catch {
case e: IOException => {
Logger("accepting timed out");
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在我们不调用,而是bluetoothServerSocket.close()将变量设置为 false
def cancel() {
toContinue = false;
}
Run Code Online (Sandbox Code Playgroud)
实际代码有点复杂,因为您需要一个回调函数在 while 循环退出时执行某些操作,但主要问题已解决,如上所示