1
試圖解決這個問題的最近2天。我在這個論壇上看到這個主題的很多解決方案,並嘗試其中的大多數,如This和This,但沒有運氣。因此,我問關於這個話題的新問題。所以任何人都可以指導我如何解決這個問題?特別是我認爲我不能使用正確的UUID(儘管我使用標準UUID 00001101-0000-1000-8000-00805F9B34FB)連接我的服務器PC(用java編寫)和客戶端Android。安卓藍牙服務發現失敗例外
服務器代碼(JAVA):
public class Main {
private final LocalDevice mLocalDevice;
public static void main(String[] args) throws IOException, InterruptedException
{
new Main().start();
}
public Main() throws IOException
{
mLocalDevice = LocalDevice.getLocalDevice();
mLocalDevice.setDiscoverable(DiscoveryAgent.GIAC);
}
public void start() throws IOException
{
StreamConnectionNotifier connectionNotifier =(StreamConnectionNotifier) Connector.open("btspp://localhost:" +
"0000110100001000800000805F9B34FB;name=BtExample;" +
"authenticate=false;encrypt=false;master=false");
System.out.println("accepting on " + mLocalDevice.getBluetoothAddress());
StreamConnection streamConnection = connectionNotifier.acceptAndOpen();
DataInputStream is = streamConnection.openDataInputStream();
byte[] bytes = new byte[1024];
int r;
while ((r = is.read(bytes)) > 0)
{
System.out.println(new String(bytes, 0, r));
}
streamConnection.close();
}
}
客戶端代碼(安卓):
try {
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Log.d("BT", "getting local device");
// remote MAC here:
BluetoothDevice device =mBtAdapter.getRemoteDevice("My_Computer_bluetooth_Address");
Log.d("BT", "remote device: "+ device.getName().toString());
Log.d("BT", "connecting to service");
//Process process = Runtime.getRuntime().exec("su -c 'sdptool records " + device.getAddress() + "'");
//Log.d("BT", process.toString());
//Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
//socket = (BluetoothSocket) m.invoke(device, 1);//I also tried this,but same exception occurs
//sock=socket;
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
Log.d("BT", "about to connect");
mBtAdapter.cancelDiscovery();
socket.connect();
Log.d("BT", "Connected!");
socket.getOutputStream().write("Hello, world!".getBytes());
} catch (Exception e) {
Log.e("BT", "Error connecting to device", e);
}
logcat的異常瀏覽:
02-15 14:12:02.022: D/BT(4798): getting local device
02-15 14:12:02.023: D/BT(4798): remote device: SHUVRO-PC //my PC name
02-15 14:12:02.023: D/BT(4798): connecting to service
02-15 14:12:02.024: I/BluetoothSocket_MTK(4798): [JSR82] Bluetooth Socket Constructor
02-15 14:12:02.024: I/BluetoothSocket_MTK(4798): [JSR82] type=1 fd=-1 auth=false encrypt=false port=-1
02-15 14:12:02.026: D/BT(4798): about to connect
02-15 14:12:02.055: I/BluetoothSocket_MTK(4798): [JSR82] connect: do SDP
02-15 14:12:08.057: I/BluetoothSocket_MTK(4798): [JSR82] SdpHelper::onRfcommChannelFound: channel=-1
02-15 14:12:08.059: E/BT(4798): Error connecting to device
02-15 14:12:08.059: E/BT(4798): java.io.IOException: Service discovery failed
02-15 14:12:08.059: E/BT(4798): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:813)
02-15 14:12:08.059: E/BT(4798): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:382)
02-15 14:12:08.059: E/BT(4798): at com.bluetoothTest.BluetoothTestActivity.onClick(BluetoothTestActivity.java:82)
02-15 14:12:08.059: E/BT(4798): at android.view.View.performClick(View.java:3517)
02-15 14:12:08.059: E/BT(4798): at android.view.View$PerformClick.run(View.java:14155)
02-15 14:12:08.059: E/BT(4798): at android.os.Handler.handleCallback(Handler.java:605)
02-15 14:12:08.059: E/BT(4798): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 14:12:08.059: E/BT(4798): at android.os.Looper.loop(Looper.java:137)
02-15 14:12:08.059: E/BT(4798): at android.app.ActivityThread.main(ActivityThread.java:4508)
02-15 14:12:08.059: E/BT(4798): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 14:12:08.059: E/BT(4798): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 14:12:08.059: E/BT(4798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
02-15 14:12:08.059: E/BT(4798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
02-15 14:12:08.059: E/BT(4798): at dalvik.system.NativeStart.main(Native Method)
我有類似的問題。任何線索解決這個問題? – ben75 2013-08-23 13:56:23
不,還沒有,我停止了那項工作,因爲我無法擺脫它!如果你發現讓我知道.. – ridoy 2013-08-23 15:21:25
@ ben75:在這裏相同的概率。你找到解決方案嗎? – Basher51 2014-03-14 02:11:12