为什么在尝试通过蓝牙发送byte []时会出现NullPointerException?它发生在mConnectedThread.write(mOutput);在内部ConnectThread类之前的底层附近的主类中调用。我一直在向控制台写日志,他们都说这个字节不是空的,但它仍然拒绝发送。这是我的主要课程:
public class bluetoothMusicListener extends AppCompatActivity {
BluetoothAdapter mBluetoothAdapter;
Context mContext;
ConnectedThread mConnectedThread;
ConnectThread mConnectThread;
Handler mHandler;
View mConnectToDevices;
View mSendText;
BluetoothDevice mDevice;
ParcelUuid list[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bluetooth_listener);
mConnectToDevices = findViewById(R.id.connectToDevices);
mSendText = findViewById(R.id.sendText);
mContext = getApplicationContext();
mBluetoothAdapter = mBluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(mContext, "No bluetooth adapter found", Toast.LENGTH_SHORT).show();
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
mConnectToDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
//BluetoothDevice mDevice[] = (BluetoothDevice)pairedDevices.toArray();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
mDevice = device;
list = device.getUuids();
}
}
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
byte[] writeBuf = (byte[]) msg.obj;
int begin = (int)msg.arg1;
int end = (int)msg.arg2;
switch(msg.what) {
case 1:
String writeMessage = new String(writeBuf);
TextView fromDevice = (TextView) findViewById(R.id.fromDevice);
fromDevice.setText(writeMessage);
break;
}
}
};
mConnectThread = new ConnectThread(mDevice, mHandler);
mConnectThread.start();
}
});
mSendText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = (EditText)findViewById(R.id.editText);
if (input.getText() != null) {
//for(int i = 0; i < mConnectedThread.length || i < 5; i++) {
String stringOutput = input.getText().toString();
byte[] mOutput = (byte[])stringOutput.getBytes();
Log.v("input", "input is: " + input);
Log.v("input", "input getText is: " + input.getText());
Log.v("input", "input getText toString is: " + input.getText().toString());
Log.v("input", "output is: " + mOutput.toString());
mConnectedThread.write(mOutput);
//}
}
}
});
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private final Handler mHandler;
private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
public ConnectThread(BluetoothDevice device, Handler handler) {
BluetoothSocket tmp = null;
mHandler = handler;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
mmSocket = tmp;
}
public void run() {
mBluetoothAdapter.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) {
closeException.printStackTrace();
}
return;
}
mConnectedThread = new ConnectedThread(mmSocket, mHandler);
mConnectedThread.start();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是我的ConnectedThread类:
class ConnectedThread extends Thread {
private BluetoothSocket mmSocket;
private Handler mHandler;
private InputStream mmInStream;
private OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, Handler handler) {
mmSocket = socket;
mHandler = handler;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int begin = 0;
int bytes = 0;
while (true) {
try {
bytes += mmInStream.read(buffer, bytes, buffer.length - bytes);
for(int i = begin; i < bytes; i++) {
if(buffer[i] == "#".getBytes()[0]) {
mHandler.obtainMessage(1, begin, i, buffer).sendToTarget();
begin = i + 1;
if(i == bytes - 1) {
bytes = 0;
begin = 0;
}
}
}
} catch (IOException e) {
break;
}
}
}
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch(IOException e ){
e.printStackTrace();
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
谢谢!