我有一组用于简单蓝牙通信的代码,当我将其移至服务时似乎无法正常工作。我尝试了一些事情,但似乎都没有用。关于如何修复beginListenForData的任何想法?旁注:很抱歉有任何错误,第一次在这里提出问题。
我已经在移植它的单独活动中尝试过此方法。还尝试从需要侦听数据的地方调用它,但通常最终会使它崩溃。
蓝牙服务:
//connects the user to the bag
public void pairUp(String info, String address){
//checks if it was successful
boolean test = true;
//tries to connect to the address given
try {
if (btSocket == null || !isBtConnected) {
BluetoothDevice bag = bAdapt.getRemoteDevice(address);
btSocket = bag.createInsecureRfcommSocketToServiceRecord(myUUID);
btSocket.connect();
}
}
catch (IOException e){
test = false;
}
//displays if the connection succeeded or failed
if (!test)
{
String toast = "Connection Failed. Is it a SPP Bluetooth? Try again.";
Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
}
else
{
String toast = "Connected.";
Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
//lets the code know we are connected
isBtConnected = true;
//starts up the reading of the input
try{
mmInputStream = btSocket.getInputStream();
beginListenForData();
}
catch (IOException e){
}
}
}
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
String mess = data.replaceAll("x","");
int firstS = mess.indexOf(" ");
int secondS = mess.lastIndexOf(" ");
String first = mess.substring(0, firstS);
String sec = mess.substring(firstS+1, secondS);
String third = mess.substring(secondS+1, mess.length());
messages.add(first);
messages.add(sec);
messages.add(third);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
//gets the message
public ArrayList<String> getMessages() {
return messages;
}
//checks if bt is connected
public boolean btConnected(){ return isBtConnected; }
//checks if there is a new message
public boolean getNew(){
return newMes;
}
public void startListen(){
//starts up the reading of the input
try{
mmInputStream = btSocket.getInputStream();
beginListenForData();
}
catch (IOException e){
Toast.makeText(this,"exception", Toast.LENGTH_SHORT).show();
}
}
我需要输入流的代码:
public void theTimer(){
countTimer = new CountDownTimer(milliLeft, 1000){
//every second, the time is updated
@Override
public void onTick(long l){
milliLeft = l;
updateTimer();
}
//happens at the end of the timer
@Override
public void onFinish(){
//make sound play
playSound();
//end taking in signals
bCon.sendMes("3");
//get the input from the controller
waitForInput();
getValues();
//move to next screen
nextPage();
}
}.start(); //makes sure the timer starts when the aplication opens
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
connected = true;
BlueToothControl.LocalBinder mLocalbinder = (BlueToothControl.LocalBinder) service;
bCon = mLocalbinder.getServerIntance();
}
@Override
public void onServiceDisconnected(ComponentName name) {
bCon = null;
connected = false;
}
};
private void waitForInput(){
boolean checking = true;
while(checking){
if(bCon.getNew()){
checking = false;
messages = bCon.getMessages();
}
}
}
可以预期的是,电话会经过用户在先前活动中设置的计时器,只要我不拨打电话就开始监听所需代码中的数据,它就会发送字符3,然后得到一个字符串信息。到目前为止,它到达了计时器的末尾,然后被卡住了,我假设在waitForInput循环中,并且没有收到消息。