मैं ब्लूटूथ के माध्यम से एंड्रॉइड डिवाइस से 'ए' जैसे एक साधारण स्ट्रिंग डेटा भेजना चाहता हूं। मैंने एंड्रॉइड एसडीके में नमूना ब्लूटूथ कोड देखा लेकिन यह मेरे लिए बहुत जटिल है मुझे समझ नहीं आ रहा है कि जब मैं कोई बटन दबाता हूं तो मैं केवल विशिष्ट डेटा कैसे भेज सकता हूं इस समस्या का समाधान किस प्रकार से किया जा सकता है?
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(); } } }