2016-04-26 35 views
5

我们的设备通过蓝牙发送数据,在android应用程序中我们需要读取这些数据。Android BluetoothSocket连接返回零

我能够建立蓝牙连接,接下来我打电话给一个线程,以使用BluetoothDevice建立BluetoothSocket连接。这里当字节被读取时,它返回为0(零) 另外while循环只运行一次。

此外,我在下面的代码中使用的UUID来自一些蓝牙代码片段。我是否需要获取设备的正确UUID?

任何人都可以帮忙吗?如果你给我有用的答案,我将非常感谢。

//Calling ConnectThread after Bluetooth is paired 
    public class ConnectThread extends Thread { 
      private final BluetoothSocket mmSocket; 
      private final BluetoothDevice mmDevice; 
      private final UUID MY_UUID = UUID 
        .fromString("00001101-0000-1000-8000-00805f9b34fb"); 

      public ConnectThread(BluetoothDevice device) { 
       BluetoothSocket tmp = null; 
       mmDevice = device; 
       try { 
        String name = device.getName(); 
        Log.e("Device Name ", name); 
        tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
       } catch (IOException e) { 
       } 
       mmSocket = tmp; 
      } 

      public void run() { 
       // mAdapter.cancelDiscovery(); 
       try { 
        mmSocket.connect(); 

        ConnectedThread mConnectedThread = new ConnectedThread(mmSocket); 
        mConnectedThread.start(); 
       } catch (IOException connectException) { 
        // try { 
        // mSocket.close(); 
        // } catch (IOException closeException) { } 
        return; 
       } 
      } 

      public void cancel() { 
       try { 
        mmSocket.close(); 
       } catch (IOException e) { 
       } 
      } 
     } 

     private 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; 
       try { 
        tmpIn = socket.getInputStream(); 
        tmpOut = socket.getOutputStream(); 
       } catch (IOException e) { 
       } 
       mmInStream = tmpIn; 
       mmOutStream = tmpOut; 
      } 

      public void run() { 
       byte[] buffer = new byte[1024]; 
       int begin = 0; 
       int bytes = 0; 
       while (true) { 
        try { 
         // bytes += mmInStream.read(buffer, bytes, buffer.length 
         // - bytes); 
         if (mmSocket.isConnected()) { 
          Log.e("Socket is connected", "Socket is connected"); 
         } 
         int numOfBytes = mmInStream.available(); 
         Log.e("numOfBytes", String.valueOf(+numOfBytes)); 
         bytes = mmInStream.read(buffer); 

         // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) 
         // .sendToTarget(); 



        } catch (IOException e) { 
         break; 
        } 
       } 
      } 


i am struck and unable to read the data from the Bluetooth 

回答

1

我需要获得设备的正确UUID。

是的。如果需要新的UUID,您可以使用BluetoothDevicegetUuids()返回远程设备的支持功能(UUID),或返回fetchUuidsWithSdp()

while循环只运行一次。

这是因为你的代码抛出一个错误,你正在处理此错误,并退出循环

  while (true) { 
       try { 
        // bytes += mmInStream.read(buffer, bytes, buffer.length 
        // - bytes); 
        if (mmSocket.isConnected()) { 
         Log.e("Socket is connected", "Socket is connected"); 
        } 
        int numOfBytes = mmInStream.available(); 
        Log.e("numOfBytes", String.valueOf(+numOfBytes)); 
        bytes = mmInStream.read(buffer); 

        // mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) 
        // .sendToTarget(); 



       } catch (IOException e) { 
        // THIS BREAK INTERRUPT YOUR WHILE-LOOP SENTENCE 
        break; 
       } 
      } 

这里被读取的字节数,当它返回0

因为您创建了一个套接字的InputStream,它没有连接任何东西(因为您创建的用于生成套接字的UUID在您建立蓝牙连接的设备中不存在)

编辑

得到的UUID另一种方法是配置广播接收器取回的UUID:

//Register the BroadcastReceiver 
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
filter.addAction(BluetoothDevice.ACTION_UUID); 
//Set your other filters 
registerReceiver(ActionFoundReceiver, filter); // Don't forget to unregister during onDestroy 

并创建BroadcastRecievier得到的UUID:

private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver(){ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

     if(BluetoothDevice.ACTION_UUID.equals(action)) { 
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID); 
     for (int i=0; i<uuidExtra.length; i++) { 
     Log.i("TEST-UUIDS","Device: " + device.getName() + ", " + device + ", Service: " + uuidExtra[i].toString()); 
     } 
     } 
     else if(BluetoothDevice.YOUR_OTHER_ACTION_1){ 
     //YOUR CODE FOR ACTION 1 

     } 
     else if(BluetoothDevice.YOUR_OTHER_ACTION_2){ 
     //YOUR CODE FOR ACTION 2 

     } 

    } 
    }; 

注意:不是每个蓝牙设备都显示他们的服务uuid​​s'

+0

谢谢你的回复米格尔。 –

+0

我试图使用下面的代码getUuids。但是device.getUuids();返回null。对于您的信息BluetoothDevice对象不为null,但device.getUuids()为null。 BluetoothDevice设备;尝试{ \t \t \t \t \t \t \t ParcelUuid [] uuids = device。getUuids(); \t \t \t \t \t \t \t为(ParcelUuid EP:的UUID){ \t \t \t \t \t \t \t \t Log.e( “UUID记载:”,ep.toString()); \t \t \t \t \t \t \t} \t \t \t \t \t \t}赶上(例外五){ \t \t \t \t \t \t \t Log.e( “Exceptoin E”,e.toString()); \t \t \t \t \t \t} –

+1

你使用他的地址,或通过扫描发现设备连接,并将它们与设备连接? –