Я хотел бы создать дочерний поток для создания разъема Bluetooth.поэтому я создаю дочерний поток в основном потоке. Вот так.
serverThread myThread = new serverThread();
myThread.passValue((int)id);
myThread.start();
мой код в дочернем потоке:
private class serverThread extends Thread
{
private BluetoothSocket bt_socket;
private BluetoothServerSocket bt_server;
public int device_id;
public UUID uuid;
public void passValue(int id)
{
this.device_id = id;
}
@Override
public void run()
{
this.uuid = UUID.fromString("00001001-0000-1000-8000-00805f9b34fb");
try {
System.out.println(bt_device.get((int) this.device_id).getName());
bt_server = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(bt_device.get((int) this.device_id).getName(), uuid);
bt_socket = bt_device.get((int) this.device_id).createInsecureRfcommSocketToServiceRecord(uuid);
System.out.println(bt_server.toString());
}
catch (IOException e)
{
System.out.println("error to get blue_server&socket");
e.printStackTrace();
}
//
try {
bt_server.accept();
System.out.println("thread has accept the connection");
}
catch (IOException e)
{
e.printStackTrace();
}
OutputStream output = null;
byte[] buffer = new byte[1024];
int size = 20000;
try {
while (size != 0) {
size--;
output = bt_socket.getOutputStream();
if (output != null) {
String tex = "x: "+size;//sensorvalue[0].toString();
output.write(tex.getBytes("UTF-8"));
// debugView.setText("sending:\n" + tex);
try{
Thread.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
} catch (IOException e) {
try {
bt_socket.close();
bt_server.close();
}
catch (IOException a)
{
a.printStackTrace();
}
debugView.setText("error:\n");
}
//close if finished
try {
bt_socket.close();
bt_server.close();
}
catch (IOException a)
{
a.printStackTrace();
}
}
}
когда дело доходит до:
try {
while (size != 0) {
size--;
output = bt_socket.getOutputStream();
if (output != null) {
String tex = "x: "+size;//sensorvalue[0].toString();
output.write(tex.getBytes("UTF-8"));
// debugView.setText("sending:\n" + tex);
try{
Thread.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
it's error msg:
10-23 22:23:15.698 13027-13593/com.example.administrator.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-11975
Process: com.example.administrator.myapplication, PID: 13027
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:564)
at android.bluetooth.BluetoothOutputStream.write(BluetoothOutputStream.java:85)
at java.io.OutputStream.write(OutputStream.java:82)
at com.example.administrator.myapplication.MainActivity$serverThread.run(MainActivity.java:310)
Я создал дочерний поток для создания и передачи гнезда Bluetooth.Но кажется, что выходной кэш сокета является нулевым на дисплее чтения-записи сокета, но функция принятия на стороне сервера в порядке.
почему аварийное завершение программы кажется, что дочерний поток не может записать данные в выходной потоковый буфер?
и как решить эту проблему?
Я попробовал bt_socket.connect (), новая проблема:
10-24 13:15:15.217 13814-14553/com.example.administrator.myapplication W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
10-24 13:15:15.247 13814-14553/com.example.administrator.myapplication W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
10-24 13:15:15.257 13814-14553/com.example.administrator.myapplication W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:700)
10-24 13:15:15.257 13814-14553/com.example.administrator.myapplication W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:712)
10-24 13:15:15.257 13814-14553/com.example.administrator.myapplication W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:381)
10-24 13:15:15.257 13814-14553/com.example.administrator.myapplication W/System.err: at com.example.administrator.myapplication.MainActivity$serverThread.run(MainActivity.java:304)
10-24 13:15:15.257 13814-14553/com.example.administrator.myapplication I/System.out: reached
10-24 13:15:15.267 13814-14553/com.example.administrator.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-14107
Process: com.example.administrator.myapplication, PID: 13814
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6806)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:974)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5100)
at android.view.View.invalidateInternal(View.java:12817)
at android.view.View.invalidate(View.java:12781)
at android.view.View.invalidate(View.java:12765)
at android.widget.TextView.checkForRelayout(TextView.java:7370)
at android.widget.TextView.setText(TextView.java:4426)
at android.widget.TextView.setText(TextView.java:4283)
at android.widget.TextView.setText(TextView.java:4258)
at com.example.administrator.myapplication.MainActivity$serverThread.run(MainActivity.java:348)