【发布时间】:2014-11-25 17:47:55
【问题描述】:
我正在创建一个可以向蓝牙设备发送数据的应用程序。 我使用以下代码创建和连接套接字:
package com.example.bluetooth;
import java.io.IOException;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bd = ba.getRemoteDevice("20:13:10:15:39:84");
Toast.makeText(getApplicationContext(), bd.getName(), Toast.LENGTH_SHORT).show();
BluetoothSocket bs = null;
try{
bs = bd.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
catch(IOException io){
Toast.makeText(getApplicationContext(), "Socket Create : " + io.toString() , Toast.LENGTH_SHORT).show();
}
try{
ba.cancelDiscovery();
bs.connect();
}
catch(IOException io){
Log.e("Socket Connect", io.toString());
Toast.makeText(getApplicationContext(), "Socket Connect : " + io.toString() , Toast.LENGTH_LONG).show();
}
}
}
我的问题是套接字没有被连接。显示的消息是“java.io.IOException: [JSR82] connect: Connection is not created (failed or aborted).”
我使用的是 android 4.2 联想设备。
使用的蓝牙模块是HC-05,微控制器是Arduino-Uno。
我参考过类似的帖子,但没有一个可以解决我的问题。
提前致谢
【问题讨论】:
标签: android sockets arduino android-bluetooth