Android示例蓝牙代码通过蓝牙发送一个简单的string

我想通过蓝牙从Android设备发送一个简单的string数据,如“a”。 我在android sdk中查找了示例蓝牙代码,但对于我来说这非常复杂。 当我按下button时,我无法理解如何只发送特定的数据。 我怎么解决这个问题?

Solutions Collecting From Web of "Android示例蓝牙代码通过蓝牙发送一个简单的string"

private OutputStream outputStream; private InputStream inStream; private void init() throws IOException { BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter(); if (blueAdapter != null) { if (blueAdapter.isEnabled()) { Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices(); if(bondedDevices.size() > 0) { Object[] devices = (Object []) bondedDevices.toArray(); BluetoothDevice device = (BluetoothDevice) devices[position]; ParcelUuid[] uuids = device.getUuids(); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid()); socket.connect(); outputStream = socket.getOutputStream(); inStream = socket.getInputStream(); } Log.e("error", "No appropriate paired devices."); } else { Log.e("error", "Bluetooth is disabled."); } } } public void write(String s) throws IOException { outputStream.write(s.getBytes()); } public void run() { final int BUFFER_SIZE = 1024; byte[] buffer = new byte[BUFFER_SIZE]; int bytes = 0; int b = BUFFER_SIZE; while (true) { try { bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes); } catch (IOException e) { e.printStackTrace(); } } }