java对象引用出错_getRemoteDevice null对象引用发送蓝牙字符串时出错

我正在尝试将字符串发送到蓝牙设备(HC-06),但我一直收到错误:

E / AndroidRuntime:FATAL EXCEPTION:main进程:com.fiber.sliderapp,PID:29808 java.lang.RuntimeException:无法恢复活动{com.fiber.sliderapp / com.fiber.sliderapp.ControleOnlineActivity}:java.lang . 的NullPointerException:尝试调用虚拟方法“android.bluetooth.BluetoothDevice android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String中)”上在机器人在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3450)空对象引用.app.ActivityThread.handleResumeActivity(ActivityThread.java:3490)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2749)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread $ H .handleMessage(ActivityThread.java:1490)位于android.app.AtoT.Thread.main(ActivityThread)的android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:154) . java:6165)at com.android.internal.os的java.lang.reflect.Method.invoke(Native Method) . ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:888)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)引起的:显示java.lang.NullPointerException:尝试调用虚拟方法“android.bluetooth . BluetoothDevice类android.bluetooth.BluetoothAdapter.getRemoteDevice(java.lang.String中)”上在com.fiber.sliderapp.ControleOnlineActivity.onResume(ControleOnlineActivity.java:60)在android.app.Instrumentation.callActivityOnResume空对象引用(Instrumentation.java :1291)在android.app.Activity.performResume(Activity.java:6791)在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3427)在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3490)在机器人.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2749)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1490)在android.os.Handler .dispatchMessage(Handler.java:102)在android.os.Looper.loop(Loo) per.java:154)在android.app.ActivityThread.main(ActivityThread.java:6165)的java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run( ZygoteInit.java:888)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

我正在使用的代码:

package com.fiber.sliderapp;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothDevice;

import android.bluetooth.BluetoothSocket;

import android.content.Intent;

import android.os.Build;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.text.TextUtils;

import android.util.Log;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

import java.io.IOException;

import java.io.OutputStream;

import java.lang.reflect.Method;

import java.util.UUID;

public class ControleOnlineActivity extends AppCompatActivity {

private static final String TAG = "bluetooth1";

private BluetoothAdapter btAdapter = null;

private BluetoothSocket btSocket = null;

private OutputStream outStream = null;

// SPP UUID service

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

// MAC-address of Bluetooth module (you must edit this line)

public static String address = "98:D3:31:70:0D:0C";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_controle_online);

}

private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {

if(Build.VERSION.SDK_INT >= 10){

try {

final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });

return (BluetoothSocket) m.invoke(device, MY_UUID);

} catch (Exception e) {

Log.e(TAG, "Could not create Insecure RFComm Connection",e);

}

}

return device.createRfcommSocketToServiceRecord(MY_UUID);

}

@Override

public void onResume() {

super.onResume();

Log.d(TAG, "...onResume - try connect...");

// Set up a pointer to the remote node using it's address.

BluetoothDevice device = btAdapter.getRemoteDevice(address);

Log.d(TAG, "...onResume - try connect...");

// Two things are needed to make a connection:

// A MAC address, which we got above.

// A Service ID or UUID. In this case we are using the

// UUID for SPP.

try {

btSocket = createBluetoothSocket(device);

} catch (IOException e1) {

errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");

}

// Discovery is resource intensive. Make sure it isn't going on

// when you attempt to connect and pass your message.

btAdapter.cancelDiscovery();

// Establish the connection. This will block until it connects.

Log.d(TAG, "...Connecting...");

try {

btSocket.connect();

Log.d(TAG, "...Connection ok...");

} catch (IOException e) {

try {

btSocket.close();

} catch (IOException e2) {

errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");

}

}

// Create a data stream so we can talk to server.

Log.d(TAG, "...Create Socket...");

try {

outStream = btSocket.getOutputStream();

} catch (IOException e) {

errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");

}

}

@Override

public void onPause() {

super.onPause();

Log.d(TAG, "...In onPause()...");

if (outStream != null) {

try {

outStream.flush();

} catch (IOException e) {

errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");

}

}

try {

btSocket.close();

} catch (IOException e2) {

errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");

}

}

private void checkBTState() {

// Check for Bluetooth support and then check to make sure it is turned on

// Emulator doesn't support Bluetooth and will return null

if(btAdapter==null) {

errorExit("Fatal Error", "Bluetooth not support");

} else {

if (btAdapter.isEnabled()) {

Log.d(TAG, "...Bluetooth ON...");

} else {

//Prompt user to turn on Bluetooth

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(enableBtIntent, 1);

}

}

}

private void errorExit(String title, String message){

Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();

finish();

}

private void sendData(String message) {

byte[] msgBuffer = message.getBytes();

Log.d(TAG, "...Send data: " + message + "...");

try {

outStream.write(msgBuffer);

} catch (IOException e) {

String msg = "In onResume() and an exception occurred during write: " + e.getMessage();

if (address.equals("00:00:00:00:00:00"))

msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code";

msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";

errorExit("Fatal Error", msg);

}

}

public void message_Hor_Esq(View v){

EditText par;

par = (EditText) findViewById(R.id.editText_horizontal);

if (TextUtils.isEmpty(par.getText().toString())) {

Toast toast = Toast.makeText(this, "Preencha uma velocidade", Toast.LENGTH_SHORT);

toast.show();

} else {

String msg = "C_H_E_" + par.getText().toString() + "/";

sendData("1");

Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();

}

}

public void message_Hor_Dir(View v){

EditText par;

par = (EditText) findViewById(R.id.editText_horizontal);

if (TextUtils.isEmpty(par.getText().toString())) {

Toast toast = Toast.makeText(this, "Preencha uma velocidade", Toast.LENGTH_SHORT);

toast.show();

} else {

String msg = "C_H_D_" + par.getText().toString() + "/";

sendData("0");

Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show();

}

}

}

谁知道问题出在哪里?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值