我是Android编程的新手,并且有java概念,我想知道如何使用蓝牙无需配对或任何密码发送和接收数据(仅当两个设备都安装了我的应用程序时),是否有任何建议?
As far as I know it's impossible to send or receive data over RFCOMM sockets without pairing. I've tried this extensively with an application that I'm developing. My results are:
As @ethrbunny mentioned you can also just use WiFi, setup parallel server/client threads on each device, and send whatever you want. To discover services on a local network you can optionally use zeroconf.
I got the the following from this Google groups post by Kristopher Micinski . Hope it helps.
I believe the key to getting this to work is in the
mUuid
list.Take a close look at what this is doing:
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);
}
}
}
}
What this code does is looks to connect to the device, but how does it do so? It tries the socket multiple times, using multiple UUIDs for the session.
In essence it means that we can use UUID only once . So instead this application implements using seven UUIDs, then the server listens and accepts each UUID on the server side, this is what is done with the following code:
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);
}
}
Now, on the client side of things what is done? The client does not know how many active connections the server currently has.
If we have some agreed upon order that the clients must use we can simply use this, however, in our case, we simply just try each UUID in sequence until we "find the right one."
Short version: -- Use multiple UUIDs, you can only use one at once. So define seven (max for piconet usage) and try each one until you find the right one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.