【问题标题】:Create a socket for 4.0 bluetooth transmission为4.0蓝牙传输创建一个socket
【发布时间】: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


【解决方案1】:

Android BLE 的工作方式与蓝牙堆栈完全不同,请在 Wikipedia 中阅读有关 BLE 的信息。

要使用 BLE 发送数据,您需要将数据放入特征中并使用 gatt 发送!

1st,你需要检查你的BLE设备,哪个特性用于发送数据,以及使用该特性来发送数据!

byte[] data; //Place your data into this array of byte
characteristic.setValue(data); 
gatt.writeCharacteristic(characteristic);

请注意,Android BLE 堆栈有问题,一次只能写一次Characteristics,如下面的链接所述!!

您可以查看这篇关于 Android BLE 的帖子,它将让您清楚地了解 Android BLE 回调的工作原理!

Android BLE, read and write characteristics

【讨论】:

  • 您好 Tim.Thaks 为您提供有用的答案。
  • 嗨@Tim,你好吗我已经浪费了很多时间试图通过特征发送数据,但我做不到!!! (stackoverflow.com/questions/24720029/…) (stackoverflow.com/questions/24686163/…) 请,如果您有关于此事的良好文档或执行此操作的完整项目,我将不胜感激。
  • 只使用 gatt.writeCharacteristic(characteristic);要发送数据,请确保您使用正确的特性,以及您使用的是哪个 BLE 模块???请咨询 BLE 设备制造商(用户手册),看看他们是否需要某种协议来发送数据!
猜你喜欢
  • 2016-07-10
  • 2014-07-08
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多