有没有达人推荐点思路 ,或者解答下 android bluetooth socket可否同时建立两个连接
核心是创建两个相同的uuid mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 我的qq1149598411,需要的话给你发一份 public class ConnectionService extends Service { public static final String TAG = "net.clc.bt.ConnectionService"; private ArrayList<UUID> mUuid; private ConnectionService mSelf; private String mApp; // Assume only one app can use this at a time; may // change this later FetchData fd = new FetchData(); private IConnectionCallback mCallback; private ArrayList<String> mBtDeviceAddresses; private HashMap<String, BluetoothSocket> mBtSockets; private HashMap<String, Thread> mBtStreamWatcherThreads; private BluetoothAdapter mBtAdapter;
public ConnectionService() { mSelf = this; mBtAdapter = BluetoothAdapter.getDefaultAdapter(); mApp = ""; mBtSockets = new HashMap<String, BluetoothSocket>(); mBtDeviceAddresses = new ArrayList<String>(); mBtStreamWatcherThreads = new HashMap<String, Thread>(); mUuid = new ArrayList<UUID>(); mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); /* * mUuid.add(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666")); * mUuid.add(UUID.fromString("503c7430-bc23-11de-8a39-0800200c9a66")); * mUuid.add(UUID.fromString("503c7431-bc23-11de-8a39-0800200c9a66")); * mUuid.add(UUID.fromString("503c7432-bc23-11de-8a39-0800200c9a66")); * mUuid.add(UUID.fromString("503c7433-bc23-11de-8a39-0800200c9a66")); * mUuid.add(UUID.fromString("503c7434-bc23-11de-8a39-0800200c9a66")); * mUuid.add(UUID.fromString("503c7435-bc23-11de-8a39-0800200c9a66")); */ } @Override public IBinder onBind(Intent arg0) { return mBinder; } private class BtStreamWatcher implements Runnable { private String address; public BtStreamWatcher(String deviceAddress) { address = deviceAddress; } public void run() { BluetoothSocket bSock = mBtSockets.get(address); try { InputStream instream = bSock.getInputStream(); String message = ""; BufferedReader bufferedReader; while (true) { message = ""; if (instream.available() > 0) { bufferedReader = new BufferedReader( new InputStreamReader(instream)); if ((message = bufferedReader.readLine()) != null) { Thread.sleep(0); mCallback.messageReceived(address, message); } } } } catch (IOException e) { Log .i( TAG, "IOException in BtStreamWatcher - probably caused by normal disconnection", e); } catch (RemoteException e) { Log .e( TAG, "RemoteException in BtStreamWatcher while reading data", e); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } // Getting out of the while loop means the connection is dead. try { mBtDeviceAddresses.remove(address); mBtSockets.remove(address); mBtStreamWatcherThreads.remove(address); mCallback.connectionLost(address); } catch (RemoteException e) { Log .e( TAG, "RemoteException in BtStreamWatcher while disconnecting", e); } } } private class ConnectionWaiter implements Runnable { private String srcApp; private int maxConnections; public ConnectionWaiter(String theApp, int connections) { srcApp = theApp; maxConnections = connections; } public void run() { try { for (int i = 0; i < Connection.MAX_SUPPORTED && maxConnections > 0; i++) { BluetoothServerSocket myServerSocket = mBtAdapter .listenUsingRfcommWithServiceRecord(srcApp, mUuid .get(i)); BluetoothSocket myBSock = myServerSocket.accept(); myServerSocket.close(); // Close the socket now that the // connection has been made. String address = myBSock.getRemoteDevice().getAddress(); mBtSockets.put(address, myBSock); mBtDeviceAddresses.add(address); Thread mBtStreamWatcherThread = new Thread( new BtStreamWatcher(address)); mBtStreamWatcherThread.start(); mBtStreamWatcherThreads .put(address, mBtStreamWatcherThread); maxConnections = maxConnections - 1; if (mCallback != null) { mCallback.incomingConnection(address); } } if (mCallback != null) { mCallback.maxConnectionsReached(); } } catch (IOException e) { Log.i(TAG, "IOException in ConnectionService:ConnectionWaiter", e); } catch (RemoteException e) { Log .e( TAG, "RemoteException in ConnectionService:ConnectionWaiter", e); } } } private BluetoothSocket getConnectedSocket(BluetoothDevice myBtServer, UUID uuidToTry) { BluetoothSocket myBSock; try { myBSock = myBtServer.createRfcommSocketToServiceRecord(uuidToTry); myBSock.connect(); return myBSock; } catch (IOException e) { Log.i(TAG, "IOException in getConnectedSocket", e); } return null; } private final IConnection.Stub mBinder = new IConnection.Stub() { public int startServer(String srcApp, int maxConnections) throws RemoteException { if (mApp.length() > 0) { return Connection.FAILURE; } mApp = srcApp; (new Thread(new ConnectionWaiter(srcApp, maxConnections))).start(); Intent i = new Intent(); i.setClass(mSelf, StartDiscoverableModeActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i); return Connection.SUCCESS; } public int connect(String srcApp, String device) throws RemoteException { if (mApp.length() > 0) { return Connection.FAILURE; } mApp = srcApp; BluetoothDevice myBtServer = mBtAdapter.getRemoteDevice(device); BluetoothSocket myBSock = null; for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) { for (int j = 0; j < 3 && myBSock == null; j++) { myBSock = getConnectedSocket(myBtServer, mUuid.get(i)); if (myBSock == null) { try { Thread.sleep(200); } catch (InterruptedException e) { Log.e(TAG, "InterruptedException in connect", e); } } } } if (myBSock == null) { return Connection.FAILURE; } mBtSockets.put(device, myBSock); mBtDeviceAddresses.add(device); Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher( device)); mBtStreamWatcherThread.start(); mBtStreamWatcherThreads.put(device, mBtStreamWatcherThread); return Connection.SUCCESS; } public int broadcastMessage(String srcApp, String message) throws RemoteException { if (!mApp.equals(srcApp)) { return Connection.FAILURE; } for (int i = 0; i < mBtDeviceAddresses.size(); i++) { sendMessage(srcApp, mBtDeviceAddresses.get(i), message); } return Connection.SUCCESS; } public String getConnections(String srcApp) throws RemoteException { if (!mApp.equals(srcApp)) { return ""; } String connections = ""; for (int i = 0; i < mBtDeviceAddresses.size(); i++) { connections = connections + mBtDeviceAddresses.get(i) + ","; } return connections; } public int getVersion() throws RemoteException { try { PackageManager pm = mSelf.getPackageManager(); PackageInfo pInfo = pm .getPackageInfo(mSelf.getPackageName(), 0); return pInfo.versionCode; } catch (NameNotFoundException e) { Log.e(TAG, "NameNotFoundException in getVersion", e); } return 0; } public int registerCallback(String srcApp, IConnectionCallback cb) throws RemoteException { if (!mApp.equals(srcApp)) { return Connection.FAILURE; } mCallback = cb; return Connection.SUCCESS; } public int sendMessage(String srcApp, String destination, String message) throws RemoteException { if (!mApp.equals(srcApp)) { return Connection.FAILURE; } try { BluetoothSocket myBsock = mBtSockets.get(destination); if (myBsock != null) { OutputStream outStream = myBsock.getOutputStream(); byte[] stringAsBytes = (message + " ").getBytes(); stringAsBytes[stringAsBytes.length - 1] = 0; // Add a stop // er outStream.write(stringAsBytes); return Connection.SUCCESS; } } catch (IOException e) { Log.i(TAG, "IOException in sendMessage - Dest:" + destination + ", Msg:" + message, e); } return Connection.FAILURE; } public void shutdown(String srcApp) throws RemoteException { try { for (int i = 0; i < mBtDeviceAddresses.size(); i++) { BluetoothSocket myBsock = mBtSockets.get(mBtDeviceAddresses .get(i)); myBsock.close(); } mBtSockets = new HashMap<String, BluetoothSocket>(); mBtStreamWatcherThreads = new HashMap<String, Thread>(); mBtDeviceAddresses = new ArrayList<String>(); mApp = ""; } catch (IOException e) { Log.i(TAG, "IOException in shutdown", e); } } public int unregisterCallback(String srcApp) throws RemoteException { if (!mApp.equals(srcApp)) { return Connection.FAILURE; } mCallback = null; return Connection.SUCCESS; } public String getAddress() throws RemoteException { return mBtAdapter.getAddress(); } public String getName() throws RemoteException { return mBtAdapter.getName(); } };}
mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
我的qq1149598411,需要的话给你发一份
public class ConnectionService extends Service {
public static final String TAG = "net.clc.bt.ConnectionService"; private ArrayList<UUID> mUuid; private ConnectionService mSelf; private String mApp; // Assume only one app can use this at a time; may // change this later
FetchData fd = new FetchData();
private IConnectionCallback mCallback; private ArrayList<String> mBtDeviceAddresses; private HashMap<String, BluetoothSocket> mBtSockets; private HashMap<String, Thread> mBtStreamWatcherThreads; private BluetoothAdapter mBtAdapter;
public ConnectionService() {
mSelf = this;
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
mApp = "";
mBtSockets = new HashMap<String, BluetoothSocket>();
mBtDeviceAddresses = new ArrayList<String>();
mBtStreamWatcherThreads = new HashMap<String, Thread>();
mUuid = new ArrayList<UUID>(); mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mUuid.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
/*
* mUuid.add(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
* mUuid.add(UUID.fromString("503c7430-bc23-11de-8a39-0800200c9a66"));
* mUuid.add(UUID.fromString("503c7431-bc23-11de-8a39-0800200c9a66"));
* mUuid.add(UUID.fromString("503c7432-bc23-11de-8a39-0800200c9a66"));
* mUuid.add(UUID.fromString("503c7433-bc23-11de-8a39-0800200c9a66"));
* mUuid.add(UUID.fromString("503c7434-bc23-11de-8a39-0800200c9a66"));
* mUuid.add(UUID.fromString("503c7435-bc23-11de-8a39-0800200c9a66"));
*/
} @Override
public IBinder onBind(Intent arg0) {
return mBinder;
} private class BtStreamWatcher implements Runnable {
private String address; public BtStreamWatcher(String deviceAddress) {
address = deviceAddress;
} public void run() { BluetoothSocket bSock = mBtSockets.get(address);
try {
InputStream instream = bSock.getInputStream(); String message = "";
BufferedReader bufferedReader;
while (true) {
message = "";
if (instream.available() > 0) {
bufferedReader = new BufferedReader(
new InputStreamReader(instream));
if ((message = bufferedReader.readLine()) != null) {
Thread.sleep(0);
mCallback.messageReceived(address, message);
}
}
}
} catch (IOException e) {
Log
.i(
TAG,
"IOException in BtStreamWatcher - probably caused by normal disconnection",
e);
} catch (RemoteException e) {
Log
.e(
TAG,
"RemoteException in BtStreamWatcher while reading data",
e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Getting out of the while loop means the connection is dead.
try {
mBtDeviceAddresses.remove(address);
mBtSockets.remove(address);
mBtStreamWatcherThreads.remove(address);
mCallback.connectionLost(address);
} catch (RemoteException e) {
Log
.e(
TAG,
"RemoteException in BtStreamWatcher while disconnecting",
e);
}
}
} private class ConnectionWaiter implements Runnable {
private String srcApp; private int maxConnections; public ConnectionWaiter(String theApp, int connections) {
srcApp = theApp;
maxConnections = connections;
} public void run() {
try {
for (int i = 0; i < Connection.MAX_SUPPORTED
&& maxConnections > 0; i++) {
BluetoothServerSocket myServerSocket = mBtAdapter
.listenUsingRfcommWithServiceRecord(srcApp, mUuid
.get(i));
BluetoothSocket myBSock = myServerSocket.accept();
myServerSocket.close(); // Close the socket now that the
// connection has been made. String address = myBSock.getRemoteDevice().getAddress(); mBtSockets.put(address, myBSock);
mBtDeviceAddresses.add(address);
Thread mBtStreamWatcherThread = new Thread(
new BtStreamWatcher(address));
mBtStreamWatcherThread.start();
mBtStreamWatcherThreads
.put(address, mBtStreamWatcherThread);
maxConnections = maxConnections - 1;
if (mCallback != null) {
mCallback.incomingConnection(address);
}
}
if (mCallback != null) {
mCallback.maxConnectionsReached();
}
} catch (IOException e) {
Log.i(TAG, "IOException in ConnectionService:ConnectionWaiter",
e);
} catch (RemoteException e) {
Log
.e(
TAG,
"RemoteException in ConnectionService:ConnectionWaiter",
e);
}
}
} private BluetoothSocket getConnectedSocket(BluetoothDevice myBtServer,
UUID uuidToTry) {
BluetoothSocket myBSock;
try {
myBSock = myBtServer.createRfcommSocketToServiceRecord(uuidToTry);
myBSock.connect();
return myBSock;
} catch (IOException e) {
Log.i(TAG, "IOException in getConnectedSocket", e);
}
return null;
} private final IConnection.Stub mBinder = new IConnection.Stub() {
public int startServer(String srcApp, int maxConnections)
throws RemoteException {
if (mApp.length() > 0) {
return Connection.FAILURE;
}
mApp = srcApp;
(new Thread(new ConnectionWaiter(srcApp, maxConnections))).start();
Intent i = new Intent();
i.setClass(mSelf, StartDiscoverableModeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return Connection.SUCCESS;
} public int connect(String srcApp, String device) throws RemoteException {
if (mApp.length() > 0) {
return Connection.FAILURE;
}
mApp = srcApp;
BluetoothDevice myBtServer = mBtAdapter.getRemoteDevice(device);
BluetoothSocket myBSock = null; for (int i = 0; i < Connection.MAX_SUPPORTED && myBSock == null; i++) {
for (int j = 0; j < 3 && myBSock == null; j++) {
myBSock = getConnectedSocket(myBtServer, mUuid.get(i));
if (myBSock == null) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException in connect", e);
}
}
}
}
if (myBSock == null) {
return Connection.FAILURE;
} mBtSockets.put(device, myBSock);
mBtDeviceAddresses.add(device);
Thread mBtStreamWatcherThread = new Thread(new BtStreamWatcher(
device));
mBtStreamWatcherThread.start();
mBtStreamWatcherThreads.put(device, mBtStreamWatcherThread);
return Connection.SUCCESS;
} public int broadcastMessage(String srcApp, String message)
throws RemoteException {
if (!mApp.equals(srcApp)) {
return Connection.FAILURE;
}
for (int i = 0; i < mBtDeviceAddresses.size(); i++) {
sendMessage(srcApp, mBtDeviceAddresses.get(i), message);
}
return Connection.SUCCESS;
} public String getConnections(String srcApp) throws RemoteException {
if (!mApp.equals(srcApp)) {
return "";
}
String connections = "";
for (int i = 0; i < mBtDeviceAddresses.size(); i++) {
connections = connections + mBtDeviceAddresses.get(i) + ",";
}
return connections;
} public int getVersion() throws RemoteException {
try {
PackageManager pm = mSelf.getPackageManager();
PackageInfo pInfo = pm
.getPackageInfo(mSelf.getPackageName(), 0);
return pInfo.versionCode;
} catch (NameNotFoundException e) {
Log.e(TAG, "NameNotFoundException in getVersion", e);
}
return 0;
} public int registerCallback(String srcApp, IConnectionCallback cb)
throws RemoteException {
if (!mApp.equals(srcApp)) {
return Connection.FAILURE;
}
mCallback = cb;
return Connection.SUCCESS;
} public int sendMessage(String srcApp, String destination, String message)
throws RemoteException {
if (!mApp.equals(srcApp)) {
return Connection.FAILURE;
}
try {
BluetoothSocket myBsock = mBtSockets.get(destination);
if (myBsock != null) {
OutputStream outStream = myBsock.getOutputStream();
byte[] stringAsBytes = (message + " ").getBytes();
stringAsBytes[stringAsBytes.length - 1] = 0; // Add a stop
// er
outStream.write(stringAsBytes);
return Connection.SUCCESS;
}
} catch (IOException e) {
Log.i(TAG, "IOException in sendMessage - Dest:" + destination
+ ", Msg:" + message, e);
}
return Connection.FAILURE;
} public void shutdown(String srcApp) throws RemoteException {
try {
for (int i = 0; i < mBtDeviceAddresses.size(); i++) {
BluetoothSocket myBsock = mBtSockets.get(mBtDeviceAddresses
.get(i));
myBsock.close();
}
mBtSockets = new HashMap<String, BluetoothSocket>();
mBtStreamWatcherThreads = new HashMap<String, Thread>();
mBtDeviceAddresses = new ArrayList<String>();
mApp = "";
} catch (IOException e) {
Log.i(TAG, "IOException in shutdown", e);
}
} public int unregisterCallback(String srcApp) throws RemoteException {
if (!mApp.equals(srcApp)) {
return Connection.FAILURE;
}
mCallback = null;
return Connection.SUCCESS;
} public String getAddress() throws RemoteException {
return mBtAdapter.getAddress();
} public String getName() throws RemoteException {
return mBtAdapter.getName();
}
};}