我正在创建一个可以将数据发送到蓝牙设备的应用程序。我使用以下代码来创建和连接套接字:
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:连接未创建(失败或中止)“。
我使用的是android 4.2联想设备。
所用蓝牙模块为HC-05,微控制器为Arduino-Uno。
我也参考过类似的帖子,但都不能解决我的问题。
提前感谢
发布于 2014-04-05 09:51:08
我首先重启了我的平板电脑,然后尝试使用我的代码连接蓝牙插座。我只需要使用:
public void onStop(){
super.onStop();
mSocket.close();
mOutputStream.close();
}在最后。
啊,真灵!
问题是我从来没有尝试在结束时关闭套接字。
发布于 2014-11-25 17:02:09
我使用了这段代码来稳定地连接到蓝牙设备(即蓝牙打印机)。现在它连接了10次中的9.9次,如果仍然发生一些错误,我会重新编程重置我的蓝牙,再次调用这段代码,然后它10次中连接10次。
public boolean connectToPrinter(String printerName) throws IOException
{
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
BluetoothDevice device = getPrinterByName(printerName);
if (bluetoothSocket != null)
{
bluetoothSocket.close();
}
try {
Method m=device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
bluetoothSocket= (BluetoothSocket) m.invoke(device, 1);
} catch (Exception e) {
e.printStackTrace();
}
if (bluetoothSocket == null)
return false;
bluetoothSocket.connect();
return true;
}下面是getPrinterByName()的代码:
private BluetoothDevice getPrinterByName(String printerName)
{
Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
for (BluetoothDevice device : pairedDevices)
{
Log.e("","device name: "+device.getName());
if (device.getName() == null)
continue;
if (device.getName().contains(printerName))
{
remoteDevice = device;
// pairPrinter(printerName);
return remoteDevice;
}
}
return null;
}发布于 2014-04-04 19:42:41
首先从您的手机设置配对arduino蓝牙。然后尝试从你的应用程序连接到蓝牙。试试下面的代码:
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mBluetoothDevice;
private BluetoothSocket mBluetoothSocket;
private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
try{
if(mBluetoothAdapter == null){
Log.d("bluetooth:", "device does not support bluetooth");
}
if(!mBluetoothAdapter.isEnabled()){
Intent enableBt = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(enableBt);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}对于连接:
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("xx:xx:xx:xx:xx:xx");
try {
mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(uuid);
mBluetoothSocket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}https://stackoverflow.com/questions/22832198
复制相似问题