InputStream在BluetoothSocket上收听时没有收到任何东西

InputStream在BluetoothSocket上收听时没有收到任何东西

问题描述:

我已经做了一个简单的应用程序,用于监听与我的蓝牙模块的Bluetooth串行通信。在这种情况下,蓝牙模块充当服务器,而我的Android手机充当客户端。InputStream在BluetoothSocket上收听时没有收到任何东西

我能够配对设备,并连接到它。但是,我没有得到任何东西给InputStream。任何想法为什么?可能它涉及到该模块具有销1234,我从来没有硬编码的PIN的事实(但我能够连接)

监听线程的代码:

public 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) { 
     System.out.println("IO EXCEPTION: "+ e.toString()); 
    } 

    mmInStream = tmpIn; 
    // mmOutStream = tmpOut; 
} 

public void run() { 
    byte[] buffer = new byte[1024]; // buffer store for the stream 
    int bytes; // bytes returned from read() 

    // Keep listening to the InputStream until an exception occurs 
    while (true) { 
     try { 
      // Read from the InputStream 
      bytes = mmInStream.read(buffer); 
      // Send the obtained bytes to the UI activity 
      processBytes(bytes); 
     } catch (IOException e) { 
      System.out.println("IO EXCEPTION: "+ e.toString()); 
      break; 
     } 
    } 
} 

编辑:

我刚才注意到这个错误:android bluetooth connection fail (isSocketAllowedBySecurityPolicy start : device null) 但我试图让UUID的,我有一个正确:

00001101-0000-1000-8000-00805f9b34fb 

EDIT2:

这也可能是我的问题,因为我在SG3上并且使用SPP BT模块。 Android Bluetooth SPP with Galaxy S3

EDIT3: 我也试图改变:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 

有:

tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID); 

,因为他们认为这里:Bluetooth pairing without user confirmation

EDIT4: 我试图使用Android官方示例进行BT聊天。它有同样的问题。我确信我的BT模块能够正常工作,因为我已经在PC上尝试过了,并且在另一个BT终端应用上使用了同样的SG3。然而,几个BT终端没有工作,我不得不找到合适的。有什么区别吗?

您还应该知道我正在向红外接收器发送红外信号,红外接收器将红外信号传递给蓝牙模块,然后再将蓝牙模块传递给移动应用程序。 IR接收器的波特率为9600,与IR LED相同。

+0

问:什么异常被抛出? A.没有。问:如果空的catch块,你怎么可能告诉? – EJP

+0

我忘了用打印出catch catch例外的代码更新一个问题。我现在在工作。我会尽快更新。对不起。 –

+0

我已更新我的问题。但是,我正在调试它,它从来没有去过catch子句。 –

您在连接线程代码看起来不错,直到你得到这一点:

// Send the obtained bytes to the UI activity 
processBytes(bytes); 

所以没有看到你的代码的其余部分,我假设的问题是在这里。 它没有得到捕获,因为没有抛出异常。但是你没有处理如何正确地将字节传回UI。

我注意到你正在使用的代码从API这里:
developer.android.com/guide/topics/connectivity/bluetooth

而且你缺少的东西是这样的:

bytes = mmInStream.read(buffer); 
// Send the obtained bytes to the UI activity 
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) 
        .sendToTarget(); 

我也已经很正是这个问题时,我写我的第一个蓝牙应用程序。我试图简化代码示例并试图排除处理程序。

/** 
* The Handler that gets information back from the BTManager 
*/ 
private final Handler mHandler = new Handler() { 
    @Override public void handleMessage(android.os.Message msg) { 
     switch (msg.what) { 
      case Constants.MESSAGE_STATE_CHANGE: 
       switch (msg.arg1) { 
        case BluetoothManager.STATE_CONNECTED: 
         // TODO 
         break; 
        // todo .../ 
       } 
       break; 
      case Constants.MESSAGE_READ: 
       byte[] readBuf = (byte[]) msg.obj; 
       // construct a string from the valid bytes in 
       // the buffer 
       String readMessage = 
         new String(readBuf, 0, msg.arg1); 
       // TODO display your string message 
       break; 
       // todo .../    
     } 
    } 
}; 

如果有需要,你可以下载的样本developer.android.com/samples,重要的是管理线程,我用sychroninized methods。 除了看到更多的代码外,这是最有可能的猜测。

如果这不回答你的问题,让我知道。

+0

联盟论坛,我的文字我的代码在多个Android设备,其中包括银河S3 :) –

+0

谢谢你的回答,我今天将测试它,我会让你只要我得到它的工作知道的。我很确定这将是答案。再次感谢:) –

+0

嗨再次,我还没有测试的代码还,但只是让你知道,当我调试,程序从来没有从这个点上移:mmInStream.read(缓冲);所以我不确定处理程序如何帮助它。无论如何,我要去尝试。 –