通过蓝牙从android传输字符串到电子邮件

问题描述:

由于很多天,我正在经历很多与我的问题有关的例子和问题,但没有任何帮助!通过蓝牙从android传输字符串到电子邮件

目的

双向PC(窗口)之间的通信和Android(如通过味精PC和确认来自PC)

我做了什么至今

我能够从我的Android设备使用蓝牙发送字符串到PC(Windows)。

这里是我用

private String sendFile(String mac_address, String device_name) { 
    // TODO Auto-generated method stub 
    String result=""; 
    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    BluetoothDevice device = btAdapter.getRemoteDevice(mac_address); 
    Log.d("BT_SENDING_FILE",device_name+" "+mac_address); 


    try { 


     Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
     btSocket = (BluetoothSocket) m.invoke(device, 1); 
     if(!btSocket.isConnected()){ 
       Log.d(" is connected Status",""+btSocket.isConnected()); 
      // device.createInsecureRfcommSocketToServiceRecord(UUID); 

      } 
     btAdapter.cancelDiscovery(); 
     try{ 
       btSocket.connect(); 
       }catch(IOException e){ 
        btSocket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1); 
        btSocket.connect(); 
        Log.d("fall back","in catch clause"); 
       } 

     byte[] msgBuffer = stringToSend.getBytes(); 
      outStream = btSocket.getOutputStream(); 
       outStream.write(msgBuffer); 
       if (outStream != null) { 
        outStream.flush(); 

       } 
       result = "sent"; 
      outStream.close(); 
      btSocket.close(); 
      Log.d("BLUETOOTH","Closing Socket"); 
      } catch (Exception e) { 
       System.out.println(e); 
       Log.d("BLUETOOTH","exception while sending through bluetooth"); 
       result = "failed"; 
      e.getLocalizedMessage(); 
      } finally{}    
     return result; 
} 

,这是没有任何问题的良好运行的代码。

问题

,但我不能够接收来自PC的任何字符串到Android设备。我已经尝试了很多东西

我已经试过这

  BluetoothSocket socket = null; 
      BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); 

      while (true) { 

       try { 

        // Creating a new listening server socket 
        Log.d("BT", ".....Initializing RFCOMM SERVER...."); 


        mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("Bluetooth", MY_UUID); 





        // This is a blocking call and will only return on a 
        // successful connection or an exception  
        Log.d("Socket","listening..."); 
        socket = mmServerSocket.accept(120000);     

       } catch (Exception e) { 

       } 
       try { 
        Log.d("Socket", "Closing Server Socket....."); 
        mmServerSocket.close(); 

        InputStream tmpIn = null; 
        OutputStream tmpOut = null; 

        // Get the BluetoothSocket input and output streams 

        tmpIn = socket.getInputStream(); 
        tmpOut = socket.getOutputStream(); 
        Log.d("BT","IO stream init"); 
        DataInputStream mmInStream = new DataInputStream(tmpIn); 
        DataOutputStream mmOutStream = new DataOutputStream(tmpOut); 

        // use the Input Stream to take the string from the client whoever is connecting 
        //similarly use the output stream to send the data to the client 




        StringBuilder sb = new StringBuilder(); 
        while((ch = mmInStream.read())!= -1) 
         sb.append((char)ch); 

        String res = sb.toString(); 

        Log.d("BT",res); 
        tv_response.setText(res)); 
       } catch (Exception e) { 
        //catch your exception here 
        e.printStackTrace(); 
        Log.d("Exception","some thing went wrong"); 
        e.getLocalizedMessage(); 

       } 
      } 
     } 

我使用蓝科夫在窗口。 我已经通过蓝牙聊天示例工程但不明白任何事情

您的帮助表示赞赏

谢谢

我尝试下面的代码,它是成功的。它可以帮助你

BluetoothSocket socket = null; 
    while (true) { 
     try { 
      Log.i(ACCEPT_TAG, "Listening for a connection..."); 

      socket = mServerSocket.accept(); 
      Log.i(ACCEPT_TAG, "Connected to " + socket.getRemoteDevice().getName()); 

     } catch (IOException e) { 
      break; 
     } 
     // If a connection was accepted 
     if (socket != null) { 
      // Do work to manage the connection (in a separate thread) 
      try { 
       // Read the incoming string. 
       String buffer; 

       DataInputStream in = new DataInputStream(socket.getInputStream()); 

       buffer = in.readUTF(); 

       Intent i = new Intent(MESSAGE_RECEIVED_INTENT); 
       i.putExtra("Message", String.format("%s", buffer)); 
       Log.d("BT response is",buffer); 

       c.sendBroadcast(i); 
      } catch (IOException e) { 
       Log.e(ACCEPT_TAG, "Error obtaining InputStream from socket"); 
       e.printStackTrace(); 
      } 
      try { 
       mServerSocket.close(); 
      } catch (IOException e) { } 
      break; 
     } 
    } 

确保您的设备是所有即扫描模式下可见是BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE 否则使用此代码

public void requestBTDiscoverable() { 
    Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
    i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 

    startActivityForResult(i, REQ); 

    int result = 0; 

    this.onActivityResult(REQ, result, i); 
    Log.i(TAG, "Bluetooth discoverability enabled"); 
} 
+0

是!完美的代码..完美无缺 – jaimin 2014-09-27 08:06:34