【发布时间】:2014-08-03 11:31:22
【问题描述】:
我正在开发一个可以将数据传输到 4.0 蓝牙串行设备的 Android 应用。我由 LeGatt android 示例项目 (http://developer.android.com/samples/BluetoothLeGatt/index.html) 指导。在这个项目中,他们连接到设备,但没有传输数据。
对于 2.0 蓝牙,我可以创建一个 Socket、InputStream 和 OutputStream 来传输数据,如下所示:
protected BluetoothSocket mySocket = null;
private InputStream MyInStream;
private OutputStream MyOutStream;
try {
Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
} catch (Exception e) {
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
}
mySocket = tmp;
try {
mySocket.connect();
} catch (IOException e) {
textViewLog.append("\n"+e.getMessage());
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK 2");
}
try {
MyInStream = mySocket.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
try {
MyOutStream = mySocket.getOutputStream();
} catch (IOException e) {
textViewLog.append("\nERROR: "+e.getMessage());
}
try {
MyOutStream.write((letra+"\r").getBytes());
} catch (IOException e) {
textViewLog.append("\nERROR: "+e.getMessage());
}
但在 4.0 蓝牙中我无法创建 Socket,因为这种方法不起作用
try {
Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
} catch (Exception e) {
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
}
谁能帮助我使用我的 4.0 蓝牙设备进行数据传输。
【问题讨论】:
-
BLE 不需要使用输入/输出流!它的工作方式不同!!!
标签: java android sockets bluetooth transmission