java - Android输入流没有收到任何消息
问题描述
我正在尝试从连接到 arduino 的蓝牙接收数据,但是当我从输入流中读取时,它一直给我 bytes=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();
}
}
我尝试了另一个应用程序,并且正确接收了来自蓝牙的数据,所以问题不在蓝牙中,我也尝试在阅读时进入睡眠状态,但它也不起作用
解决方案
推荐阅读
- php - Ajax 在 json _encode() codeigniter 之后返回 undefined
- solr - 如何在 SOLR 可搜索文本中关联分段元数据
- java - Jackson ObjectMapper:如何从序列化中省略(忽略)某些类型的字段?
- python - 一些帮助需要 selenium python 定位名称
- java - NetBeans 12.0:“类没有主方法”(但有)
- java - 通过单击在 Mac Big Sur 上运行 .jar 文件
- javascript - devtools - 当所有网络请求完成时,向后台页面发送消息
- amazon-web-services - AWS EC2 linux 2 ami postfix 邮件服务器错误连接超时
- node.js - 如何使用 React 中的函数发送和关闭 websocket
- emacs - Orgmode 优先级:可能超过 ABC?