Android PBAP client Bluetooth

Source: Internet
Author: User
Tags dashed line response code vcard

I. Introduction:

This feature specifically uses the phone book Access profile feature after bluetoothV2.1, referred to as PBAP. Currently MTK Android only implements the server-side function, The client is not implemented. So this document gives a brief description of how to develop a simple client. And at the end of the test demo. Follow the client's development process.

Two. Prerequisite settings:

This office describes some of the actions required to perform Bluetooth before pbap. It includes opening and searching.

First, add permissions to Manifest

<uses-permissionandroid:name="Android.permission.BLUETOOTH"/>

<uses-permissionandroid:name="Android.permission.BLUETOOTH_ADMIN"/>

Decide whether to open

Private Bluetoothadaptermbluetoothadapter;

if (null ==mbluetoothadapter) {

Mbluetoothadapter = Bluetoothadapter. Getdefaultadapter ();

}

if (!mbluetoothadapter.isenabled ()) {

Mstatus = CLOSE;

} Else {

Mstatus = OPEN;

}

If it is not turned on, the execution is turned on

Intent startbluetoothintent = new Intent (

Bluetoothadapter. action_request_enable);

Startactivityforresult (Startbluetoothintent, 1001); 1001 = BT OPEN

Collect feedback in Onactivityresult after execution is open

protected void  onactivityresult ( int  requestcode, int  resultcode, Intent data) {

if   (Requestcode ==1001) {//1001 = BT OPEN

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; If   (ResultCode = = Result_ok ) {

            mbtstatus.setcurrstatus (btstatus. OPEN );

      }  else  {

          //Select Cancel on Open dialog also feedback here

Open_failed );

      }

   }

}

Accept as OK in feedback, next you can start searching for nearby Bluetooth devices

Mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();

Mbluetoothadapter.startdiscovery ();

You need to listen to the following action when searching

Intentfilter iFilter = new Intentfilter ();

Ifilter.addaction (Bluetoothdevice.action_found);

Ifilter.addaction (bluetoothadapter.action_discovery_finished);


Store the searched bluetooth_device in the defined broadcast

Bluetoothdevice Bluetoothdevice = Intent

. Getparcelableextra (Bluetoothdevice. Extra_device);

The search can be canceled when the correlation function is

Mbluetoothadapter = Bluetoothadapter.getdefaultadapter ();

Mbluetoothadapter. Canceldiscovery ();

Three. Pairing connection of Bluetooth

The UUID here is defined in the agreement. This allows access to the server-side PBAP service.

Not seen in spec. But check out the Bluetooth UUID associated with this entry found

With 0x1103

My_uuid = uuid.fromstring ("0000112F-0000-1000-8000-00805F9B34FB");

Secure connection methods provided in the API

Bluetoothsocket socket = Device.createrfcommsockettoservicerecord (MY_UUID);

The connection function. This is the broken method, which needs to be put into thread to take processing

Socket.connect ();

The spec describes 2 devices that require gap authentication to establish a secure connection. This section uses the

The UUID here is defined in the agreement. This allows access to the server-side PBAP service.

Not seen in spec. But check out the Bluetooth UUID associated with this entry found

With 0x1103

My_uuid = uuid.fromstring ("0000112F-0000-1000-8000-00805F9B34FB");

Secure connection methods provided in the API

Bluetoothsocket socket = Device.createrfcommsockettoservicerecord (MY_UUID);

The connection function. This is the broken method, which needs to be put into thread to take processing

Socket.connect ();

After the above steps are finished, a pairing dialog box will pop up on both the server and the client. After both sides are paired successfully. The server will also pop up a confirmation of whether to allow the other person to get the contact. The socket.connect is not executed until after confirmation.

There's so much going on up there. In fact, it is necessary to operate in the prophase. At this point 2 phones are connected. and established a PBAP connection.

Four. Phonebook DownLoad Feature

The following is the start of the server-side phone download feature. The function flowchart in spec is as follows

These steps are described separately below:

1. PCE Setup and PSE connection

First, declare the connection in the previous section of the connection is two different things. The first section is 2 machines to establish a Bluetooth connection

Here is the connection for OBEX.

Client Request Packet Format

Client Request Explanation:

Op

001f entire Packet length

Version Obex, always 10

00 Flags always

2000 Maximum length of packet 2k

Below 7-n for header please view OBEX spec

Target

0013 the length of the target header

796135f0f0c511d809660800200c9a66 here doesn't know where it came from. But that's it.

C3 length

0000f483 Length Value

Now when you find connect. This string of strings is not needed to become. It's definitely this.

Server Feedback Packet Format

Server Response explains:

This is a random search for a mobile phone to do the test results. But it's usually quite different. Key point Connect ID

A0 response code (only A0 is successful, others are all kinds of errors)

001f length

Version Obex

Flags of XX

4BC8 Maximum Packet length

Here are the header information. For headers See OBEX spec

Cb Connect ID (key. Very important)

00000007connect ID Value (this value needs to be stored.)

4a Who

0013 who length

796135f0f0c511d809660800200c9a660000000 who value (same as target above)

After judging response code for A0, you can operate the next step.

2. PCE Download Contact

PCE download the contact needs to use the Get method.

In Obex the Get method is defined as follows

The following requirements are defined in the PBAP spec

Follow the description. The data is organized as follows

(can also be 03)

004f the length of the entire packet. The last manual calculation results

Cb Connect ID

00000007 here is the data obtained after the completion of the Connect method above.

The name

0021 Length of name

00740065006c00650063006f006d002f00700062002e0076006300660000

The above is the Unicode code for TELECOM/PB.VCF. The end must be 0000. The definition of name in OBEX must be processed in Unicode. This string represents all the contacts that were taken to the other phone's storage.

Type

0012 Length of type

782d62742f70686f6e65626f6f6b00 for X-bt/phonebook end must be used 00. Here

4c App Params

The length of the 0014 app params

06 Here is a PBAP customization, representing Vcardfilter

08 8-bit

0000000000000000 64-bit mask. If you need to see spec. All 0, return all

Version vcard

01 length

01 01 = 3.1 00 = 2.0

How many Maxlistcount to take

02 Length

Ffff Ffff means to take all.

The example here is to take all of the pure in the contact person's phone.

The parameters of the Maxlistcount are not tested. It is not known whether the setting is useful here.

The version of the vcard, the server will be defined at the same time 2.0 and 3.1 of the Vcardbuilder, which is required in the Protocol. All for the client specific we want to use that, as we use that Vard Praser to decide.

Filter is actually very useful. What data we are getting back. After all, if the server's contacts have avatars or something like that. If we take them all back. We have to think about the speed problem.

Liststartoffset, change the example to take all. All this parameter is not passed. The default is 0.

Server Feedback Packet Format

No data is stored here. For the time being there are no examples. Analysis also ibid.

Response code must be a0 to continue parsing

The resolution of the header to the body or end of body. The buffer is stored according to length. Save directly in the VCF file. If you implement the vcard Praser in code, It can be parsed and loaded directly into the contact database.

By the way: for PBAP.MTK does not support calllog processing. Some of the other specs are supported.

3. The dashed line indicates that it can be looped here.

This is not explained in detail here. You can view the virtual folder in the PBAP spec. The features here can be downloaded separately from server side 1 or several contacts.

To implement the functionality here. It is better to understand the function of phonebook browser first.

4. PCE Terminal connection to PSE

Call Obex's disconnect function. The code is not implemented at this time

Five. Current summary

The demo is easy to complete. The local contacts that are saved to the T-card VCF can be imported normally. But the code flow is all in accordance with some normal operation to go. Follow up on the need to add all the necessary error handling.

Resources:

OBEX image transfer using the JSR-82 API

http://419443161-qq-com.iteye.com/blog/544299

Research and implementation of PIM synchronization technology for Android phone

Http://cdmd.cnki.com.cn/Article/CDMD-10614-1012470693.htm

PBAP Simplified Chinese version of the agreement

Http://www.docin.com/p-204262132.html

Related Article

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.