简体   繁体   中英

Sending character via bluetooth on Android?

I know how to send files by invoking the phone's native bluetooth settings and letting the user choose who to send to.

But let's say I want to send the character 'v' directly to a paired device. I know the device's name, and address. What is the best way to go about doing this?

You can do it in this way:

    private void sendDataToPairedDevice(String message ,BluetoothDevice device){       
           byte[] toSend = message.getBytes();
            try {
                UUID applicationUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
                BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
                OutputStream mmOutStream = socket.getOutputStream();
                mmOutStream.write(toSend);
                // Your Data is sent to  BT connected paired device ENJOY.
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

Now Call above method like

sendDataToPairedDevice("text to send" ,bluetoothDevice);

thats it. thanks, enjoy buddy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM