我正在努力了解工作客户端 - 服务器蓝牙。 android是客户端的地方。 PC是服务器。
我找到了code。这工作了一个三星galaxy 10.1(android 3.2),但不起作用三星galaxy s(android 2.3.3)和htc野火s(android 2.2)。
MY_UUID = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb");
04-25 18:32:37.023: D/BluetoothCommandService(13665): setState() 1 -> 2
04-25 18:32:37.033: I/BluetoothCommandService(13665): BEGIN mConnectThread
04-25 18:32:37.063: E/BluetoothService.cpp(102): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
04-25 18:32:37.103: E/BluetoothCommandService(13665): Unable to start Service Discovery
04-25 18:32:37.103: E/BluetoothCommandService(13665): java.io.IOException: Unable to start Service Discovery
04-25 18:32:37.103: E/BluetoothCommandService(13665): at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:367)
04-25 18:32:37.103: E/BluetoothCommandService(13665): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201)
04-25 18:32:37.103: E/BluetoothCommandService(13665): at com.luugiathuy.apps.remotebluetooth.BluetoothCommandService$ConnectThread.run(BluetoothCommandService.java:258)
04-25 18:32:37.103: D/BluetoothCommandService(13665): setState() 2 -> 1
答案 0 :(得分:6)
您的设备无法进行服务发现(我认为如此)。
我已经阅读过许多三星设备和HTC设备的BT问题,但是那些专门用于L2CAP / HID配置文件的设备。 Soln:如果您使用的是L2CAP
如果您使用上述解决方案之一,请尝试使用标准UUID
SPP 00001101-0000-1000-8000-00805F9B34FB
RFCOMM 00000003-0000-1000-8000-00805F9B34FB
修改
或者,您可以尝试使用反射来获得类似于以下方法的套接字连接
private static BluetoothSocket createBluetoothSocket(
int type, int fd, boolean auth, boolean encrypt, String address, int port){
try {
Constructor<BluetoothSocket> constructor = BluetoothSocket.class.getDeclaredConstructor(
int.class, int.class,boolean.class,boolean.class,String.class, int.class);
constructor.setAccessible(true);
BluetoothSocket clientSocket = (BluetoothSocket)
constructor.newInstance(type,fd,auth,encrypt,address,port);
return clientSocket;
}catch (Exception e) { return null; }
}
答案 1 :(得分:2)
试试这个:
static int sdk = Integer.parseInt(Build.VERSION.SDK);
private static final UUID MY_UUID =
UUID.fromString((sdk<=8||sdk>=11)?"04c6093b-0000-1000-8000-00805f9b34fb":"00001101-0000-1000-8000-00805F9B34FB");
它对我有用:)
测试2.1 - 3.3和4.0.4安卓版本。