我想将bT模块连接到我的Android手机。但是当我运行我的代码时,这个错误就来了我/编舞:跳过了439帧!应用程序可能在其主线程上做了太多工作。
package com.example.hp.classcircuit;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
//decleare valiables
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mDevice;
ListView listView;
ConnectThread mConnectThread;
ArrayList pdal;
String DEVICE_ADDRESS;
boolean found=false;
BluetoothManager BM;
Set<BluetoothDevice> pairedDevices;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)//for chacking sutable
//API
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);//Listview in xml
//call BT Manager
BM= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
//call BT Adaptor
mBluetoothAdapter=BM.getAdapter();
//call paired device
pairedDevices = mBluetoothAdapter.getBondedDevices();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(MainActivity.this, "Device not support bluetooth", Toast.LENGTH_LONG).show();
}
//check BT is on or off
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
pdal=new ArrayList();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
pdal.add(device.getName());
}
ArrayAdapter pda=new ArrayAdapter(this, android.R.layout.simple_list_item_1,pdal);
listView.setAdapter(pda);
}
listView.setOnItemClickListener(myListClickListener);
}
**
//动作点击listnor for listview
private AdapterView.OnItemClickListener myListClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String DeviceName=pdal.get(i).toString();
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals(DeviceName)) {
DEVICE_ADDRESS=device.getAddress();
Log.i(device.getName(), DEVICE_ADDRESS);
mDevice=device;
found=true;
break;
}
}
mConnectThread = new ConnectThread(mDevice);
//call the inner class method
mConnectThread.doInBackground();
}
};
//内部课程
public class ConnectThread extends AsyncTask<String, Integer, Long> {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
//constructor of inner class
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
//override the methode
protected Long doInBackground(String... strings) {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
Log.i("Status","connected");
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return null;
}
return null;
}
//function for close the socket
public void close() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}
以上是我的异步课。我没有发现错误,请指导我错误的地方。感谢所有
答案 0 :(得分:1)
不要打电话
mConnectThread.doInBackground();
你所做的只是破坏了AsyncTask
的概念,并完成了UI线程上的所有工作。
必须使用线程规则
此类必须遵循一些线程规则 正常工作:
- 必须在UI线程上加载AsyncTask类。这是从JELLY_BEAN开始自动完成的。
- 必须在UI线程上创建任务实例。
必须在UI线程上调用- execute(Params ...)。
- 不调用 onPreExecute(),onPostExecute(Result), doInBackground(参数...),onProgressUpdate(进度...)手动强>
- 任务只能执行一次(如果尝试第二次执行,则会抛出异常。)
AsyncTask
调用 execute()
。所以将上面的行改为:
mConnectThread.execute();
请注意您定义ConnectThread
预期某些String
参数 - extends AsyncTask<String, Integer, Long>
的方式,但您从未在代码中使用此类参数。这些参数可以作为execute(String ... params)
方法中的参数传递。最好将签名更改为:
public class ConnectThread extends AsyncTask<Void, Integer, Long> {
protected Long doInBackground(Void... params) {
//your code here
}
}