Java 如何在Android中从蓝牙插座获取数据?

Java 如何在Android中从蓝牙插座获取数据?,java,android,sockets,bluetooth,Java,Android,Sockets,Bluetooth,我对安卓系统的编码还不熟悉。我一直在关注《专业Android 4应用程序开发》一书,我基本上是想找到一种方法,通过蓝牙接收任何设备发送的一串字符串。这就是我目前所拥有的 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up the window layout setContentView(R.layout.data); Li

我对安卓系统的编码还不熟悉。我一直在关注《专业Android 4应用程序开发》一书,我基本上是想找到一种方法,通过蓝牙接收任何设备发送的一串字符串。这就是我目前所拥有的

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set up the window layout
    setContentView(R.layout.data);

    ListView myData = (ListView)findViewById(R.id.list);
    final ArrayList<String> mData = new ArrayList<String>();
    final ArrayAdapter<String> aa;

    aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mData);

    //Bind Array to List View

    myData.setAdapter(aa);

    startDiscovery();

    String add = BluetoothDevice.EXTRA_DEVICE;

    BluetoothDevice device = mAdapter.getRemoteDevice(add);
    connectToServerSocket(device, MY_UUID);     

    mData.add(0, incoming);
    aa.notifyDataSetChanged();

}
我的数据监听如下所示:

private void listenforData(BluetoothSocket socket, StringBuilder incoming){
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        try{
            InputStream instream = socket.getInputStream();
        int bytesRead = -1;

        while(true){
        bytesRead = instream.read(buffer);
        if (bytesRead != -1){
            String result = "";
            while((bytesRead == bufferSize)&&(buffer[bufferSize-1]!=0)){
            result = result + new String(buffer,0,bytesRead-1);
            bytesRead= instream.read(buffer);
            }
            result = result + new String(buffer, 0, bytesRead -1);
            incoming.append(result);
        }
        socket.close();
        }
    }catch (IOException e){
        Log.e(TAG, "Message received failed.", e);
    }
    finally{
    }
}}

我缺少的是如何从套接字侦听器检索字符串并将其放置在ListView上。我需要一些指导。我这样做是否正确?

为什么不在这里阅读开发文档???并尝试他们的示例代码???@Tim我读了那个网页,我觉得我有几乎相同的代码,但我不知道如何将数据发送回OnCreate。在您发送给我的示例中,他们使用mHandler.obtainMessage(MESSAGE_READ,bytes,-1,buffer);我真的不确定如何在我的程序中实现这一点。我需要将字符串添加到我拥有的ListView中。你知道怎么做吗?改变你的标题,它不是关于BLE,是关于handler,读一下Android!你需要实现你的onCreate!因此,您可以使用处理程序将数据发送到OnCreate和receiver以获取数据!
private void connectToServerSocket(BluetoothDevice device, UUID uuid){
try{
        BluetoothSocket clientSocket = device.createRfcommSocketToServiceRecord(uuid);
        clientSocket.connect();
        StringBuilder incoming = new StringBuilder();
        listenforData(clientSocket, incoming);

        transferSocket = clientSocket;
    }catch(IOException e){
        Log.e("BLUETOOTH","Bluetooth client I/O Exception", e);
    }
}
private void listenforData(BluetoothSocket socket, StringBuilder incoming){
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        try{
            InputStream instream = socket.getInputStream();
        int bytesRead = -1;

        while(true){
        bytesRead = instream.read(buffer);
        if (bytesRead != -1){
            String result = "";
            while((bytesRead == bufferSize)&&(buffer[bufferSize-1]!=0)){
            result = result + new String(buffer,0,bytesRead-1);
            bytesRead= instream.read(buffer);
            }
            result = result + new String(buffer, 0, bytesRead -1);
            incoming.append(result);
        }
        socket.close();
        }
    }catch (IOException e){
        Log.e(TAG, "Message received failed.", e);
    }
    finally{
    }
}}