use*_*000 4 java android bluetooth
我知道如何通过调用手机的本机蓝牙设置发送文件,并让用户选择要发送给谁.
但是,假设我想将字符'v'直接发送到配对设备.我知道设备的名称和地址.这样做的最佳方法是什么?
你可以这样做:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
现在调用上面的方法就像
sendDataToPairedDevice("text to send" ,bluetoothDevice);
Run Code Online (Sandbox Code Playgroud)
而已.谢谢,享受好友.
| 归档时间: |
|
| 查看次数: |
4281 次 |
| 最近记录: |