我正在创建一个可以将数据发送到蓝牙设备的应用程序。 我使用以下代码创建并连接socket:
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();
}
}
}
我的问题是socket没有连接。显示的消息是“java.io.IOException:[JSR82] connect:未创建连接(失败或中止)。”
我使用的是Android 4.2联想设备。
使用的蓝牙模块是HC-05,微控制器是Arduino-Uno。
我已经提到类似的帖子,但没有一个可以解决我的问题。
先谢谢
答案 0 :(得分:4)
我已经使用这段代码使我与蓝牙设备(即蓝牙打印机)的连接稳定。现在它连接了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;
}
答案 1 :(得分:3)
我首先重新启动平板电脑,然后尝试使用我的代码连接蓝牙套接字。我只需要使用:
public void onStop(){
super.onStop();
mSocket.close();
mOutputStream.close();
}
最后。
有效!
问题是我从未试图在最后关闭套接字。
答案 2 :(得分:0)
首先将手机设置中的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();
}