![](/img/trans.png)
[英]How I can connect and send data in android with bluetooth between two devices?
[英]Android bluetooth connect two or more devices and send data
嗨,我需要將3個不同的設備(客戶端)連接到第四個設備(服務器)。 這是我的服務器代碼:
public class MainServerActivity extends Activity {
ArrayAdapter<String> listAdapter;
ListView listView;
TextView textViewTextoRecibido;
BluetoothAdapter btADapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
IntentFilter filter;
BroadcastReceiver receiver;
public AcceptThread acceptThread;
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static final String NAME = "Tablet_Madre";
//public static final UUID MY_UUID = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
ConnectedThread connectedThread;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS_CONNECT:
connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), "Dispositivo conectado!", Toast.LENGTH_LONG).show();
//String s = "successfully connected";
//connectedThread.write(s.getBytes());
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String string = new String(readBuf);
textViewTextoRecibido.setText(string);
Toast.makeText(getApplicationContext(), "Llego el mensaje!!!!", Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_server);
init();
if (btADapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth detectado", Toast.LENGTH_LONG).show();
finish();
} else {
if (!btADapter.isEnabled()) {
turnOnBT();
}
//getPairedDevices();
//startDiscovery();
aceptarConexiones();
}
}
private void aceptarConexiones() {
try {
acceptThread = new AcceptThread();
//acceptThread.run();
} catch (Exception e) {
System.out.print(e);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
try {
acceptThread.run();
//start();
} catch (Exception e) {
System.out.print(e);
}
}
private void startDiscovery() {
// TODO Auto-generated method stub
btADapter.cancelDiscovery();
btADapter.startDiscovery();
}
private void turnOnBT() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
/*Inicializa todos los componentes*/
private void init() {
listView = (ListView) findViewById(R.id.listView);
//listView.setOnItemClickListener(this);
textViewTextoRecibido = (TextView) findViewById(R.id.textViewTextoRecibido);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
listView.setAdapter(listAdapter);
btADapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Bluetooth debe estar encendido", Toast.LENGTH_SHORT).show();
finish();
}
}
public void enviarMensaje(View view){
String s = "Campeon del Siglo";
connectedThread.write(s.getBytes());
}
private class AcceptThread extends Thread{
public final BluetoothServerSocket mmServerSocket;
private int cantClientes;
public AcceptThread() {
BluetoothServerSocket tmp = null;
// Create a new listening server socket
try {
tmp = btADapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
//Log.e(TAG, "listen() failed", e);
}
mmServerSocket = tmp;
}
public void run(){
BluetoothSocket socket = null;
while(true){
try{
socket = mmServerSocket.accept();
//No se llama al socket.connect() porque esto ya los conecta.
} catch (IOException e){
break;
}
if(socket != null){
cantClientes = cantClientes + 1;
}
mHandler.obtainMessage(SUCCESS_CONNECT, socket).sendToTarget();
if (socket != null){
//manageConnectedSocket(socket);
try {
mmServerSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
public void cancel(){
try{
mmServerSocket.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;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
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
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}
這是我的客戶代碼:
public class MainClientActivity extends ActionBarActivity implements OnItemClickListener {
ArrayAdapter<String> listAdapter;
ListView listView;
TextView textViewTextoRecibido;
BluetoothAdapter btADapter;
Set<BluetoothDevice> devicesArray;
ArrayList<String> pairedDevices;
ArrayList<BluetoothDevice> devices;
IntentFilter filter;
BroadcastReceiver receiver;
protected static final int SUCCESS_CONNECT = 0;
protected static final int MESSAGE_READ = 1;
public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static final String NAME = "Tablet_Madre";
//public static final UUID MY_UUID = UUID.fromString("00001105-0000-1000-8000-00805F9B34FB");
ConnectedThread connectedThread;
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SUCCESS_CONNECT:
connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
Toast.makeText(getApplicationContext(), "Dispositivo conectado!", Toast.LENGTH_LONG).show();
//String s = "successfully connected";
//connectedThread.write(s.getBytes());
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String string = new String(readBuf);
textViewTextoRecibido.setText(string);
Toast.makeText(getApplicationContext(), "Llego el mensaje!!!!", Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_client);
init();
if (btADapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth detectado", Toast.LENGTH_LONG).show();
finish();
} else {
if (!btADapter.isEnabled()) {
turnOnBT();
}
getPairedDevices();
startDiscovery();
}
}
/*Inicializa todos los componentes*/
private void init() {
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemClickListener(this);
textViewTextoRecibido = (TextView) findViewById(R.id.textViewTextoRecibido);
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 0);
listView.setAdapter(listAdapter);
btADapter = BluetoothAdapter.getDefaultAdapter();
pairedDevices = new ArrayList<String>();
filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
devices = new ArrayList<BluetoothDevice>();
receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
boolean yaEstaListado = false;
//Si encuentro uno, me fijo is ya esta en la lista de paireds
for (int j = 0; j < pairedDevices.size(); j++) {
if (device.getName().equals(pairedDevices.get(j))) {
yaEstaListado = true;
break;
}
}
if(!yaEstaListado){
listAdapter.add(device.getName() + "\n" + device.getAddress());
devices.add(device);
}
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
if (btADapter.getState() == btADapter.STATE_OFF) {
turnOnBT();
}
}
}
};
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
}
private void startDiscovery() {
btADapter.cancelDiscovery();
btADapter.startDiscovery();
}
public void buscarDispositivo(View view){
startDiscovery();
}
private void turnOnBT() {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
devicesArray = btADapter.getBondedDevices();
if (devicesArray.size() > 0) {
for (BluetoothDevice device : devicesArray) {
pairedDevices.add(device.getName());
listAdapter.add(device.getName() + " (Paired) " + "\n" + device.getAddress());
devices.add(device);
}
}
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Bluetooth debe estar encendido", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
if(btADapter.isDiscovering()){
btADapter.cancelDiscovery();
}
BluetoothDevice selectedDevice = devices.get(position);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
} catch (Exception e) {
System.out.println(e);
}
}
public void enviarMensaje(View view){
String s = "Carbonero querido";
connectedThread.write(s.getBytes());
}
private class ConnectThread extends Thread {
private BluetoothSocket mmSocket = null;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
// BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
mmSocket = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
//Method m = mmDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
//mmSocket = (BluetoothSocket) m.invoke(mmDevice, 1);
} catch (IOException e) { }
}
public void run() {
// Cancel discovery because it will slow down the connection
btADapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
Thread.sleep(2000);
mmSocket.connect();
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
} catch (IOException connectException) {
System.out.println(connectException);
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) {
}
return;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println(e);
}
}
/** Will cancel an in-progress connection, and close the socket */
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;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
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
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}
我看到了一些答案,我想我不清楚。 現在,使用此代碼,我無法連接兩個設備,無法將數據從客戶端發送到服務器並向后發送。 問題的第二部分是我不知道如何從服務器端管理3個連接。 給我一些想法,例子...
我有四個問題:
1)我需要創建一個線程,以便可以接受一個以上的設備,因為現在正在接受一個,並且當我執行socket.accept()時,主線程處於凍結狀態;
2)有時連接斷開很好,但有時卻沒有。 如果您看到該代碼,則在連接時我會在兩個設備上都顯示一條消息,有時出現在一個設備中,有時出現在另一個設備中。
3)當我建立連接時,當我嘗試發送消息時,這不起作用。 我不知道是否必須在客戶端和服務器中都創建一個ConnectionThread,但我都在兩者中創建了,但是當我要在客戶端編寫時,connectionThread為null。
4)如何管理服務器中的3個線程,每個客戶端一個?
我希望我很清楚……有些名字是西班牙語。
映入眼簾。
您一次只能在兩個設備之間進行通信。
如果您嘗試使用此代碼與第三台設備通信,它將無法正常工作。
要在兩個以上的設備之間進行通信,您需要在開始與客戶端2的通信之前結束與客戶端1的通信。這將需要更多的代碼來以您希望的任何方式管理連接,無論您是否希望將它們放入某種形式的設備中。隊列。
這取決於您是要自動進行通信還是要手動啟動和停止設備之間的通信,以了解如何進行管理。
您可以遍歷可發現的設備並啟動和停止連接。 這將涉及為藍牙服務器調用和管理線程,以便關閉套接字,並為每個新連接進行新的初始化,以等待新連接。
這意味着您將需要跟蹤哪個設備已連接以及隊列中的下一個設備,並進行錯誤處理以在服務器准備好接受來自該設備的連接時檢查該設備是否可用。 由於確保設備的可用性,它相當復雜並且容易出錯,您將需要仔細地遍歷代碼,並確保在服務器就緒時為不可用的設備做好准備,以便讓服務器隨后循環到下一個可用的設備。
因此,對於服務器代碼,基本連接和線程管理與到一個客戶端的連接相同,但是由您的代碼調用和管理,以遍歷客戶端設備,或手動停止與一台設備通信並嘗試與其他設備的連接。
無論您是嘗試遍歷一個以上的客戶端設備還是僅處理一個客戶端,客戶端代碼都不必更改。 處理此類代碼將需要在服務器代碼中進行管理。
首先,您需要清楚地了解如何僅在兩個設備之間進行通信,並且不清楚您是否知道該如何做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.