Java 蓝牙连接错误
我对android编程有点陌生,但现在我想尝试编写一个应用程序,通过蓝牙将我的手机(三星galaxy note 2)与电路板连接起来。我只想打开/关闭一些LED。我正在电路板上使用bc417调制解调器 我已经搜索了蓝牙示例,但它们似乎都很难,我只需要所需的命令。但是现在,当我在ListView上选择一个项目时,我的应用程序总是崩溃 有人能帮我吗Java 蓝牙连接错误,java,android,bluetooth,connection,Java,Android,Bluetooth,Connection,我对android编程有点陌生,但现在我想尝试编写一个应用程序,通过蓝牙将我的手机(三星galaxy note 2)与电路板连接起来。我只想打开/关闭一些LED。我正在电路板上使用bc417调制解调器 我已经搜索了蓝牙示例,但它们似乎都很难,我只需要所需的命令。但是现在,当我在ListView上选择一个项目时,我的应用程序总是崩溃 有人能帮我吗 package com.test.bluetoothtest; import java.io.IOException; import java.uti
package com.test.bluetoothtest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
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.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.bluetooth.BluetoothSocket;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private static final UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
Toast.makeText(MainActivity.this, "Bluetooth couldn't be started.", Toast.LENGTH_SHORT).show();
}
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast.makeText(MainActivity.this, "Bluetooth started succesfully", Toast.LENGTH_SHORT).show();
}
List<String> devicelist = new ArrayList<String>();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
devicelist.add(device.getName());
}
}
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, devicelist);
final ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
try
{
Toast.makeText(MainActivity.this, lv.getAdapter().getItem(arg2).toString(), Toast.LENGTH_SHORT).show();
BluetoothDevice finaldevice = (BluetoothDevice) lv.getAdapter().getItem(arg2);
BluetoothSocket clientSocket = finaldevice.createRfcommSocketToServiceRecord(myuuid);
clientSocket.connect();
Toast.makeText(MainActivity.this, "Connectin successful!", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
很高兴知道你的应用程序在哪里崩溃。LogCat也可能会有所帮助。 因为我自己也有一些UUID的问题,所以我和
Method m = finaldevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
clientSocket = (Bluetoothsocket) m.invoke(finaldevice, Integer.valueOf(1));
clientSocket.connect();
这对我很管用。你的问题是以下几行
BluetoothDevice finaldevice = (BluetoothDevice) lv.getAdapter().getItem(arg2);
getItem返回的字符串不是Bluetooth设备。你的演员阵容失败了
执行以下操作
您可以查询配对的设备并在listview中显示它们。从配对设备中,您需要其Mac地址才能创建连接。当您在成对的设备中进行迭代时,可以提取每个设备的地址。在稍后的单击中,执行以下操作以获得Bluetooth设备
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
然后使用以下命令获取BluetoothSocket
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
然后你可以用这个插座连接
btSocket.connect();
欢迎来到Stackoverflow。我知道你是新来的,但请记住,你的一些研究对你有很大帮助。你犯了什么错误?请先表现出你的努力,这样其他人可能会帮助你。另外,请阅读,eclipse告诉我在您的代码周围添加一些catch子句,但它仍然不起作用:(,如果我单击listview中的某个项目,我的应用程序就会崩溃,这意味着在公共窗口的某个地方,单击功能好的,这是有意义的。但是我如何获得BluetoothDevice呢?
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();