尝试连接设备时,应用程序(OBDII阅读器应用程序)崩溃。我的MainActivity中有 BTHandler 类,其中BTHandler btHandler = new BTHandler();
btHandler.btConnect();
现在是,但有人告诉我应该创建一个类,然后在MainActivity中创建一个实例。
所以这段代码:
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//AA:BB:CC:11:22:33");
try {
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
Log.v("connect", "connect");
}
catch (IOException e) {
e.printStackTrace();
Log.v("exception","e");
}
曾经在MainActivity中,但被告知它应该是我的大学的另一个班级,他帮助我。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter mAdapter;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.connect);
mAdapter = BluetoothAdapter.getDefaultAdapter();
//init();
if (mAdapter == null) {
Toast.makeText(getApplicationContext(), "Device does not Support Bluetooth", Toast.LENGTH_LONG).show();
finish();
} else {
if (!mAdapter.isEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
}
}
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.connect:
onConnect(); //Operation
Log.v("Log", "Pressed onClick");
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Bluetooth must be Enabled", Toast.LENGTH_SHORT).show();
finish();
}
}
private void onConnect() {
ArrayList deviceStrs = new ArrayList();
final ArrayList<String> devices = new ArrayList();
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
Set pairedDevices = mAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (Object device : pairedDevices) {
BluetoothDevice bdevice = (BluetoothDevice) device;
deviceStrs.add(bdevice.getName() + "\n" + bdevice.getAddress());
devices.add(bdevice.getAddress());
}
}
// show list
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.select_dialog_singlechoice,
deviceStrs.toArray(new String[deviceStrs.size()]));
alertDialog.setSingleChoiceItems(adapter, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
int position = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
String deviceAddress = devices.get(position);
BTHandler btHandler = new BTHandler();
btHandler.btConnect();
}
});
alertDialog.setTitle("Paired devices");
alertDialog.show();
}
}
BTHandler.java
public class BTHandler {
final ArrayList<String> devices = new ArrayList();
BluetoothAdapter mAdapter;
BluetoothDevice device;
String deviceAddress;
public BTHandler(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void btConnect(String deviceAdress) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
device = mAdapter.getRemoteDevice(deviceAddress);
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//AA:BB:CC:11:22:33");
try {
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
socket.connect();
Log.v("connect", "connect");
}
catch (IOException e) {
e.printStackTrace();
Log.v("exception","e");
}
}
}
logcat的:
Process: com.example.asabanov.powersupplytool, PID: 14113 java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String)' on a null object reference
at com.example.asabanov.powersupplytool.BTHandler.btConnect(BTHandler.java:19)
at com.example.asabanov.powersupplytool.MainActivity$1.onClick(MainActivity.java:93)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1108)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1214)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3139)
at android.widget.AbsListView$3.run(AbsListView.java:3981)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
答案 0 :(得分:1)
您没有使用正在初始化mAdapter
的构造函数,导致它null
NullPointerException
更改
BTHandler btHandler = new BTHandler();
带
BTHandler btHandler = new BTHandler(MainActivity.this, null);
在警告对话框的onClick
方法中。
注意:您没有使用第二个参数,因此null
不应该担心您,或者您可能应该同时删除第二个参数。
<强>更新强>
像这样创建BTHandler
个实例
BTHandler btHandler = new BTHandler(MainActivity.this, new Handler() {
public void handleMessage(Message msg) {
String response = msg.getData().getString("message");
// TODO: take your action here
}
});
使用此功能在BTHandler
内发送消息。创建全局变量mHandler
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", "Hello");
msg.setData(b);
mHandler.sendMessage(msg);
像这样改变你的BTHandler
构造函数
Handler mHandler;
public BTHandler(Context context, Handler handler) {
mAdapter = BluetoothAdapter.getDefaultAdapter();
mHandler = handler;
}