How to find and connect to a remote device

Source: Internet
Author: User
How to find and connect to a remote device

When an application connects to a remote device to apply a certain service of the application, we need to take the following steps: first, check whether there is a device that you want to communicate. Check whether the required service is available. Finally, the application needs to be connected, and some security measures may be required.

The following topics introduce the relevant steps in sequence:

  • How to select a remote device
  • How to query remote devices
  • How to query services on a remote device
  • How to connect and transmit data with remote devices
  • How to Apply security measures on external sockets
How to select a remote device

An application can use the following methods to determine which remote device is the object for communication:

· Fixed connections

· Storage Configuration

· Selected by users

· Automatic Program decision

Each of the above may be appropriate. In the first two methods, the address of the remote device only needs to be retrieved from the storage. The third method is generally provided in the Bluetooth UI.

The last program automatically selects a remote device and may be applied in the following situations:

· Provides users with customizable device selection UI

· In some special circumstances, select a device, for example, "select the most idle LAN Access Point"

How to query remote devices

Each bluetooth device has a unique 48-bit address in its hardware. A basic device Query within a range returns zero or several such addresses.

In addition to this address, the bluetooth device also has a text name displayed to the user. If you want to display the list of available devices to users, You need to obtain these names for display.

If the underlying hardware supports this function, the address and name can be queried at the same time. Otherwise, the address query must be completed before the name query request is sent.

Generally, the socket class fig is used to query the address and device name. In addition, tinquirysockaddr is a specialized Bluetooth socket address class, which encapsulates the Bluetooth address and query entry code to provide related query services and devices.

Obtain the address of the remote device

To query the address of a remote device, follow these steps:

1. Connect to the socket server (RSocketServ), And then useRSocketServ::FindProtocol()Select the Protocol to use. Because the name and address query are supported by the btlinkmanager protocol layer of the stack, select it.

2. Create and initializeRhostResolverObject.

3. SetTinquirySockAddrParameter for query: For address query, KHostResInquiryThe flag must passTInquirySockAddr::SetAction()Set.

Then you can useRHostResolver::GetByAddress()Start query.

How to query remote device services

One remote device can have one or more Bluetooth service providers. A wide range of indicators of the service can be read from the device class. This can be used to select the appropriate device icon for the user. This device class can be used after querying the addressTInquirySockAddr::MajorClassOfDevice().

In a specific environment, coarse-level service discovery provided by the device class does not meet the selection criteria for the device. In this way, the Service Discovery request can be used to determine whether it is a suitable choice. But it should be applied only when necessary because it takes some extra time to send SDP requests to each device. SDP request passedBluetooth discovery proxy API.

How to connect and transmit data with remote devices

Once the device and service are confirmed, you can connect to the remote device and start using it.

Use the generalized Symbian OS socket to connect to the device by using the connect () function of rsocket. Bluetooth sockets can be opened using L2CAP and RFCOMM protocols. For an L2CAP Bluetooth socket, the port is used to connect to the Protocol/service multiplexing (PSM); For RFCOMM, the port is the server channel. If these values are unknown, you can obtain them by reading the protocoldescriptorlist service attribute. For more information, see useBluetooth service discovery proxy

You can read and write data (AT command, text, HTTP, PPP, etc.) in the expected format of the target service through the socket)

Example:

// Assume have a TInquirySockAddr object, addr, with relevant device info

// Connect an L2CAP socket
RSocket socket;
TRequestStatus status;
User::LeaveIfError(socket.Open(socketServ,KBTAddrFamily,KSockSeqPacket,KL2CAP));
User::LeaveIfError(socket.Connect(addr,status));
User::WaitForRequest(status);

if (status == KErrNone)
    {
    // Write some simple data
    _LIT8(KDataToWrite,"01234");
    socket.Write(KDataToWrite,status);
    User::WaitForRequest(status);
    }

// Close socket
socket.Close();

Note:

· For RFCOMM and L2CAP protocols, no data can be found inConnect()AndShutdown()Sending in the call, otherwise it will lead to an error in obtaining additional data.

· RFCOMM uses the stream socket type (KSockStream).

· L2CAP uses the socket type of the sequence package (KsockSeqPacket).

How to Apply security measures on external sockets

For an external connection, the Bluetooth Stack is not implemented by default. If the remote device requires authentication or encoding connection, the stack is directly reflected in the application.

Sometimes, local programs also need to ensure authentication or encoding connections. You can use IOCTLs to authenticate rsocket.KHCIAuthRequestIoctl) Or encoding (KHCIEncryptIoctl) Connection.

Example

In the following example, IOCTL is applied to the authentication request.

// Assume have an opened L2CAP RSocket object, socket
socket.Ioctl(KHCIAuthRequestIoctl, status, NULL, KSolBtHCI);
User::WaitForRequest(status);
if (status==KErrNone)
    {
    // Success...

PS: I am still learning. If you have any mistakes, please advise me.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.