Android新手在这里。我密切关注了该视频,因为它是one和two的一部分。该视频教我如何制作可以扫描附近的蓝牙设备并显示它们的应用程序,我个人认为这是一个非常好的视频。
但是,当我将确切的代码编码到我的Android Studio中并安装到我的android手机中时,并且当我单击SCAN
按钮时,ListView中没有弹出蓝牙设备。就像onClickListener
失败一样。这个问题使我深感困惑。这个问题有什么解释吗?这是我的MainActivity.java:
package com.example.bluetooth_01;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button scanButton;
ListView scanListView;
ArrayList<String> stringArrayList=new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
BluetoothAdapter myAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("onCreate","Activated!");
scanButton = (Button) findViewById(R.id.scanButton);
scanListView = (ListView) findViewById((R.id.scanListView));
// When the 'SCAN' button being clicked...
scanButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Returns a boolean value indicating whether discovery has successfully
// it is either TRUE or FALSE
myAdapter.startDiscovery();
}
});
// To receive information about each device discovered,
// we must register a BroadcastReceiver for the ACTION_FOUND
IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(myReceiver,intentFilter);
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,stringArrayList);
scanListView.setAdapter(arrayAdapter);
}
BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// When a new device is discovered...
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action))
{
// Contains the information of the discovered device
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Populate the ListView in activity_main.xml
stringArrayList.add(device.getName());
//stringArrayList.add("WAHAHAHA");
// Notify arraayAdapter that stringArrayList has beingg changed
arrayAdapter.notifyDataSetChanged();
}
}
};
}
这是activity_main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SCAN"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="146dp" />
<ListView
android:id="@+id/scanListView"
android:layout_width="395dp"
android:layout_height="521dp"
android:background="@color/colorPrimary"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="202dp" />
</android.support.constraint.ConstraintLayout>
或者在activity_main.xml中有任何问题吗?
是因为在simple_list_item_1
中使用arrayAdapter
导致了问题吗?如果是这样,有什么解决办法吗?
如果能回答我对此事的困惑,我将非常感谢!