我需要一些帮助我的许可完成项目。 我编写这段代码并没有语法错误但应用程序已停止。 这个代码和xml文件在下面添加。我想只扫描附近的蓝牙设备并在listview上显示这个设备
MainActivity:
public class MainActivity extends AppCompatActivity
{
private BluetoothAdapter btadapter;
private ArrayAdapter<String>arrayAdapter;
private ArrayList<String> devicelist;
private ListView list;
private static final int BT_REQUEST_ENABLE =1;
private static final int BT_VISIABLE_ENABLE = 12 ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
final CheckBox visibilitycontol = (CheckBox) findViewById(R.id.visibilitycontrol);
ListView list = (ListView) findViewById(R.id.listView);
btadapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
// Bluetooth device control
if ( btadapter == null)
{Toast.makeText(getApplicationContext(),"Bluetooth Device Not Available",Toast.LENGTH_LONG).show();}
// Bluetooth status check
if ( !btadapter.isEnabled() )
{ Toast.makeText(getApplicationContext(),"Please Turn On The Bluetooth Device",Toast.LENGTH_LONG).show();
Intent bt_request_intent = new Intent((BluetoothAdapter.ACTION_REQUEST_ENABLE));
startActivityForResult(bt_request_intent , BT_REQUEST_ENABLE);
}
else
{Toast.makeText(getApplicationContext(),"Bluetooth Device Already Turned On",Toast.LENGTH_LONG).show();}
// Bluetooth visibility control
visibilitycontol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( visibilitycontol.isChecked() )
{
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
startActivityForResult(discoverableIntent , BT_VISIABLE_ENABLE);}
}
});
}
// discovering new device
public final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
Toast.makeText(getApplicationContext(),"DEVICE FOUND",Toast.LENGTH_LONG).show();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String devicename = device.getName();
String deviceadress = device.getAddress();
ArrayList<String> devicelist = new ArrayList<String>();
ArrayList<BluetoothDevice>btdevicelist = new ArrayList<BluetoothDevice>();
btdevicelist.add(device);
devicelist.add(devicename+"\n"+deviceadress);
list.setAdapter(arrayAdapter);
list.refreshDrawableState();
}
}
};
// Scan button code
public void scan(View view)
{
if(btadapter.isDiscovering()) {btadapter.cancelDiscovery();}
Toast.makeText(getApplicationContext(), "Searching for devices...", Toast.LENGTH_LONG).show();
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,devicelist);
btadapter.startDiscovery();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == BT_REQUEST_ENABLE )
{
if(resultCode == RESULT_OK)
{
Toast.makeText(getApplicationContext(), "Bluetooth Device Is Turning On", Toast.LENGTH_LONG).show();
}
if ( resultCode == RESULT_CANCELED)
{finish();}
}
if( requestCode == BT_VISIABLE_ENABLE)
{
if (resultCode == 120)
{Toast.makeText(getApplicationContext(), "Now Device Visiable For a 120 seconds!", Toast.LENGTH_LONG).show();}
else {Toast.makeText(getApplicationContext(), "Please Give The Visibility Permission", Toast.LENGTH_LONG).show(); }
}
}
@Override
protected void onDestroy()
{super.onDestroy(); unregisterReceiver(mReceiver);}
}
和我的XML文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fatih.bluetoothcontrol.MainActivity">
<TextView
android:text="Bluetooth Control Application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="@+id/textView2" />
<CheckBox
android:text="Make This Device Visiable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/visibilitycontrol"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp" />
<TextView
android:text="00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/counter"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_marginStart="260dp" />
<TextView
android:text="Remaining Time for Visibility"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/textView3"
android:layout_marginEnd="21dp"
android:layout_below="@+id/visibilitycontrol"
android:layout_alignEnd="@+id/textView2" />
<ListView
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_below="@+id/textView3"
android:layout_alignParentStart="true"
android:layout_marginTop="54dp"
android:background="@android:drawable/editbox_dropdown_light_frame"
android:id="@+id/listView" />
<Button
android:text="Control Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/controlmenu"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp" />
<Button
android:text="scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/listView"
android:layout_alignStart="@+id/counter"
android:id="@+id/scan"
android:elevation="0dp"
android:onClick="scan" />
<Button
android:text=" connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/connect"
android:layout_below="@+id/scan"
android:layout_alignParentEnd="true"
/>
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fatih.bluetoothcontrol, PID: 16078
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 (has extras) } in com.example.fatih.bluetoothcontrol.MainActivity$2@32531784
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:933)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.fatih.bluetoothcontrol.MainActivity$2.onReceive(MainActivity.java:97)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)