波纹管代码在api级别8上完美运行但在api级别23上,android marshmallow成功发送数据但未能收到。我评论了'注1' 的一行。它显示了异常消息"尝试调用虚拟方法int java.io.inStream.availabe()"在null对象引用上。
public class BtTimeSync extends AppCompatActivity{
BluetoothAdapter btAdapter;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
BluetoothDevice selectedDevice ;
BluetoothSocket mmSocket;
ConnectThread connect;
ConnectedThread connectedThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_bt_time_sync);
init();
}
void init(){
BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
btAdapter = bluetoothManager.getAdapter();
selectedDevice=btAdapter.getRemoteDevice(getIntent().getExtras().getString("MAC"));
}
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
// DO something
//ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(), "CONNECT", Toast.LENGTH_SHORT).show();
//getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Calendar c = Calendar.getInstance();
String dt = "!m0:" + c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.MONTH) + "/" + c.get(Calendar.YEAR) + "/" + c.get(Calendar.HOUR_OF_DAY) + "/" + c.get(Calendar.MINUTE) + "/" + c.get(Calendar.SECOND) + ";";
connectedThread.write(dt.getBytes());
//String s = "successfully connected";
//connectedThread.write(new String("@#"+s+"#@").getBytes());
//connectedThread.write(new String(s+";").getBytes());
//connectedThread.cancel();
break;
case MESSAGE_READ:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
break;
}
}
};
private class ConnectThread extends Thread {
//private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
//mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
while (true) {
try {
while (mmSocket.getInputStream().available() > 0) {
//mHandler.obtainMessage(MESSAGE_READ, "Data has come".length(), -1, "Data has come".getBytes()).sendToTarget();
String s1="";
//s += "5";
//flag=true;
int tmp=0;
while(true) {
if(tmp==';'){
break;
}
if(mmSocket.getInputStream().available()>0) {
tmp = mmSocket.getInputStream().read();
s1+=String.valueOf((char)tmp);
//baos.write(tmp);
}
//s += "6";
Thread.sleep(1);
}
while (mmSocket.getInputStream().available() > 0) {
Thread.sleep(1);
mmSocket.getInputStream().read();
}
mHandler.obtainMessage(MESSAGE_READ, s1.length(), -1, s1.getBytes()).sendToTarget();
}
} catch (Exception e) {
e.printStackTrace();
mHandler.obtainMessage(MESSAGE_READ, "Exception".length(), -1, "Exception".getBytes()).sendToTarget();//note 1
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
@Override
protected void onPause() {
super.onPause();
btAdapter.cancelDiscovery();
connect.cancel();
try {
mmSocket.close();
}
catch (Exception e){}
}
@Override
protected void onResume() {
super.onResume();
connect = new ConnectThread(selectedDevice);
connect.start();
connectedThread = new ConnectedThread(mmSocket);
connectedThread.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
connect.cancel();
try {
mmSocket.close();
}
catch (Exception e){}
}
}
堆栈跟踪
Reconstruct Branch : NOTHING
08-27 15:53:54.628 9233-9276 / com.example.sid.roommanager I / OpenGLRenderer:初始化的EGL,版本1.4
08-27 15:54:01.183 9233-9233 / com.example.sid.roommanager I / Timeline:Timeline:Activity_launch_request time:49199633 08-27 15:54:01.246 9233-9385 / com.example.sid.roommanager W / BluetoothAdapter:getBluetoothService()调用没有BluetoothManagerCallback 08-27 15:54:01.247 9233-9386 / com.example.sid.roommanager W / System.err:java.lang.NullPointerException:尝试调用虚方法' int java.io.InputStream.available() '在null对象引用上 08-27 15:54:01.249 9233-9386 / com.example.sid.roommanager W / System.err:at android.bluetooth.BluetoothSocket.available(BluetoothSocket.java:529) 08-27 15:54:01.249 9233-9386 / com.example.sid.roommanager W / System.err:at android.bluetooth.BluetoothInputStream.available(BluetoothInputStream.java:40) 08-27 15:54:01.249 9233-9386 / com.example.sid.roommanager W / System.err:at com.example.sid.roommanager.BtTimeSync $ ConnectedThread.run(BtTimeSync.java:162) 08-27 15:54:01.250 9233-9233 / com.example.sid.roommanager V / BoostFramework:BoostFramework():mPerf = com.qualcomm.qti.Performance@ac0730d 08-27 15:54:01.250 9233-9233 / com.example.sid.roommanager V / BoostFramework:BoostFramework():mPerf = com.qualcomm.qti.Performance@d22c2 08-27 15:54:01.262 9233-9233 / com.example.sid.roommanager D / ActivityThreadInjector:clearCachedDrawables。 08-27 15:54:01.346 9233-9276 / com.example.sid.roommanager V / RenderScript:0x5589f6a110启动线程,CPU 8 08-27 15:54:01.360 9233-9276 / com.example.sid.roommanager D / OpenGLRenderer:0x5589de6380(RippleDrawable)上的endAllStagingAnimators,句柄为0x5589de6190