Create a socket for 4.0 bluetooth transmission

[亡魂溺海] 提交于 2021-02-07 17:02:53

问题


I'm developing an Android app than can transmit data to a 4.0 Bluetooth serial device. I'm guiding by LeGatt android sample project (http://developer.android.com/samples/BluetoothLeGatt/index.html). In this project, they connect to the device, but nothing about transmission data.

For 2.0 bluetooth I can create a Socket, InputStream and OutputStream to transmit the data, something like this:

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());
                }

But in 4.0 Bluetooth I can't create the Socket, because this method doesn't works

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");
                        }

Can someone help me to reach the data transmission using my 4.0 bluetooth device.


回答1:


Android BLE works entirely different from the Bluetooth stack, read about BLE in Wikipedia.

To send a data using BLE, you need to place your data in characteristics and send it using the gatt!

1st, you need to check your BLE device, which characteristic is used for sending data and used that characteristics for sending data!

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

Please take note that Android BLE stack is buggy, you can only writeCharacteristics once at a time, as mention in the link below!!

You can check this post about Android BLE, it will give you a clear understanding of the how the Android BLE callbacks work!

Android BLE, read and write characteristics



来源:https://stackoverflow.com/questions/24196289/create-a-socket-for-4-0-bluetooth-transmission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!