2015-09-07 56 views
0

我有一个简单的问题:蓝牙发送文本到其他设备

第一,这是我的所有代码:

包com.example.mybluetoothapp;

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Set; 
import java.util.UUID; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>(); 
    BroadcastReceiver broadcastReceiver; 
    BluetoothAdapter bluetoothAdapter; 
    public UUID serverUuid = UUID.fromString("SERVEUR"); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

     if (bluetoothAdapter == null) { 
      Toast.makeText(this, "Votre appareil n'a pas de Bluetooth", 
        Toast.LENGTH_LONG).show(); 
      finish(); 
     } 

     if (!bluetoothAdapter.isEnabled()) { 
      Intent bluetoothIntent = new Intent(
        BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(bluetoothIntent, 1); 
     } 

     Set<BluetoothDevice> pairedDevices = bluetoothAdapter 
       .getBondedDevices(); 
     if (pairedDevices.size() > 0) { 
      for (BluetoothDevice bluetoothDevice : pairedDevices) { 
       devices.add(bluetoothDevice); 
      } 
     } 

     broadcastReceiver = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
        BluetoothDevice device = intent 
          .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        devices.add(device); 
       } 
      } 
     }; 

     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(broadcastReceiver, filter); 

     Intent dicoverableIntent = new Intent(
       BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
     dicoverableIntent.putExtra(
       BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); 
     startActivity(dicoverableIntent); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    protected void onDestroy() { 
     unregisterReceiver(broadcastReceiver); 
     super.onDestroy(); 
    } 

    private class AcceptThread extends Thread { 

     BluetoothServerSocket bluetoothServerSocket; 

     public AcceptThread() { 
      BluetoothServerSocket serverSocketTmp = null; 
      try { 
       serverSocketTmp = bluetoothAdapter 
         .listenUsingRfcommWithServiceRecord("Bluetooth APP", 
           serverUuid); 
       bluetoothServerSocket = serverSocketTmp; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void run() { 
      BluetoothSocket socket = null; 
      while (true) { 
       try { 
        socket = bluetoothServerSocket.accept(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       if (socket != null) { 
        // TODO: Managed Connection 

        // TODO: Envoyer Bonjour et Bienvenue 

        try { 
         bluetoothServerSocket.close(); 
         break; 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      super.run(); 
     } 

     private void cancel() { 
      try { 
       bluetoothServerSocket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

    } 

    private class ConnectThread extends Thread { 

     BluetoothSocket socket; 
     BluetoothDevice device; 

     public ConnectThread(BluetoothDevice device) { 
      this.device = device; 

      BluetoothSocket socketTmp = null; 
      try { 
       socketTmp = device 
         .createRfcommSocketToServiceRecord(serverUuid); 
       socket = socketTmp; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void run() { 
      bluetoothAdapter.cancelDiscovery(); 

      try { 
       socket.connect(); 
      } catch (IOException e) { 
       try { 
        socket.close(); 
       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 
       e.printStackTrace(); 
       return; 
      } 

      // TODO: Manage connection 

      super.run(); 
     } 

     private void cancel() { 
      try { 
       socket.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

    } 

    private class ConnectedThread extends Thread { 
     private BluetoothSocket bluetoothSocket; 
     InputStream inputStream = null; 
     OutputStream outputStream = null; 

     public ConnectedThread(BluetoothSocket socket) { 
      bluetoothSocket = socket; 

      InputStream inputStreamTmp = null; 
      OutputStream outputStreamTmp = null; 

      try { 
       inputStreamTmp = bluetoothSocket.getInputStream(); 
       outputStreamTmp = bluetoothSocket.getOutputStream(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      inputStream = inputStreamTmp; 
      outputStream = outputStreamTmp; 
     } 

     @Override 
     public void run() { 
      byte[] buffer = new byte[1024]; 
      int bytes; 

      while (true) { 
       try { 
        bytes = inputStream.read(buffer); 
        // TODO: Envoyer message à l'affichage 
       } catch (IOException e) { 
        e.printStackTrace(); 
        break; 
       } 
      } 

      super.run(); 
     } 

     public void write(byte[] bytes) { 
      try { 
       outputStream.write(bytes); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

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

    } 

} 

我得到这个代码从一个网站,所以请我怎么可以我们就发送一个“世界你好” bettween 2个智能手机?

请我需要帮助

谢谢

+1

在SO上张贴之前尝试一些东西。如果我们看到努力,我们将帮助您。 –

+0

我发誓所有的测试,但我不知道如何发送文本,请帮助我 – Misskaty

+0

你有没有尝试过照顾这[链接](http://grepcode.com/file/repository.grepcode.com/java/ext/com .google.android /机器人的应用程式/ 4.2_r1/COM /示例/机器人/ BluetoothChat/BluetoothChat.java)。 –

回答

0

使用ConnectedThread

如何发送任何数据。

//Code to send text 
String text = "Example"; 
mConnectedThread.write(text.getBytes()); 

如何接收发送文本(数组长度为1024首和1024个字节为1 KB),修改run()方法ConnectedThread

@Override 
public void run() { 
    byte[] buffer = new byte[1024]; 
    int bytes; 

    while (true) { 
     try { 
      bytes = inputStream.read(buffer); 

      //Received text is here 
      String text = new String(buffer, 0, bytes); 
      Log.d(TAG, text); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      break; 
     } 
    } 

    super.run(); 
} 

检查你的代码后,似乎线程避风港” t已经开始