Android蓝牙应用程序进程被终止
我对android编程非常陌生。我有两个班:main和btmanager。当我试着在手机上测试我的应用程序时,我得到的只是一条信息,procees被杀了。我做错了什么 代码实现: 主要类别:Android蓝牙应用程序进程被终止,android,bluetooth,connection,Android,Bluetooth,Connection,我对android编程非常陌生。我有两个班:main和btmanager。当我试着在手机上测试我的应用程序时,我得到的只是一条信息,procees被杀了。我做错了什么 代码实现: 主要类别: public class MainActivity extends Activity { BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter(); Btmanager manager; @Override publi
public class MainActivity extends Activity {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
Btmanager manager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (bluetooth == null)
{
Toast.makeText(this, "Bluetooth is not enabled on this device", Toast.LENGTH_LONG).show();
System.exit(0);
}
}
@Override
public void onStart()
{
super.onStart();
if (!bluetooth.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 2);
}
manager.run();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void closeApp (View view)
{
System.exit(0);
}
}
Btmanager类:
public class Btmanager extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public static final UUID myUUID = UUID.fromString("0x1101");
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
public Btmanager(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(myUUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
bluetooth.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
我在您的代码中看到了两个问题:
Btmanager
对象,当您调用run
时,它仍然是null
。(将导致NullPointerException
-您的应用程序将崩溃)Btmanager
的run
方法,而不是start
方法。如果希望run
方法中的代码在新线程中运行,则必须调用start
。调用run
将使其在相同的线程中运行。这会阻塞UI线程,如果阻塞时间过长,可能会导致应用程序崩溃出于测试目的,我不使用BTmanager类-在onStart()方法中,我添加了带有连接实现的新线程,但仍然没有任何结果-应用程序崩溃
public void onStart()
{
super.onStart();
if (!bluetooth.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 2);
}
new Thread(new Runnable()
{
public void run()
{
try {
//Create a Socket connection: need the server's UUID number of registered
socket = device.createRfcommSocketToServiceRecord(myUUID);
socket.connect();
Log.d("EF-BTBee", "Connectted");
}
catch (IOException e)
{
Log.e("EF-BTBee", "Error : ", e);
}
}
}).start();
}
您是否已在AndroidManifest.xml中授予蓝牙权限?是。复制了我的应用程序清单文件:好的,在设备中运行应用程序,同时连接USB并上传错误日志,好的,现在我通过构造函数创建Btmanager对象,但是albo对线程有一个问题。我必须调用方法“start”而不是“run”?它可能是错误的UUID吗?我使用BTM222模块。在Windows中,我发现UUID
0x1101
,但谷歌说00001101-0000-1000-8000-00805F9B34FB
?