在设备上进行连接时出现问题,该应用程序应创建附近可用设备的列表,在单击所需设备上时,将建立连接,我想在以后发送数据,但是当我单击在所选设备上,该应用程序崩溃了,我已尽力而为,但无济于事,请提供帮助
enter code here
package com.example.arduinobluetoothinterface;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
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.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_ENABLE_BT = 1;
public static final UUID my_uuid = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");
private static final String TAG = "Main app";
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public BroadcastReceiver BlueToothReceiver;
public BluetoothSocket mBluetoothSocket;
public BluetoothDevice mBluetoothDevice;
@Override
protected void onDestroy() {
super.onDestroy();
// Don't forget to unregister the ACTION_FOUND receiver.
unregisterReceiver(BlueToothReceiver);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating Array List
final ArrayList<BtDevicesClass> listOfNames = new ArrayList<BtDevicesClass>();
//Creating Array Adapter Object
final ArrayAdapterBt bluetoothArrayAdapter = new ArrayAdapterBt(this,
listOfNames);
//Setting bluetooth
if (mBluetoothAdapter != null) {
//if bluetooth is supported and Turn it on and make the device discoverable
if (!mBluetoothAdapter.isEnabled()) {
//making an Intent to enable bluetooth
Intent newIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//start the intent
startActivityForResult(newIntent, REQUEST_ENABLE_BT);
//making an intent too set the device discoverable
Intent newIntent2 = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//start the intent
startActivityForResult(newIntent2, 2);
}
} else {
message("Bluetooth is not supported on this Device");
}
/*
when a button is clicked the app will start searching for nearby bluetooth devices
and when he finds them he will list them int list View
*/
Button searchButton = findViewById(R.id.search_button);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//start searching for nearby devices
mBluetoothAdapter.startDiscovery();
message("Searching For Nearby Devices");
mBluetoothAdapter.startDiscovery();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(BlueToothReceiver, filter);
}
});
// Create a BroadcastReceiver for ACTION_FOUND.
BlueToothReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
//Creating listView Object
final ListView listOfDevices = findViewById(R.id.list_of_objects);
//Setting Array Adapter
listOfDevices.setAdapter(bluetoothArrayAdapter);
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice
.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
listOfNames.add(new BtDevicesClass(deviceName, deviceHardwareAddress));
message(deviceName);
/*
handle clicking on list element to connect to it
*/
listOfDevices.setOnItemClickListener(new AdapterView
.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mBluetoothAdapter.cancelDiscovery();
BluetoothSocket temp = null;
try {
temp = mBluetoothDevice.
createRfcommSocketToServiceRecord(my_uuid);
} catch (IOException e) {
message("error while creating bluetooth path");
e.printStackTrace();
}
mBluetoothSocket = temp;
try {
mBluetoothSocket.connect();
} catch (IOException e) {
e.printStackTrace();
message("Can't establish a Connection ");
}
}
});
}
}
};
}
private void message(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
}
这是错误跟踪
D / AndroidRuntime:关闭VM E / AndroidRuntime:致命异常:main 进程:com.example.arduinobluetoothinterface,PID:17512 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)” 在com.example.arduinobluetoothinterface.MainActivity $ 2 $ 1.onItemClick(MainActivity.java:109) 在android.widget.AdapterView.performItemClick(AdapterView.java:310) 在android.widget.AbsListView.performItemClick(AbsListView.java:1194) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3240) 在android.widget.AbsListView.onTouchUp(AbsListView.java:4202) 在android.widget.HwAbsListView.onTouchUp(HwAbsListView.java:434) 在android.widget.AbsListView.onTouchEvent(AbsListView.java:3904) 在android.view.View.dispatchTouchEvent(View.java:8601) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2513) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2157) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2491) 在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1804) 在android.app.Activity.dispatchTouchEvent(Activity.java:2827) 在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) 在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2452) 在android.view.View.dispatchPointerEvent(View.java:8807) 在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4662) 在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4520) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4034) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4087) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4053) 在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4190) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4061) 在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4247) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4034) 在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4087) 在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4053) 在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4061) 在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4034) 在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6463) 在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6437) 在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6390) 在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6622) 在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 在android.os.MessageQueue.nativePollOnce(本地方法) 在android.os.MessageQueue.next(MessageQueue.java:148) 在android.os.Looper.loop(Looper.java:151) 在android.app.ActivityThread.main(ActivityThread.java:5668) 在java.lang.reflect.Method.invoke(本地M I / Process:正在发送信号。 PID:17512 SIG:9 应用已终止。