Android IBeacon Development (i)

Source: Internet
Author: User

Recently the company is engaged in Ibeacon, because the company another learning Android, and the rest of the people, only I touch Java, Android, so I only to do this.

Statement, I am a C # programmer, Java, Android are weak, just slightly involved, do not understand, the error, a lot of advice.

At first, I search the Internet ibeacon information (using Baidu, alas, it seems I am 2B programmer AH), the most detailed only two, and these two are the same person, HELLOGV, podcast address: HTTP://MY.CSDN.NET/HELLOGV

The two posts I found,

http://blog.csdn.net/hellogv/article/details/24267685

http://blog.csdn.net/hellogv/article/details/24661777

Because there is no contact with this thing, so I can not understand AH. Had to download the code down, to actually run a bit.

Of course, I first download is the search base station that, or no base station, where I go to communicate??!!!

By the way, this ble is in Android 4.3.1 version and above to use OH.

All right, don't talk nonsense.

(1) Turn on Bluetooth

There is not much to say about this step.

Get Bluetoothmanager, Bluetoothadapter

1     /**2 * Initializes a reference to the local Bluetooth adapter.3      * 4      * @returnReturn True if the initialization is successful.5      */6      Public BooleanInitialize ()7     {8         //Use this check to determine whether BLE are supported on the device. then you can9         //selectively disable ble-related features. Ten         if(!Mcontext.getpackagemanager (). Hassystemfeature (Packagemanager.feature_bluetooth_le)) One         { ALOG.E (TAG, "Unable to initialize Bluetooth.")); -             return false; -         } the         //for API level and above, get a reference to Bluetoothadapter through -         //Bluetoothmanager. -         if(Mbluetoothmanager = =NULL) -         { +Mbluetoothmanager =(Bluetoothmanager) Mcontext.getsystemservice (context.bluetooth_service); -             if(Mbluetoothmanager = =NULL) +             { ALOG.E (TAG, "Unable to initialize Bluetoothmanager.")); at                 return false; -             } -         } -          -Mbluetoothadapter =Mbluetoothmanager.getadapter (); -         if(Mbluetoothadapter = =NULL) in         { -LOG.E (TAG, "Unable to obtain a bluetoothadapter.")); to             return false; +         } -          the         return true; *     } $     

Then turn on Bluetooth

     Public Boolean Openblue ()    {        // turn on Bluetooth        if (! mbluetoothadapter.isenabled ())             return mbluetoothadapter.enable ();         Else            return true ;    }

Because I write C # habits, the VS code style is like this, so, hey, can see on the line ha

(2) Scan

1     Private voidScanledevice (Final Booleanenable)2     {3         if(enable)//enable =true means to start scanning4         {5             //Stops scanning after a pre-defined scan period.6             //the code below is intended to stop scanning after scan_period .7             //If you need to keep scanning, you can comment out8Mhandler.postdelayed (NewRunnable ()9             {Ten @Override One                  Public voidRun () A                 { -Mscanning =false; - Mbluetoothadapter.stoplescan (mlescancallback); the                     //This is the Reset menu, which will be the Stop button in menu, the scan button - Invalidateoptionsmenu (); -                 } - }, Scan_period); +              -Mscanning =true;//This variable indicates whether the scan is +Mbluetoothadapter.startlescan (Mlescancallback);//This is the beginning of the scan. A         } at         Else -         { -Mscanning =false; -Mbluetoothadapter.stoplescan (Mlescancallback);//Stop the scan . -         } -         //This is the Reset menu, the Stop button, the Scan button in the menu in Invalidateoptionsmenu (); -}

Here's a variable mlescancallback, what's this?

1     //Device Scan callback.2     PrivateBluetoothadapter.lescancallback Mlescancallback =NewBluetoothadapter.lescancallback ()3     {4 @Override5          Public voidOnlescan (FinalBluetoothdevice device,intRssibyte[] scanrecord)6         {7IBeacon IBeacon =fromscandata (device, Rssi, Scanrecord); -         } +     }; A     

By exploring it, this thing is a callback function that calls the Mlescancallback.onlescan () function when Bluetooth receives a broadcast. Device is, of course, the broadcast equipment, RSSI is the signal strength, Scanrecord is broadcast information.

Then you can know the details of the device by parsing the Scanrecord. Formscandata I will not say more, in the source of the great God has.

After getting to ibeacon, you can put him in the list, how to display it is your problem.

Android IBeacon Development (i)

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.