Android應用程序可以獲得SDP註冊到特定UUID的遠程設備上可用套接字的列表嗎?我可以看到如何使用給定的UUID連接到單個套接字,但我無法找到一種方法來遍歷所有共享該UUID的套接字。使用特定UUID發現所有服務的藍牙
我知道我的代碼是廢話。我知道我不應該在活動中進行阻止操作,並且這裏的所有內容都是非常黑客。我只是試圖在我正確設計這個構思之前獲得概念證明。
這就是說,這是我的代碼。我採取了一種不起作用的方法。
package {intentionally removed};
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.UUID;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 1;
private String MY_TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
}
if (!mBluetoothAdapter.isEnabled()) {
//Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast error = Toast.makeText(getApplicationContext(), "Enable BT and try again", Toast.LENGTH_LONG);
error.show();
Log.e(MY_TAG, "BT not enabled. Quitting.");
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
Log.i(MY_TAG, "Found " + pairedDevices.size() + " bonded devices");
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
int i = 1;
while (true) {
Log.i(MY_TAG, "Device " + device.getAddress() + " with name " + device.getName());
//Repeatedly try to connect to the SDP UUID; move to the next device when this fails.
BluetoothSocket s = null;
try {
s = device.createRfcommSocketToServiceRecord(UUID.fromString("{intentionally removed}"));
Thread.sleep(1000);
try{
s.connect();
}
catch (Exception e) {
Log.i(MY_TAG, "could not connect to socket.");
break;
}
Log.i(MY_TAG, "Connected to a socket! Whee! " + i + " found.");
InputStream is = s.getInputStream();
Log.i(MY_TAG, "got the stream");
while (true) {
Log.i(MY_TAG, "began read loop");
int j = -1;
try {
j = is.read();
}
catch (Exception e)
{
//RESUME NEXT...mwahahaha
}
if (j == -1) {
Log.i(MY_TAG, "ran out of ibytes");
break;
}
Log.i(MY_TAG, "ibyte: " + is.read());
}
i++;
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(MY_TAG, "Unable to connect.", e);
break;
}
}
}
}
}
@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 onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
}
(的UUID我拉,因爲我不想透露我離開的時候了。這是別人的。)
我知道那裏是在另一端的服務相同的UUID多個套接字。我剛纔給出的代碼嘗試連接到每個套接字,迫使createRfcommSocketToServiceRecord
抓住另一個套接字。然而,我無法連接的套接字之一...讓我無法迭代到下一個套接字(因爲createRfcommSocketToServiceRecord
剛剛返回相同的套接字下一次迭代)。
有沒有更好的方法來做到這一點?我想要一個很好的函數,我可以調用哪些列表列出了可以使用給定的UUID連接的所有套接字。這樣的事情存在嗎?
謝謝