"服务发现失败"从Android的蓝牙不安全RFCOMM [英] "Service discovery failed" from Android Bluetooth Insecure Rfcomm
问题描述
有谁知道如何创建在API级别2.3.3在使用任意声明的服务名称?(不是随机的或改变服务的名称,只是一个服务名2 Android设备之间的不安全RFCOMM连接我定义我自己)
Does anyone know how to create an insecure RFCOMM connection between 2 Android devices at API level 2.3.3 while using an arbitrarily declared service name? (not random or changing service name, just a service name that I define myself)
我想创建2 Android设备之间的不安全RFCOMM连接:Droid的X2和华硕变压器。我假设这两种器件都具有的功能在安卓2.3.3的水平,实际上得到使用不安全的RFCOMM的能力。
I am trying to create an insecure Rfcomm connection between 2 Android devices: Droid X2 and an Asus Transformer. I am assuming that both of these devices have functionality at the level of Android 2.3.3 to actually gain the ability to use insecure Rfcomm.
当我尝试创建蓝牙连接如<一个href="http://stackoverflow.com/questions/5308373/how-to-create-insecure-rfcomm-socket-in-android">here,用现在的公共createInsecureRfcommSocketToServiceRecord()和listenUsingInsecureRfcommWithServiceRecord(SERVICE,UUID),我得到一个报道:
When I try to create the Bluetooth connection as described here, using the now public createInsecureRfcommSocketToServiceRecord() and listenUsingInsecureRfcommWithServiceRecord(SERVICE, UUID), I get a reported:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201)
at com.s4d.bluenomad.DeviceConnections$ClientThread.run(DeviceConnections.java:406)
我发现了一个<一个href="http://stackoverflow.com/questions/8282481/android-bluetooth-connection-secure-insecure">related问题如果有人创造一个正常的连接被得到这个错误,并使用反射来调用一个私有方法。不过,我不知道什么私人方法现在将对应于发起不安全连接。我试着用在相关的问题提出的解决方案,但我问的Android配对设备这正是我需要避免。我真的需要不安全的做法。
I found a related question where someone creating a normal connection was getting this error and used reflection to invoke a private method. However, I have no idea what private method would now correspond to initiating an "insecure" connection. I tried using the solution proposed in that related question, but I am asked by Android to pair the devices which is exactly what I need to avoid. I really do need the insecure approach.
我甚至尝试了正式的组合和黑客攻击的解决方案概述<一href="http://stackoverflow.com/questions/8282481/android-bluetooth-connection-secure-insecure">here
I even tried a combination of the official and hacked solutions outlined here
创建ServerThread侦听连接
Log.i(TAG, "Constructing a ServerThread");
// Use a temporary object that is later assigned to serverSocket,
// because serverSocket is final
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = btAdapter.listenUsingInsecureRfcommWithServiceRecord(SERVICE_NAME, SERVICE_UUID);
Log.i(TAG,"Started listening on insecure RFCOMM channel for service requests for: " + SERVICE_NAME);
} catch (IOException e) { }
serverSocket = tmp;
ServerThread监听连接
BluetoothSocket socket;
while(true)
{
try
{
socket = serverSocket.accept();
}
catch( IOException e)
{
break;
}
if( socket != null )
{
Log.i(TAG, "Received new socket connection requesting service: " + SERVICE_NAME);
}
else
{
Log.i(TAG, "Socket connection attempted, but socket received is NULL.");
}
}
创建ClientThread来发起连接
Log.i(TAG, "Constructing a ClientThread");
BluetoothSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createInsecureRfcommSocketToServiceRecord(SERVICE_UUID);
Log.i(TAG,"Created client socket on insecure RFCOMM channel for service requests for: " + SERVICE_NAME);
}
catch (IOException e)
{
Log.i(TAG, "Failed to createInsecureRfcommSocket() against MAC: " + device.getAddress());
}
clientSocket = tmp;
ClientThread连接到服务器
try
{
clientSocket.connect();
}
catch( final IOException e)
{
DeviceConnections.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
console.append("Client unable to connect to service.");
Log.i(TAG, "Client socket unable to connect() to: " + clientSocket.getRemoteDevice().getAddress());
e.printStackTrace();
}
});
}
我得到的日志输出客户端套接字无法连接()到:[MY_MAC_ADDRESS],然后我得到的堆栈跟踪为服务发现失败异常
I do get the log output "Client socket unable to connect() to: [MY_MAC_ADDRESS]", then I get the stacktrace for the "Service discovery failed" exception.
推荐答案
它出现的问题是,之前我叫
It appears the problem was that before I called
clientSocket.connect()
我需要调用
btAdapter.cancelDiscovery()
我在文档看到了这一点,但它也被列为表演的问题 - 在我的情况看来,这实际上是功能性的问题。有一次,我添加了取消发现呼叫,插座连接马上工作。
I had seen this in documentation but it was listed as a "performance" issue - in my case it seems that this was actually a "functional" issue. Once I added the cancel discovery call, the socket connection worked immediately.
这篇关于&QUOT;服务发现失败&QUOT;从Android的蓝牙不安全RFCOMM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!