如何通过BlueTooth

时间:2016-03-22 16:50:53

标签: android bluetooth

我有3个编辑文本。来自这些edittexts的数据连接成一个字符串。我希望使用我的应用程序将此字符串通过蓝牙从我的手机发送到另一部手机中的相同应用程序。

蓝牙管理器的代码和获取设备的配对列表是:

   if (!bluetoothAdapter.isEnabled()) {
            startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 0);
            Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(getVisible, 0);
            Toast.makeText(this, "Bluetooth Turned ON", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Bluetooth Alredy ON", Toast.LENGTH_LONG).show();
        }

       //show the list of paired devices...
        startActivity(new Intent(this,DeviceList.class));
   }
}

    public void list(View v){
        pairedDevices = bluetoothAdapter.getBondedDevices();
        ArrayList list = new ArrayList();

        for(BluetoothDevice bt : pairedDevices)
            list.add(bt.getName());
        Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show();

        final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        lv.setAdapter(adapter);
    }

。现在我想将手机中的应用程序中的字符串发送到另一部手机中的同一个应用程序。那假设我连接了这个应用程序的两部手机(比如A和B)。我想从A到B

发送一个字符串(比如" 123")

我在网上经历了一些教程。我所理解的是,这需要一个套接字和一个唯一的密钥来识别应用程序。我怎么能这样做? 请详细说明。

1 个答案:

答案 0 :(得分:0)

您只能通过您提供的代码搜索可用的蓝牙设备。你是对的,你需要建立一个套接字连接来与其他设备通信。实现此链接中示例中给出的代码,您应该能够在另一台设备上看到您的字符串。 https://developer.android.com/guide/topics/connectivity/bluetooth.html

相关问题