Tôi muốn gửi một dữ liệu chuỗi đơn giản như 'a' từ thiết bị Android sang thiết bị khác qua bluetooth. Tôi nhìn mẫu mã bluetooth trong sdk android nhưng nó rất phức tạp đối với tôi. Tôi không thể hiểu làm thế nào tôi có thể gửi dữ liệu cụ thể chỉ khi tôi nhấn một nút. Làm thế nào tôi có thể giải quyết vấn đề này?Mã mẫu bluetooth Android để gửi một chuỗi đơn giản qua bluetooth
Trả lời
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();
}
}
}
Cám ơn relpy của bạn. Thêm vào đó, làm thế nào tôi có thể nhận được tin nhắn này từ thiết bị khác? – user3374956
@ user3374956 nói chung bạn cần phải đọc dữ liệu từ 'InputStream'. Cách nhận dữ liệu tùy thuộc vào người gửi. Tôi đã cập nhật mã. Yêu cầu phải có – eleven
giấy phép? – Prasad
- 1. Mẫu mã cho lập trình Android Bluetooth
- 2. Gửi nhiều tệp qua Bluetooth
- 3. gửi/nhận dữ liệu qua Bluetooth
- 4. ADB qua Bluetooth Android
- 5. Gửi tệp bằng bluetooth android?
- 6. Lỗi khi gửi tệp qua bluetooth trong android?
- 7. Ứng dụng đơn giản mô phỏng phụ kiện bluetooth
- 8. Android ymodem qua bluetooth trong Java
- 9. In Bluetooth Android
- 10. Truyền tệp Bluetooth Android
- 11. Arduino + Bluetooth module + Android
- 12. Cách gửi/nhận dữ liệu qua điện thoại bluetooth android đến thiết bị y tế có bluetooth
- 13. Máy in hóa đơn Bluetooth Bluetooth có hỗ trợ?
- 14. Gỡ lỗi thiết bị Android qua Bluetooth
- 15. Làm thế nào để gửi giá trị Hex qua Bluetooth trong Android
- 16. Android: In Bluetooth
- 17. Cách gửi tệp đến các iPhone khác qua Bluetooth
- 18. MIDI qua Bluetooth
- 19. Cổng COM Bluetooth Android
- 20. Truyền lượng dữ liệu lớn qua bluetooth trên Android Gingerbread
- 21. Lỗi Transmisison dữ liệu bằng SPP qua Bluetooth trên Android
- 22. Kết nối Bluetooth; không thể gửi các chuỗi đúng cách
- 23. Android 6 bluetooth
- 24. Lập trình Bluetooth trên Android
- 25. lõi Bluetooth chậm khi gửi gói tin
- 26. Không thể hiểu mHandler.obtainMessage() trong mẫu Android Bluetooth
- 27. Biểu mẫu đơn giản không gửi dữ liệu qua _POST
- 28. Gửi Lệnh tới Bluetooth từ thiết bị Android
- 29. Android: Phát âm thanh qua tai nghe Bluetooth Sco
- 30. Android Bluetooth - Không thể kết nối ra
Bạn có thể tham khảo [ở đây] cũng (https://stackoverflow.com/questions/13450406/how-to-receive-serial-data-using-android-bluetooth) –