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