可以尝试以下两种方法来解决该问题:
private final UUID SPP_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //RN4678的SPP服务UUID
private final String MAC_ADDRESS = "xx:xx:xx:xx:xx:xx"; //RN4678的MAC地址
//创建BluetoothSocket实例并连接
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(MAC_ADDRESS);
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(SPP_UUID);
socket.connect();
//发送数据
OutputStream outputStream = socket.getOutputStream();
String message = "Hello RN4678";
byte[] bytes = message.getBytes(Charset.forName("UTF-8"));
outputStream.write(bytes);
//关闭连接
socket.close();
private final String MAC_ADDRESS = "xx:xx:xx:xx:xx:xx"; //RN4678的MAC地址
//连接设备并发送AT命令
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(MAC_ADDRESS);
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
socket.connect();
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
outputStream.write("AT+CONNL\r\n".getBytes());