Java Android输入流没有';我没有收到任何消息

Java Android输入流没有';我没有收到任何消息,java,android,bluetooth,Java,Android,Bluetooth,我试图从连接到arduino的Bluetooth接收数据,但当我从输入流中读取数据时,它总是给我字节=0 这是我的蓝牙服务代码: package com.example.googlemapproject; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.Bluetoot

我试图从连接到arduino的Bluetooth接收数据,但当我从输入流中读取数据时,它总是给我字节=0 这是我的蓝牙服务代码:

package com.example.googlemapproject;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.SystemClock;
import android.speech.tts.TextToSpeech;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.UUID;
import java.util.Vector;


public class BluetoothService extends Service {
    TextToSpeech txt_to_speech;

    private BluetoothAdapter mBluetoothAdapter;

    public static final String B_UUID = "00001101-0000-1000-8000-00805f9b34fb";


    public static final int STATE_NONE = 0;

    public static final int STATE_CONNECTING = 2;


    private ConnectBtThread mConnectThread;
    private static ConnectedBtThread mConnectedThread;

    private static Handler mHandler = null;
    public static int mState = STATE_NONE;

    public static BluetoothDevice sDevice = null;
    public Vector<Byte> packData = new Vector<>(2048);


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    public void toast(String mess){
        Toast.makeText(this,mess,Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        String deviceg = intent.getStringExtra("bluetooth_device");


        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        connectToDevice(deviceg);

        return START_STICKY;
    }
    private synchronized void connectToDevice(String macAddress){
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("00:13:EF:00:E3:77");
        if (mState == STATE_CONNECTING){
            if (mConnectThread != null){
                mConnectThread.cancel();
                mConnectThread = null;
            }
        }
        if (mConnectedThread != null){
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        mConnectThread = new ConnectBtThread(device);
        toast("connecting");
        mConnectThread.start();
        setState(STATE_CONNECTING);
    }
    private void setState(int state){
        mState = state;
        if (mHandler != null){
            // mHandler.obtainMessage();
        }
    }
    public synchronized void stop(){
        setState(STATE_NONE);
        if (mConnectThread != null){
            mConnectThread.cancel();
            mConnectThread = null;
        }
        if (mConnectedThread != null){
            mConnectedThread.cancel();
            mConnectedThread = null;
        }
        if (mBluetoothAdapter != null){
            mBluetoothAdapter.cancelDiscovery();
        }

        stopSelf();
    }

    public void sendData(String message){
        if (mConnectedThread!= null){
            mConnectedThread.write(message.getBytes());
            toast("sent data");
        }else {
            Toast.makeText(BluetoothService.this,"Failed to send data",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean stopService(Intent name) {
        setState(STATE_NONE);

        if (mConnectThread != null){
            mConnectThread.cancel();
            mConnectThread = null;
        }

        if (mConnectedThread != null){
            mConnectedThread.cancel();
            mConnectedThread = null;
        }

        mBluetoothAdapter.cancelDiscovery();
        return super.stopService(name);
    }

/*private synchronized void connected(BluetoothSocket mmSocket){

    if (mConnectThread != null){
        mConnectThread.cancel();
        mConnectThread = null;
    }
    if (mConnectedThread != null){
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    mConnectedThread = new ConnectedBtThread(mmSocket);
    mConnectedThread.start();


    setState(STATE_CONNECTED);
}*/

    private class ConnectBtThread extends Thread{
        private final BluetoothSocket mSocket;
        private final BluetoothDevice mDevice;

        public ConnectBtThread(BluetoothDevice device){
            mDevice = device;
            BluetoothSocket socket = null;
            try {
                socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(B_UUID));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mSocket = socket;

        }

        @Override
        public void run() {
            mBluetoothAdapter.cancelDiscovery();

            try {
                mSocket.connect();
                Log.d("service","connect thread run method (connected)");
                SharedPreferences pre = getSharedPreferences("BT_NAME",0);
                pre.edit().putString("bluetooth_connected",mDevice.getName()).apply();

            } catch (IOException e) {

                try {
                    mSocket.close();
                    Log.d("service","connect thread run method ( close function)");
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
            //connected(mSocket);
            mConnectedThread = new ConnectedBtThread(mSocket);
            mConnectedThread.start();
        }

        public void cancel(){

            try {
                mSocket.close();
                Log.d("BT","connect thread cancel method");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class ConnectedBtThread extends Thread{
        private final BluetoothSocket cSocket;
        private final InputStream inS;
        private final OutputStream outS;

        private byte[] buffer;

        public ConnectedBtThread(BluetoothSocket socket){
            cSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            try {
                tmpIn = socket.getInputStream();

            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                tmpOut = socket.getOutputStream();

                Log.d("BT","OUTPUT STREAM");
            } catch (IOException e) {
                e.printStackTrace();
            }

            inS = tmpIn;
            outS = tmpOut;
        }

        @Override
        public void run() {
          //  Looper.prepare();
            Log.d("BT","REading FROM IS");
            int bytesAvailable = 0;
            try {
                bytesAvailable = inS.available();
                Log.d("BT","REading FROM IS");
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[bytesAvailable];
            int bytes;
            Log.d("BT","REading #BYTES");

            while (true) {
                try {try {
                    sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                    bytes = inS.read(buffer);
                    Log.d("BT",String.valueOf(bytes));
                    final String strReceived = new String(buffer, 0, bytes);
                   // Log.d("BT",strReceived);


            } catch (IOException e) {
                    e.printStackTrace();
                }}
         }


        private void cancel(){
            try {
                cSocket.close();
                Log.d("service","connected thread cancel method");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        this.stop();
        super.onDestroy();
    }
}

package com.example.googlemapproject;
导入android.app.Service;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothSocket;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Handler;
导入android.os.IBinder;
导入android.os.Looper;
导入android.os.Message;
导入android.os.Messenger;
导入android.os.RemoteException;
导入android.os.SystemClock;
导入android.speech.tts.TextToSpeech;
导入android.support.annotation.Nullable;
导入android.support.v4.content.LocalBroadcastManager;
导入android.util.Log;
导入android.widget.Toast;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.Locale;
导入java.util.UUID;
导入java.util.Vector;
公共类BluetoothService扩展了服务{
text to speech txt_to_speech;
私人蓝牙适配器mBluetoothAdapter;
公共静态最终字符串B_UUID=“00001101-0000-1000-8000-00805f9b34fb”;
公共静态最终整数状态_NONE=0;
公共静态最终int状态_连接=2;
私有连接线程mConnectThread;
私有静态连接的btthread mConnectedThread;
私有静态处理程序mHandler=null;
public static int mState=状态\无;
公共静态蓝牙设备sDevice=null;
公共向量packData=新向量(2048);
@可空
@凌驾
公共IBinder onBind(意向){
返回null;
}
公共空间吐司(字符串混乱){
Toast.makeText(this,mess,Toast.LENGTH_SHORT).show();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
字符串设备g=intent.getStringExtra(“蓝牙设备”);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
连接到设备(设备G);
返回开始时间;
}
私有同步的void connectToDevice(字符串macAddress){
BluetoothDevice=mBluetoothAdapter.getRemoteDevice(“00:13:EF:00:E3:77”);
if(mState==状态_连接){
如果(mConnectThread!=null){
mConnectThread.cancel();
mConnectThread=null;
}
}
if(mConnectedThread!=null){
mConnectedThread.cancel();
mConnectedThread=null;
}
mConnectThread=新的ConnectBtThread(设备);
吐司(“连接”);
mConnectThread.start();
设置状态(连接状态);
}
私有无效设置状态(int状态){
mState=状态;
if(mHandler!=null){
//mHandler.obtainMessage();
}
}
公共同步无效停止(){
设置状态(无状态);
如果(mConnectThread!=null){
mConnectThread.cancel();
mConnectThread=null;
}
if(mConnectedThread!=null){
mConnectedThread.cancel();
mConnectedThread=null;
}
if(mBluetoothAdapter!=null){
mBluetoothAdapter.cancelDiscovery();
}
stopSelf();
}
公共void sendData(字符串消息){
if(mConnectedThread!=null){
mConnectedThread.write(message.getBytes());
toast(“发送数据”);
}否则{
Toast.makeText(BluetoothService.this,“发送数据失败”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
公共布尔停止服务(意图名称){
设置状态(无状态);
如果(mConnectThread!=null){
mConnectThread.cancel();
mConnectThread=null;
}
if(mConnectedThread!=null){
mConnectedThread.cancel();
mConnectedThread=null;
}
mBluetoothAdapter.cancelDiscovery();
返回super.stopService(名称);
}
/*已连接专用同步void(BluetoothSocket mmSocket){
如果(mConnectThread!=null){
mConnectThread.cancel();
mConnectThread=null;
}
if(mConnectedThread!=null){
mConnectedThread.cancel();
mConnectedThread=null;
}
mConnectedThread=新连接的BTThread(mmSocket);
mConnectedThread.start();
设置状态(连接状态);
}*/
私有类ConnectBtThread扩展线程{
私人最终蓝牙插座mSocket;
私有最终蓝牙设备mDevice;
公共连接线程(蓝牙设备){
mDevice=设备;
BluetoothSocket=null;
试一试{
socket=device.createInsurerCommsocketToServiceRecord(UUID.fromString(B_UUID));
}捕获(IOE异常){
e、 printStackTrace();
}
mSocket=插座;
}
@凌驾
公开募捐{
mBluetoothAdapter.cancelDiscovery();
试一试{
mSocket.connect();
Log.d(“服务”,“连接线程运行方法(已连接)”);
SharedReferences pre=GetSharedReferences(“BT_名称”,0);
pre.edit().putString(“bluetooth_connected”,mDevice.getName()).apply();
}捕获(IOE异常){
试一试{
mSocket.close();
d(“服务”,“连接线程运行方法(关闭函数)”);
}捕获(IOE1异常){
e1.printStackTrace();
}
e、 printStackTrace();
}
//已连接(mSocket);
mConnectedThread=新连接的BTThread(mSocket);
mConnectedThread.start();
}
公开作废取消(){
试一试{
mSocket.close();
日志d(“BT”、“conn”