我正在寻找不安全地连接rfcomm socket的方法.我能够找到下面提到的方法
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
Run Code Online (Sandbox Code Playgroud)
这暂时正在做我想做的事.即使是这里的文档也说我们需要使用createInsecureRfcommSocketToServiceRecord来进行不安全的连接.但是没有这样的方法.我发现的唯一方法是使用如上所示的反射.甚至在createInsecureRfcommSocket中传递的方法,而不是createInsecureRfcommSocketToServiceRecord.我只是想知道这种方式有多可靠.如果我在方法中提到createInsecureRfcommSocketToServiceRecord,则连接永远不会发生.
我正在尝试创建一个基本的蓝牙应用程序,用于测试设备.
我从developer.android获得了代码.这是链接:http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices
这是我的线程代码的一部分:
public void run() {
mBluetoothAdapter.cancelDiscovery();
Log.i(TAG, "Discovery Cancel!");
try {
Log.i(TAG, "Connection Started");
mmSocket.connect();
Log.i(TAG, "Connection Ended");
} catch (IOException e) {
try {
Log.e(TAG, "Connection Failed", e);
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "Connection Close Failed", e2);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试mmSocket.connect();过什么都行不通.总是抛出IOException并从logcat中获取该日志:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)
我看了这些文章,并尝试了写的东西,没有一个解决了我的问题.
Android蓝牙java.io.IOException:连接被拒绝了?
顺便说一句,我正在研究android ics 4.0.4.
我知道这不是设备问题,因为我在不同的设备上尝试过这个应用程序.
我正在做一个Android应用程序,我需要2台或更多设备才能通过蓝牙连接到一台设备.以对等形式将两个设备连接在一起的代码有效,但是当我尝试连接另一个时,我得到一个IOException,说"连接被拒绝",因为Socket已关闭,因此无法完成配对.错误如下所示.
Socket closed. Unable to complete pairing.
java.io.IOException: Connection refused
at android.bluetooth.BluetoothSocket.connectNative(Native Method)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
at com.ablueworld.androidgridcomputingframework.BluetoothManager$ConnectAsyncTask.doInBackground(BluetoothManager.java:270)
at com.ablueworld.androidgridcomputingframework.BluetoothManager$ConnectAsyncTask.doInBackground(BluetoothManager.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Could not connect to this device
Run Code Online (Sandbox Code Playgroud)
我已经读过它需要为每个连接提供不同的UUID,是吗?无论如何,我已经编写了如下所示的代码,为每个新连接从数组中获取不同的UUID.
// Unique UUID for this application
private static int indexUUID = 0;
private static final UUID[] MY_UUID_SECURE = {UUID.fromString("98b97e6b-62ff-4a36-a81b-82256fd1d168"),
UUID.fromString("98b97e6b-62ff-4a36-a81b-82256fd1d169"),
UUID.fromString("98b97e6b-62ff-4a36-a81b-82256fd1d170"),
UUID.fromString("98b97e6b-62ff-4a36-a81b-82256fd1d171"),
UUID.fromString("98b97e6b-62ff-4a36-a81b-82256fd1d172")};
private static final String NAME_SECURE = "GridFrameworkSecure";
/**
* Method to connect securely to Bluetooth device using it's MAC …Run Code Online (Sandbox Code Playgroud) 我有一个支持OBEX对象推送配置文件的设备,此配置文件基于串行端口配置文件.我的猜测是我可以使用Android蓝牙聊天示例将此设备连接到我的Android手机.但是我遇到了一个问题,关于socket.accept()android SDK中的功能.我尝试完成将手机连接到此设备,如下所示:
adapter = BluetoothAdapter.getDefaultAdapter();
device = adapter.getRemoteDevice("00:1B:DC:0F:EC:7E");
AcceptThread = new AcceptThread(true, adapter, device);
AcceptThread.start();
Run Code Online (Sandbox Code Playgroud)
AcceptThread中的构造函数编码如下:
public AcceptThread(boolean secure, BluetoothAdapter adapter, BluetoothDevice device) {
BluetoothServerSocket tmp = null;
this.adapter = adapter;
this.device = device;
// Create a new listening server socket
try {
tmp = adapter.listenUsingInsecureRfcommWithServiceRecord(device.getName(), UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
} catch (Exception e) {
Log.e(TAG, ".AcceptThread # listen() failed", e);
}
mmServerSocket = tmp;
}
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试connect()按照我之前说过做的时候
public void run() {
BluetoothSocket socket = null;
// Listen to the …Run Code Online (Sandbox Code Playgroud)