Android Learning Bluetooth Summary

Source: Internet
Author: User

Bluetooth is API Level 5 There is a technology because I am interested in this technology to learn the next will learn the knowledge and share the next

Client
private static final UUID my_uuid_secure = uuid.fromstring ("Fa87c0d0-afac-11de-8a39-0800200c9a66"); Safe for
private static final UUID my_uuid_insecure = uuid.fromstring ("8ce255c0-200a-11e0-ac64-0800200c9a66"); Not safe.


First, let's add the Bluetooth permission
<uses-permission android:name= "Android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name= "Android.permission.BLUETOOTH"/>

To scan to Bluetooth Bluetooth, the caller must be visible
Bluetoothadapter.action_request_discoverable can be visible via intent boot

Bluetooth is visible, we get the adapter to get Bluetooth

Bluetoothadapter.getdefaultbluetoothadapter ();

In the StartDiscovery () method through the Bluetooth adapter start scanning peripheral visible Bluetooth by binding broadcast filter new Intentfilter (Bluetoothdevice.action_found)//representative found Bluetooth

Bluetoothdevice device = Intent.getparcelableextra (Bluetoothdevice.extra_device); Get the driver for Bluetooth to get the address and related information based on the driver
if (device.getbondstate ()! = bluetoothdevice.bond_bonded) {
}

You can also get the driver by Getbondeddevices () This method returns a collection

To connect to the server we get the address through the drive via Getremotedevice (addresses); Gets the driver for the specified bluetooth

In the Createinsecurerfcommsockettoservicerecord through this drive (my_uuid_insecure); It's not safe, it doesn't need to be verified.

Createrfcommsockettoservicerecord (my_uuid_secure); This is safe, it needs to be verified.


Both of them will return a Bluetoothsocket object we call Connect () and the server-side connection through him

If the connection succeeds, it can be written and read by the input or output stream he gets.


Server-side

We pass Blueadapter's Listenusinginsecurerfcommwithservicerecord (name_insecure,my_uuid_insecure);//unsafe these two methods is corresponding to the two methods of the client

Listenusingrfcommwithservicerecord (Name_secure, my_uuid_secure); Safe for


They will return a Bluetoothserversocket object in the Accpet () method through this object (note that this method is going to block the main thread we're going to put him on child threads) to return a Bluetoothsocket object

To transfer data by acquiring input or output streams through the socket object

Bluetooth explanation is complete, the first time to write blog also hope Daniel advice

Android Learning Bluetooth Summary

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.