Android蓝牙连接两个或多个设备并发送数据
嗨,我需要将3个不同的设备客户端连接到第四个设备服务器。 这是我的服务器代码:Android蓝牙连接两个或多个设备并发送数据,android,multithreading,sockets,android-bluetooth,Android,Multithreading,Sockets,Android Bluetooth,嗨,我需要将3个不同的设备客户端连接到第四个设备服务器。 这是我的服务器代码: public class MainServerActivity extends Activity { ArrayAdapter<String> listAdapter; ListView listView; TextView textViewTextoRecibido; BluetoothAdapter btADapter; Set<BluetoothDev
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个连接。给我一些想法,例子
我有4个问题:
1我需要创建一个线程,这样我就可以接受多个设备,因为现在正在接受一个设备,当我执行socket.accept时,主线程是冻结的
2有时断开连接可以正常工作,但有时不能。如果您看到代码,我会在连接时在两个设备中显示一条消息,有时出现在一个设备中,有时出现在另一个设备中
3当我有一个连接,当我试图发送消息时,这是不起作用的。我不知道是否必须在客户端和服务器中创建ConnectionRead,我在两者中都创建,但是当我要在客户端写入时,ConnectionRead为null
4如何从服务器管理3个线程,每个客户端一个
我希望我说的很清楚…有些名字是西班牙语的
问候。您一次只能在两台设备之间进行通信 如果您尝试使用此代码与第三台设备通信,则它将无法工作 要在两个以上的设备之间通信,您需要在开始与客户端2通信之前结束与客户端1的通信。这将需要更多的代码来以您希望的任何方式管理您的连接,无论您是否希望将它们放入某种队列中 这取决于您是想自动化通信,还是手动启动和停止设备之间的通信,以及如何管理这一点 您可以在可发现的设备中循环并启动和停止连接。这将涉及调用和管理蓝牙服务器的线程,以便关闭套接字,并对每个新连接进行等待新连接的新初始化 这意味着您需要跟踪已连接的设备和队列中的下一个设备,并进行错误处理,以便在服务器准备接受来自该设备的连接时检查该设备是否可用。这相当复杂,而且容易出错,因为要确保设备的可用性,您需要仔细检查代码,确保在服务器准备就绪时为不可用的设备做好准备,以便允许服务器循环到下一个可用设备 因此,在服务器代码的情况下,基本连接和线程管理与连接到一个客户机的情况相同,但这是由您的代码调用和管理的,以循环通过客户机设备,或手动停止与一个设备的通信并尝试与另一个设备的连接 客户机代码不必更改您是尝试通过多个客户机设备循环,还是仅处理一个客户机。处理此类代码需要在服务器代码中进行管理
首先,您需要清楚地了解如何在两台设备之间进行通信,我不清楚您是否知道如何进行通信 谢谢你的回答。我的第一步是只与两个设备通信,一个客户端和一个服务器,并将一些数据客户端发送到服务器并向后发送。这个我做不到,这个代码是错误的,我不知道在哪里。第二部分是你提到的。请看一些示例或告诉我一些想法如何管理来自服务器的3个连接。问题是我从未启动AcceptThread或ConnectedThread!!现在,我正在搜索如何将这些线程传递到另一个活动。我创建了一个类ManageBluetooth来管理连接。这是一个单身班。问题是我不知道如何从ManageBluetooth向设备已连接的活动提供建议。。。