Chọn thiết bị Bluetooth từ xa cho Communications
lớp Các BluetoothSocket có thể được sử dụng trên các thiết bị của khách hàng để bắt đầu một kênh thông tin liên lạc từ bên trong ứng dụng của bạn đến một máy chủ nghe Bluetooth Socket. Nó cũng được trả về bởi Bluetooth server socket Listener sau khi kết nối với một thiết bị của khách hàng đã được thành lập.
Tạo một khách hàng phía Bluetooth socket bằng cách gọi createRfcommSocketToServiceRecord trên một đối tượng BluetoothDevice đại diện các mục tiêu thiết bị máy chủ từ xa. Các thiết bị mục tiêu nên có một Bluetooth server socket lắng nghe các yêu cầu kết nối (như mô tả trong phần trước).
Có một số cách để có được một tham chiếu đến một thiết bị Bluetooth từ xa, và một số cảnh báo quan trọng liên quan đến các thiết bị mà bạn có thể tạo ra . một liên kết truyền thông
Yêu cầu thiết bị Bluetooth kết nối
để cho một socket Bluetooth để thiết lập một kết nối đến một thiết bị Bluetooth từ xa, các điều kiện sau đây phải đúng:
. ► các thiết bị từ xa phải được phát hiện
► các thiết bị từ xa phải được chấp nhận kết nối qua Bluetooth máy chủ socket.
► Các thiết bị địa phương và từ xa phải được ghép nối (ngoại quan). Nếu các thiết bị không được kết nối, người sử dụng của từng thiết bị sẽ được nhắc nhở để ghép chúng khi các yêu cầu kết nối được bắt đầu.
Việc tìm kiếm một thiết bị Bluetooth để kết nối Để
đối tượng thiết bị Bluetooth được sử dụng để đại diện cho các thiết bị từ xa. Bạn có thể truy vấn họ cho các thuộc tính của từng thiết bị từ xa, và để bắt đầu kết nối Bluetooth Socket.
Có một số cách để bạn có được BluetoothDevices trong mã. Trong mỗi trường hợp, bạn nên kiểm tra để đảm bảo rằng thiết bị bạn có ý định để kết nối là có thể phát hiện và (tùy chọn) xác định xem bạn đang liên kết với nó. Nếu bạn không thể khám phá các thiết bị từ xa, bạn nên nhắc nhở người dùng để kích hoạt khả năng khám phá về nó.
Bạn đã học được một kỹ thuật cho việc tìm kiếm phát hiện các thiết bị Bluetooth trước đó trong phần này. Sử dụng phương pháp startDiscovery và giám sát các chương trình phát sóng action_found cho phép bạn nhận Broadcast Intents bao gồm một BluetoothDevice.EXTRA_DEViCE phụ có chứa các thiết bị Bluetooth phát hiện ra.
Bạn cũng có thể sử dụng phương pháp getRemoteDevice trên adapter Bluetooth cục bộ của bạn, xác định địa chỉ phần cứng của thiết bị Bluetooth từ xa bạn muốn kết nối:
thiết bị BluetoothDevice = bluetooth.getRemoteDevice ( "01: 23: 77: 35: 2F: AA");
Điều này đặc biệt hữu ích khi bạn biết địa chỉ phần cứng của thiết bị mục tiêu, chẳng hạn như khi sử dụng một công nghệ như Android beam để chia sẻ thông tin giữa các thiết bị.
để tìm tập hợp các thiết bị hiện đang kết nối, gọi getBondedDevices trên adapter Bluetooth cục bộ. Bạn có thể truy vấn trả về thiết lập để tìm hiểu xem các thiết bị mục tiêu Bluetooth đã ghép nối với các adapter Bluetooth cục bộ.
Thức BluetoothDevice knownDevice =
bluetooth.getRemoteDevice ( "01: 23: 77: 35: 2F: AA");
SetbondedDevices = bluetooth.getBondedDevices ();
if (bondedDevices.contains (knownDevice))
// TODO thiết bị mục tiêu được ngoại quan / ghép nối với các thiết bị cục bộ.
Mở một khách hàng Bluetooth Ổ cắm kết nối
Để khởi tạo một kênh thông tin liên lạc đến một thiết bị từ xa, tạo ra một Bluetooth socket sử dụng các đối tượng BluetoothDevice đại diện cho nó.
đang được dịch, vui lòng đợi..