Android 连接到安卓蓝牙插座
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20009565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect to Android Bluetooth socket
提问by user2744785
Hi I am trying to create an Android app which will connect to a Blue SMiRF Bluetooth dongle which I want to send data to. I have read over the developer pages and looked at multiple different examples however I am currently having trouble creating a connection to the socket. The Bluetooth portion of the code is pretty much from an example that I was able to find. When trying to connect to the Bluetooth dongle the app gets a force close because there is some error I am not handling correctly. However I have also tried to use the app just to connect to another PC and the connection wont get established correctly for some reason even though I am already paired with the device through the Bluetooth settings before I even run the app. I have posted some of the more important code below for where I think my issue may be. Any help will be very appreciated, please let me know if I should post any additional code.
嗨,我正在尝试创建一个 Android 应用程序,该应用程序将连接到我想向其发送数据的 Blue SMiRF 蓝牙加密狗。我已经阅读了开发人员页面并查看了多个不同的示例,但是我目前无法创建到套接字的连接。代码的蓝牙部分几乎来自我能够找到的示例。当尝试连接到蓝牙加密狗时,应用程序会强制关闭,因为我没有正确处理一些错误。但是,我也尝试使用该应用程序只是为了连接到另一台 PC,并且由于某种原因无法正确建立连接,即使在运行该应用程序之前我已经通过蓝牙设置与设备配对。我在下面发布了一些更重要的代码,说明我认为我的问题可能出在哪里。
protected void connect(BluetoothDevice device) {
//BluetoothSocket socket = null;
try {
//Create a Socket connection: need the server's UUID number of registered
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
socket.connect();
Log.d("EF-BTBee", ">>Client connectted");
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });
new Thread() {
public void run() {
while(true)
{
try {
Log.d("EF-BTBee", ">>Send data thread!");
OutputStream outputStream = socket.getOutputStream();
outputStream.write(new byte[] { (byte) 0xa2, 0, 7, 16, 0, 4, 0 });
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
}
}
};
}.start();
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
} finally {
if (socket != null) {
try {
Log.d("EF-BTBee", ">>Client Close");
socket.close();
finish();
return ;
} catch (IOException e) {
Log.e("EF-BTBee", "", e);
}
}
}
}`
I have also tried using
我也试过使用
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
instead of just the "socket =" line from above and still had no success.
而不仅仅是上面的“socket =”行,但仍然没有成功。
回答by bourax webmaster
if the device is already paired , then you can use
如果设备已经配对,那么您可以使用
if(device.getBondState()==device.BOND_BONDED){
Log.d(TAG,device.getName());
//BluetoothSocket mSocket=null;
try {
mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.d(TAG,"socket not created");
e1.printStackTrace();
}
try{
mSocket.connect();
}
catch(IOException e){
try {
mSocket.close();
Log.d(TAG,"Cannot connect");
} catch (IOException e1) {
Log.d(TAG,"Socket not closed");
e1.printStackTrace();
}
}
for the MY_UUID use
对于 MY_UUID 使用
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
回答by Gilad Haimov
Assuming (check!) the UUID you are using is indeed the server UUID, your best bet would be to attempt insecure mode of Bluetooth communication. Some devices simply do not support secure mode.
假设(检查!)您使用的 UUID 确实是服务器 UUID,您最好的办法是尝试不安全的蓝牙通信模式。有些设备根本不支持安全模式。
To do that duplicate your flow, replacing createRfcommSocketToServiceRecord with createInsecureRfcommSocketToServiceRecord and "createRfcommSocket" with "createInsecureRfcommSocket".
要做到这一点,请复制您的流程,将 createRfcommSocketToServiceRecord 替换为 createInsecureRfcommSocketToServiceRecord,将“createRfcommSocket”替换为“createInsecureRfcommSocket”。
Or better still: use BTWizwhich encapsulate this flow inside. BTWiz also provides a simple async API for Bluetooth IO.
或者更好:使用BTWiz将这个流程封装在里面。BTWiz 还为蓝牙 IO 提供了一个简单的异步 API。
Finally - socket creation & connect()ion must never be performed on UI thread. Your code is not conclusive here but I suspect you will need to fix this as well.
最后 - 永远不能在 UI 线程上执行套接字创建和 connect()ion。你的代码在这里不是决定性的,但我怀疑你也需要解决这个问题。
Gilad Haimov
吉拉德·海莫夫
www.mobileedge.co.il
www.mobileedge.co.il
回答by user2744785
Have finally found a work around. Don't know why this has to be done to work properly.
终于找到了解决办法。不知道为什么必须这样做才能正常工作。
IOException: read failed, socket might closed - Bluetooth on Android 4.3