研究android蓝牙连接时,在网上找了个例子,作为客户端在调用connect函数时失败。经查资料
将
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));
改为
Method m; try { m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); try { socket = (BluetoothSocket) m.invoke(device, 1); _bluetooth.cancelDiscovery(); try { socket.connect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); }
就可以连接。
原因还没查出来。做个标记。
有人说用00001101-0000-1000-8000-00805F9B34FB这个UUID就可以。
但我测试使用这个UUID同样会出现:
10-26 08:04:20.196: ERROR/ClientSocketActivity(2147): java.io.IOException: Service discovery failed