安卓 java。如果在蓝牙扫描期间滚动lang.IllegalStateException
我正在尝试制作一个应用程序来扫描蓝牙设备。如果我在扫描过程中滚动,应用程序就会崩溃。否则,如果我把它留到扫描周期,它就可以正常工作
以下是我的主要活动:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner mBluetoothLeScanner;
private boolean mScanning;
private static final int ENABLE_BLUETOOTH = 1;
private static final int ENABLE_LOCATION = 255;
Button btnScan;
ListView listViewLE;
List<BluetoothDevice> listBluetoothDevice;
ListAdapter adapterLeScanResult;
private Handler mHandler;
private static final long SCAN_PERIOD = 7500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//check BLE supported on this device or not
if(!getPackageManager().hasSystemFeature(getPackageManager().FEATURE_BLUETOOTH_LE)){
Toast.makeText(this,"Bluetooth Low Energy is not supported.", Toast.LENGTH_SHORT).show();
finish();
}
//get bluetooth adapter and le scanner
btAdapterAndleScanner();
//check if bluetooth is supported on this device
if(mBluetoothAdapter == null){
Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_SHORT).show();
finish();
return;
}
//check for location permission
locationPermission();
btnScan = (Button)findViewById(R.id.scan_button);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanLeDevice(true);
btnScan.setVisibility(View.GONE);
}
});
listViewLE = (ListView)findViewById(R.id.device_list);
listBluetoothDevice = new ArrayList<BluetoothDevice>();
adapterLeScanResult = new ArrayAdapter<BluetoothDevice>(this,安卓.R.layout.simple_expandable_list_item_1, listBluetoothDevice);
listViewLE.setAdapter(adapterLeScanResult);
listViewLE.setOnItemClickListener(scanResultOnItemClickListener);
mHandler = new Handler();
}
AdapterView.OnItemClickListener scanResultOnItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final BluetoothDevice device = (BluetoothDevice) parent.getItemAtPosition(position);
//TODO: Add bondstate to check whether the device is bonded or not.
String msg = "Address: " + device.getAddress() + "\n" + "Type: " + getBTDeviceType(device);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Name: " + device.getName())
.setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
};
private String getBTDeviceType(BluetoothDevice d){
String type = "";
switch (d.getType()){
case BluetoothDevice.DEVICE_TYPE_CLASSIC:
type = "Classic";
break;
case BluetoothDevice.DEVICE_TYPE_DUAL:
type = "Dual";
break;
case BluetoothDevice.DEVICE_TYPE_LE:
type = "Low Energy";
break;
case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
type = "Unknown";
break;
default:
type = "unknown...";
}
return type;
}
@Override
protected void onResume(){
super.onResume();
//TODO: use if condition twice to check how it differes from using it from the first time.
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BLUETOOTH);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == ENABLE_BLUETOOTH && resultCode == Activity.RESULT_CANCELED){
finish();
return;
}
//TODO: get bluetooth adapter and lescan function to see what happens. go without it first.
//TODO: check bluetooth supported or not. no idea why!!!
super.onActivityResult(requestCode, resultCode, data);
}
private void btAdapterAndleScanner(){
final BluetoothManager mBluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
mScanning = false;
}
private void scanLeDevice(final boolean enable){
if (enable){
listBluetoothDevice.clear();
listViewLE.invalidateViews();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeScanner.stopScan(scanCallback);
listViewLE.invalidateViews();
Toast.makeText(MainActivity.this, "Scan timeout", Toast.LENGTH_SHORT).show();
mScanning = false;
btnScan.setEnabled(true);
}
},SCAN_PERIOD);
mBluetoothLeScanner.startScan(scanCallback);
mScanning = true;
btnScan.setEnabled(false);
} else {
mBluetoothLeScanner.stopScan(scanCallback);
mScanning = false;
btnScan.setEnabled(true);
}
}
private ScanCallback scanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
addBluetoothDevice(result.getDevice());
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
for (ScanResult result : results){
addBluetoothDevice(result.getDevice());
}
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
Toast.makeText(MainActivity.this, "onScanFailed: " + String.valueOf(errorCode),Toast.LENGTH_LONG).show();
}
private void addBluetoothDevice(BluetoothDevice device){
if(!listBluetoothDevice.contains(device)){
listBluetoothDevice.add(device);
listViewLE.invalidateViews();
}
}
};
@TargetApi(23)
private void locationPermission() {
if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION) != PERMISSION_GRANTED) {
requestPermissions(new String[]{ACCESS_COARSE_LOCATION}, ENABLE_LOCATION);
}
}
}
这是我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
tools:context=".MainActivity">
<ListView
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:id="@+id/device_list"/>
<Button
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:text="Scan"
安卓:id="@+id/scan_button"
安卓:layout_alignParentBottom="true"/>
</RelativeLayout>
下面是日志:
2019-09-30 15:38:29.036 2786-2786/com.example.v_daq_re E/InputEventReceiver: Exception dispatching input event.
2019-09-30 15:38:29.037 2786-2786/com.example.v_daq_re E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2019-09-30 15:38:29.043 2786-2786/com.example.v_daq_re E/MessageQueue-JNI: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230812, class 安卓.widget.ListView) with Adapter(class 安卓.widget.ArrayAdapter)]
at 安卓.widget.ListView.layoutChildren(ListView.java:1810)
at 安卓.widget.AbsListView.onTouchMove(AbsListView.java:6514)
at 安卓.widget.AbsListView.onTouchEvent(AbsListView.java:6368)
at 安卓.widget.ListView.onTouchEvent(ListView.java:1705)
at 安卓.view.View.dispatchTouchEvent(View.java:13484)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3216)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2888)
at 安卓.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6285)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.安卓.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.安卓.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at 安卓.app.Activity.dispatchTouchEvent(Activity.java:3487)
at 安卓x.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.安卓.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at 安卓.view.View.dispatchPointerEvent(View.java:13732)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6119)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5897)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at 安卓.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5524)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at 安卓.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5581)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8408)
at 安卓.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8341)
at 安卓.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8294)
at 安卓.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8523)
at 安卓.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at 安卓.os.MessageQueue.nativePollOnce(Native Method)
at 安卓.os.MessageQueue.next(MessageQueue.java:326)
at 安卓.os.Looper.loop(Looper.java:181)
at 安卓.app.ActivityThread.main(ActivityThread.java:7037)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494
2019-09-30 15:38:29.043 2786-2786/com.example.v_daq_re D/AndroidRuntime: Shutting down VM
2019-09-30 15:38:29.045 2786-2786/com.example.v_daq_re E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.v_daq_re, PID: 2786
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230812, class 安卓.widget.ListView) with Adapter(class 安卓.widget.ArrayAdapter)]
at 安卓.widget.ListView.layoutChildren(ListView.java:1810)
at 安卓.widget.AbsListView.onTouchMove(AbsListView.java:6514)
at 安卓.widget.AbsListView.onTouchEvent(AbsListView.java:6368)
at 安卓.widget.ListView.onTouchEvent(ListView.java:1705)
at 安卓.view.View.dispatchTouchEvent(View.java:13484)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3216)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2888)
at 安卓.widget.AbsListView.dispatchTouchEvent(AbsListView.java:6285)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at 安卓.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3222)
at 安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2904)
at com.安卓.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:697)
at com.安卓.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1879)
at 安卓.app.Activity.dispatchTouchEvent(Activity.java:3487)
at 安卓x.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.安卓.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:655)
at 安卓.view.View.dispatchPointerEvent(View.java:13732)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6119)
at 安卓.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5897)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at 安卓.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5524)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at 安卓.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5581)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5399)
at 安卓.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5365)
at 安卓.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5373)
at 安卓.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5346)
at 安卓.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8408)
at 安卓.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8341)
at 安卓.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8294)
at 安卓.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8523)
at 安卓.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:198)
at 安卓.os.MessageQueue.nativePollOnce(Native Method)
at 安卓.os.MessageQueue.next(MessageQueue.java:326)
at 安卓.os.Looper.loop(Looper.java:181)
at 安卓.app.ActivityThread.main(ActivityThread.java:7037)
2019-09-30 15:38:29.045 2786-2786/com.example.v_daq_re E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:965)
该应用程序本身运行良好。唯一的问题是在扫描过程中向下滚动。如果有人能找出问题并让我知道如何解决它,这将是很有帮助的。谢谢
# 1 楼答案
编辑: 正如@Codo所评论的,在这种情况下,只有
notifyDataSetChanged()
没有被调用修改
listBluetoothDevice
后在适配器上调用notifyDataSetChanged()
如果从另一个线程更改
listBluetoothDevice
。 用runOnUiThread
包装它并调用notifyDataSetChanged()