[英]text message from android to pc using Bluetooth sometimes received as null
Im trying to send Text from a client android to a server java on PC 我正在尝试将文本从客户端Android发送到PC上的服务器Java
here is the android side code 这是android侧码
protected static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private String BluetoothMacAddress = "A8:A7:95:65:26:BA";
protected void onCreate() {
initBluetoothDevice();
CheckBluetoothState();
device = mBluetoothAdapter.getRemoteDevice(getBluetoothMacAddress());
new Send("Hello from Client").execute();
mBluetoothAdapter.cancelDiscovery();
}
class Send extends AsyncTask<Void, Void, Void> {
BluetoothSocket btSocket;
//private Socket client;
private String TextMessage;
//private PrintWriter toServer;
protected OutputStream outStream = null;
public Send(String string) {
TextMessage = string;
}
@Override
protected Void doInBackground(Void... params) {
try {
Log.d(TAG, "doInBackground: "+TextMessage);
//client = new Socket(IpAdress, Port); // connect to the server Ip given by my mobile hotspot
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();
outStream = btSocket.getOutputStream();
byte[] msgBuffer = TextMessage.getBytes();
outStream.write(msgBuffer);
outStream.flush();
btSocket.close(); // closing the connection
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
here is the java code 这是java代码
public class SimpleSPPServer {
StreamConnection connection;
StreamConnectionNotifier streamConnNotifier;
InputStream inStream;
BufferedReader bReader;
private void startServer() throws IOException{
//Create a UUID for SPP
UUID uuid = new UUID("0001101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
connection = streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
while(true){
try{
//streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
connection = streamConnNotifier.acceptAndOpen();
inStream = connection.openInputStream();
bReader=new BufferedReader(new InputStreamReader(inStream));
String lineRead = bReader.readLine();
System.out.println(lineRead);
inStream.close();
}catch(Exception e){
e.printStackTrace();
// break;
}
}
}
} }
the code works but the problem is that the received message is sometimes null, can anyone tell me where is the problem and how to fix it 该代码有效,但问题是收到的消息有时为空,任何人都可以告诉我问题出在哪里以及如何解决
Looking up the documentation for BufferedReader#readLine()
it specifies the following as return value: 查找
BufferedReader#readLine()
的文档,将以下内容指定为返回值:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
一个字符串,其中包含行的内容,不包含任何行终止符;如果已到达流的末尾,则为null
So when it returns a null
that means the underlying data stream did not have any further data. 因此,当它返回
null
,表示基础数据流没有任何其他数据。 With any communication between two computers like bluetooth it might just be a case of communication failure and should be expected to happen every so often. 在两台计算机之间(例如蓝牙)之间进行任何通信时,可能只是通信失败的情况,应该经常发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.